Facebook
From ryan, 2 Months ago, written in Verilog.
Embed
Download Paste or View Raw
Hits: 180
  1. module ALU_tb;
  2.  
  3.     // Parameters
  4.     parameter CLK_PERIOD = 10; // Clock period in ns
  5.  
  6.     // Signals
  7.     reg [31:0] operand1;
  8.     reg [31:0] operand2;
  9.     reg [4:0] operation_select;
  10.     wire [31:0] result;
  11.     wire Z, N, C, V;
  12.  
  13.     // Instantiate the ALU module
  14.     alu32 dut (
  15.         .operand1(operand1),
  16.         .operand2(operand2),
  17.         .operation_select(operation_select),
  18.         .result(result),
  19.         .Z(Z),
  20.         .N(N),
  21.         .C(C),
  22.         .V(V)
  23.     );
  24.  
  25.     // Clock Generation
  26.     reg clk = 0;
  27.     always #((CLK_PERIOD / 2)) clk = ~clk;
  28.  
  29.     // Test stimulus
  30.     initial begin
  31.         // Initialize inputs
  32.         operand1 = 32'h12345678;
  33.         operand2 = 32'h87654321;
  34.        
  35.         // Test LD (Load) operation
  36.         operation_select = 5'b00001; // LD
  37.         #20;
  38.        
  39.         // Test ADD operation
  40.         operation_select = 5'b00011; // ADD
  41.         #20;
  42.        
  43.         // Test SUB operation
  44.         operation_select = 5'b00100; // SUB
  45.         #20;
  46.        
  47.         // Test AND operation
  48.         operation_select = 5'b00101; // AND
  49.         #20;
  50.        
  51.         // Test OR operation
  52.         operation_select = 5'b00110; // OR
  53.         #20;
  54.        
  55.         // Test XOR operation
  56.         operation_select = 5'b00111; // XOR
  57.         #20;
  58.        
  59.         // Test INV operation
  60.         operation_select = 5'b01000; // INV
  61.         #20;
  62.        
  63.         // Test SHL operation
  64.         operation_select = 5'b01001; // SHL
  65.         #20;
  66.        
  67.         // Test SHR operation
  68.         operation_select = 5'b01010; // SHR
  69.         #20;
  70.        
  71.         // End simulation
  72.         $finish;
  73.     end
  74.  
  75. endmodule
  76.