Hi! I want to write the code for a serializer which works both on rising
edge and falling edge of the clock. The idea is to write two registers,
one for the even bits and the other one for the odd bits, and then a MUX
which send out the data. Moreover, I have a control signal called LOAD:
when LOAD is high, I want to load the data into the two registers. The
problem is that the even bits are not loaded into the register and I do
not know why because I wrote the same piece of code both for the odd
bits and the even ones. Can anyone help me? Thank you very much! The
code is hereinafter.
1 | module serializer (data_in, send, clk, load, rst, data_out);
|
2 |
|
3 | input wire [11:0] data_in;
|
4 | input wire send, clk, load, rst;
|
5 |
|
6 | output reg data_out;
|
7 |
|
8 | reg [7:0] temp_data_out_even;
|
9 | reg [7:0] temp_data_out_odd;
|
10 |
|
11 | integer i;
|
12 | integer j;
|
13 |
|
14 | always @ (posedge clk)
|
15 | begin
|
16 | if (rst) begin
|
17 | data_out <= 1'b0;
|
18 | temp_data_out_odd <= 8'b00000000;
|
19 | end
|
20 | else if (load) begin
|
21 | temp_data_out_odd <= 8'b11000000;
|
22 | for(i=0; i<6; i=i+1)
|
23 | temp_data_out_odd[5-i] <= data_in[11-i*2];
|
24 | end
|
25 | else if (send) begin
|
26 | data_out <= temp_data_out_odd[7];
|
27 | temp_data_out_odd <= {temp_data_out_odd[6:0], 1'b0}; // Shifting phase
|
28 | end
|
29 | end
|
30 |
|
31 | always @ (negedge clk)
|
32 | begin
|
33 | if (rst) begin
|
34 | data_out <= 1'b0;
|
35 | temp_data_out_even <= 8'b00000000;
|
36 | end
|
37 | else if (load) begin
|
38 | temp_data_out_even <= 8'b00000000;
|
39 | for(j=0; j<6; j=j+1)
|
40 | temp_data_out_even[5-j] <= data_in[10-i*2];
|
41 | end
|
42 | else if (send) begin
|
43 | data_out <= temp_data_out_even[7];
|
44 | temp_data_out_even <= {temp_data_out_even[6:0], 1'b0}; // Shifting phase
|
45 | end
|
46 | end
|
47 |
|
48 | endmodule //end module serializer
|
The testbench is the following
1 | `include "serializer.v"
|
2 | `timescale 1ns / 1ps
|
3 |
|
4 | module serializer_tb();
|
5 | // Declare inputs as regs and outputs as wires
|
6 | reg [11:0] data_in;
|
7 | reg send, clk, load, rst;
|
8 | wire data_out;
|
9 | reg [11:0] reg_sipo;
|
10 | reg check;
|
11 | integer i;
|
12 |
|
13 |
|
14 | // Initialize all variables
|
15 | initial begin
|
16 | clk = 1; // initial value of clock
|
17 | rst = 1; // initial value of reset
|
18 | load = 0;
|
19 | send = 0;
|
20 | #15 rst = 0; // De-assert the reset
|
21 |
|
22 | forever begin
|
23 | foreach(data_in[j]) data_in[j] = $urandom;
|
24 | #13 load = 1;
|
25 | #20 load = 0;
|
26 | #5 send = 1;
|
27 | #20 //header
|
28 | for(i=0; i<12; i=i+1) begin
|
29 | #5 reg_sipo[11-i] <= data_out;
|
30 | end
|
31 | #10 send = 0;
|
32 | if(reg_sipo == data_in) check = 0;
|
33 | else check = 1;
|
34 | end
|
35 | end
|
36 |
|
37 | // Clock generator
|
38 | always begin
|
39 | #5 clk = ~clk; // Toggle clock every 5 ns - 100 MHz
|
40 | end
|
41 |
|
42 | // Connect DUT to test bench
|
43 | serializer U_serializer (data_in, send, clk, load, rst, data_out);
|
44 |
|
45 | endmodule
|