これは、
とあるシリアルプロトコルで出力されたデータを
module xxx_serial(
input clk,
input rstn,
output reg to,
input rx,
output tx,
inout [7:0] d,
output [9:0] dac,
output ldacn,
output wrn,
output clrn,
output gain,
output led,
output pdn
);
reg [5:0] bcount; // baudrate counter
reg [5:0] tcount; // tx counter
reg [8:0] txbuf;
reg txf; // tx flag
reg [3:0] count; // Serial counter
reg [15:0] buff; // Serial buffer
reg [9:0] vout; // 10bit DAC buffer
reg dacw;
wire cko,ckf,tco,rst;
always @(posedge clk) begin
if (bcount==43) begin
bcount <= 0;
end
else begin
bcount <= bcount+1;
end
end
always @(posedge clk or posedge rst) begin
if (rst) begin
txf <= 0;
txbuf <= 9'b111111111;
end
else if (txf==1 && bcount==0) begin
if (tcount==1) begin
txbuf <= {4'b0011,1'b0,to,vout[9:8],1'b0};
end
else if (tcount==11) begin
txbuf <= {4'b0011,vout[7:4],1'b0};
end
else if (tcount==21) begin
txbuf <= {4'b0011,vout[3:0],1'b0};
end
else if (tcount==31) begin
txbuf <= {8'h0D,1'b0};
end
else if (tcount==41) begin
txf <= 0;
end
else begin
txbuf <= {1'b1,txbuf[8:1]};
end
end
else if (rx==0) begin
txf <= 1;
end
end
always @(posedge clk) begin
if (!txf) begin
tcount <= 0;
end
else if (txf && bcount==0) begin
tcount <= tcount+1;
end
end
always @(posedge cko or posedge rst) begin
if (rst) begin
buff <= 0;
dacw <= 1;
vout <= 0;
end
else begin
if (ckf) begin
if (count==7) begin // 8bit - 1frame
vout <= {buff[7:1],3'b000};
to <= buff[0];
end
else if (count==15) begin // 16bit - 1frame
vout <= {~buff[15],buff[14:6]};
to <= buff[5];
end
count <= 0;
dacw <= 0; // DAC WR set
end
else begin
dacw <= 1; // DAC WR clear
count <= count+1;
end
buff <= {buff[14:0],tco}; // Serial-Parallel convert
end
end
assign rst = ~rstn;
assign cko = ~d[1];
assign ckf = ~d[3];
assign tco = ~d[5];
assign ldacn = 0; // DAC ldac enable
assign pdn = 1; // DAC pd disable
assign clrn = 1; // DAC clr disable
assign gain = 0; // DAC gain 0-Vref
assign wrn = dacw; // DAC write
assign dac = vout; // 10bit -> 10bit
assign led = ~to;
assign tx = txbuf[0];
endmodule