El código:
module demux( input clk, rst_n, NewPacket,
input [7:0] DataIn,
output reg[7:0] Dataout0,Dataout1,Dataout2,Dataout3,Dataout4,Dataout5,Dataout6,Dataout7);
reg[1:0] state, state_n;
reg[7:0] Data_temp1;
reg[3:0] packet_size;
reg[2:0] outport;
parameter S0 = 2'd0,
S1 = 2'd1,
S2 = 2'd2;
always @(posedge clk)
if(!rst_n)
state <= S0;
else begin
state <= state_n;
Data_temp1 <= DataIn;
if(packet_size > 0)begin
packet_size <= packet_size - 1;
outport <= outport + 1;
end
end
always @(state, NewPacket, packet_size) begin
state_n = 2'bx;
case(state)
S0: if(NewPacket)
state_n = S1;
else
state_n = S0;
S1: begin
packet_size = Data_temp1[3:0];
outport = Data_temp1[6:4] - 1;
state_n = S2;
end
S2: if(packet_size > 0)
state_n = S2;
else
state_n = S0;
endcase
end
always @(posedge clk)
if(!rst_n)
{Dataout0,Dataout1,Dataout2,Dataout3,Dataout4,Dataout5,Dataout6,Dataout7} <= 64'b0;
else begin
{Dataout0,Dataout1,Dataout2,Dataout3,Dataout4,Dataout5,Dataout6,Dataout7} <= 64'b0;
if(state == S2)
case(outport)
0: Dataout0 <= Data_temp1;
1: Dataout1 <= Data_temp1;
2: Dataout2 <= Data_temp1;
3: Dataout3 <= Data_temp1;
4: Dataout4 <= Data_temp1;
5: Dataout5 <= Data_temp1;
6: Dataout6 <= Data_temp1;
7: Dataout7 <= Data_temp1;
endcase
end
endmodule
El banco de pruebas:
'timescale 1ns / 1ps
module demux_tb;
// Inputs
reg clk;
reg rst_n;
reg NewPacket;
reg [7:0] DataIn;
// Outputs
wire [7:0] Dataout0;
wire [7:0] Dataout1;
wire [7:0] Dataout2;
wire [7:0] Dataout3;
wire [7:0] Dataout4;
wire [7:0] Dataout5;
wire [7:0] Dataout6;
wire [7:0] Dataout7;
// Instantiate the Unit Under Test (UUT)
demux uut (
.clk(clk),
.rst_n(rst_n),
.NewPacket(NewPacket),
.DataIn(DataIn),
.Dataout0(Dataout0),
.Dataout1(Dataout1),
.Dataout2(Dataout2),
.Dataout3(Dataout3),
.Dataout4(Dataout4),
.Dataout5(Dataout5),
.Dataout6(Dataout6),
.Dataout7(Dataout7)
);
initial
forever #10 clk <= ~clk;
initial
begin
clk = 1;
rst_n = 1;
NewPacket = 0;
DataIn = 0;
#20 rst_n = 0;
#60 rst_n = 1;
#20 DataIn = 8'h73;
NewPacket = 1;
#20 DataIn = 8'h11;
NewPacket = 0;
#20 DataIn = 8'h22;
#20 DataIn = 8'h33;
#20 DataIn = 8'h44;
#20 DataIn = 8'b0;
#20 DataIn = 8'h02;
NewPacket = 1;
#20 DataIn = 8'h88;
NewPacket = 0;
#20 DataIn = 8'h99;
end
endmodule
En mi opinión, state_n debería cambiar a 1 primero y el siguiente clk state cambiar a 1 y state_n cambiar a 2. Pero en forma de onda, state_n cambiar a 2 desde 0 directamente, ¿Por qué?