`timescale 1ns/1ps // Independently exercise96 waiting plus3 urgent slots and every stored bit. module motor_queue_storage_tb; reg clk=0;always #20 clk=~clk; reg rst_n=0,we=0,re=0; reg [6:0] wa=0,ra=0; reg [127:0] input_word=0; wire busy,valid; wire [127:0] output_word; motor_queue_storage #(.SLOTS(99)) dut(.clk(clk),.rst_n(rst_n),.wr_req(we),.rd_req(re), .wr_slot(wa),.rd_slot(ra),.wr_data(input_word),.wr_busy(busy),.rd_data(output_word),.rd_valid(valid)); reg [127:0] expected[0:98]; integer slot,k,cycles; reg [127:0] value; task write_slot(input integer n,input [127:0] word_value); begin @(negedge clk);wa=n;input_word=word_value;we=1; @(negedge clk);we=0;cycles=0; while(busy && cycles<10)begin @(negedge clk);cycles=cycles+1;end if(busy || cycles!=5)$fatal(1,"five-word write completion %0d",cycles); end endtask task read_slot(input integer n); begin @(negedge clk);ra=n;re=1;@(negedge clk);re=0;cycles=0; while(!valid && cycles<20)begin @(negedge clk);cycles=cycles+1;end if(!valid || cycles!=10 || output_word!==expected[n]) $fatal(1,"serial queue slot%0d cycles%0d data%032h expected%032h",n,cycles,output_word,expected[n]); end endtask initial begin repeat(4)@(negedge clk);rst_n=1; for(slot=0;slot<99;slot=slot+1)begin value=0;value[7:0]=slot<96 ? slot/32 : slot-96;value[15:8]=slot%5; value[23:16]=8'h0A | (slot[0] ? 8'h05 : 8'h00); value[63:32]=32'h80000001 ^ (slot*32'h1020101); value[95:64]=32'hFFFFFFFF-slot*1024;value[111:96]=16'hA500+slot; expected[slot]=value;expected[slot][7:0]=0;expected[slot][17]=0;expected[slot][19]=0; write_slot(slot,value); end for(slot=98;slot>=0;slot=slot-1)read_slot(slot); // Walk every one of the85 stored bits in the last address slot. for(k=0;k<85;k=k+1)begin value=0; if(k<3)value[8+k]=1; else if(k==3)value[16]=1; else if(k==4)value[18]=1; else if(k<37)value[32+k-5]=1; else if(k<69)value[64+k-37]=1; else value[96+k-69]=1; expected[98]=value;write_slot(98,value);read_slot(98); end // Write one slot while the other port reads a different valid slot. expected[31]=128'h0000BEEFCAFEBABE89ABCDEF00050000; @(negedge clk);wa=31;input_word=expected[31];we=1;ra=32;re=1; @(negedge clk);we=0;re=0;wait(valid);@(negedge clk); if(output_word!==expected[32])$fatal(1,"independent queue read/write ports interfered"); read_slot(31);read_slot(32); $display("QUEUE_WAITING_SLOTS=96 URGENT_SLOTS=3 STORED_BITS=85 WRITE_CLOCKS=5 READ_CLOCKS=10"); $display("motor_queue_storage_tb PASS");$finish; end initial begin #1000000;$fatal(1,"queue storage bounded deadline");end endmodule