Lab 7
Lab 7
`timescal 1ns/1ps
module counter_tb();
reg clk_tb;
reg rst_tb;
reg en_tb;
wire [3:0] count_tb;
counter DUT(
.clk(clk_tb),
.rst(rst_tb),
.en(en_tb),
.count(count_tb)
);
//initial signal
initial begin
clk_tb <=1'b0;
rst_tb <= 1'b1;
en_tb <=1'b0;
end
//clock gen
parameter CLOCK_PERIOD =10;
always begin
#((CLOCK_PERIOD)/2) clk_tb <= ~clk_tb;
end
initial begin
#10 rst_tb <= 1'b0;
#20 en_tb <= 1'b1;
#100 rst_tb <= 1'b1;
#10 rst_tb <= 1'b0;
#100 en_tb <= 1'b0;
#50 $stop;
end endmodule
///câu 1
module led_7_segment(
input wire [3:0] SW,
output reg [0:6] HEX0
);
endmodule
///testbench
`timescalse 1ns / 1ps
module led_7_segment_tb(
reg [3:0]SW_tb,
wire [0:6]HEX0_tb);
led_7_segment DUT(
.SW(SW_tb),
.HEX0(HEX0_tb));
//initial signal
integer i;
initial begin
for(i=0, i<16,i+=1)begin
SW_tb=i[3:0]
#10
end
$stop;
end
endmodule
//////câu 2
// ====================================================================
// 32-bit Counter Module
// ====================================================================
module counter_32bit (
input wire clk, // Clock signal
input wire reset, // Reset signal (active high)
input wire enable, // Enable signal (active high)
output reg [31:0] count // 32-bit counter output
);
endmodule
// ====================================================================
// Testbench for 32-bit Counter
// ====================================================================
`timescale 1ns/1ps
module tb_counter_32bit;
// Test signals
reg clk;
reg reset;
reg enable;
wire [31:0] count;
// Test sequence
initial begin
// Initialize signals
reset = 0;
enable = 0;
// Display header
$display("====================================================================");
$display("32-bit Counter Testbench - ModelSim Simulation");
$display("Clock Period: 10ns");
$display("====================================================================");
$display("Time(ns) | Reset | Enable | Count");
$display("---------+-------+--------+----------");
// End simulation
$display("\n=== Simulation Complete ===");
$display("Total simulation time: %0t ns", $time);
#50;
$finish;
end
endmodule
// ====================================================================
// Alternative Asynchronous Reset Version (Optional)
// ====================================================================
module counter_32bit_async_reset (
input wire clk, // Clock signal
input wire reset, // Asynchronous reset signal (active high)
input wire enable, // Enable signal (active high)
output reg [31:0] count // 32-bit counter output
);
endmodule
// ====================================================================
// Comprehensive Testbench with Both Reset Types
// ====================================================================
module tb_comprehensive_counter;
counter_32bit_async_reset async_counter (
.clk(clk),
.reset(reset_async),
.enable(enable_async),
.count(count_async)
);
// Clock generation
initial begin
clk = 0;
forever #5 clk = ~clk;
end
// Comprehensive test
initial begin
// Initialize
reset_sync = 0; enable_sync = 0;
reset_async = 0; enable_async = 0;
$display("====================================================================");
$display("Comprehensive Counter Comparison - Sync vs Async Reset");
$display("====================================================================");
// Monitoring
always @(*) begin
$display("Time: %0t | Sync: %0d | Async: %0d", $time, count_sync, count_async);
end
endmodule
/////////câu 3
// ====================================================================
// 32-bit ROM Module (Read-Only Memory)
// ====================================================================
module rom_32bit (
input wire clk, // Clock signal
input wire chipselect, // Chip select (active high)
input wire [3:0] address, // 4-bit address (0x0 to 0xF)
output reg [31:0] data // 32-bit data output
);
endmodule
// ====================================================================
// Alternative ROM Implementation using Case Statement
// ====================================================================
module rom_32bit_case (
input wire clk,
input wire chipselect,
input wire [3:0] address,
output reg [31:0] data
);
endmodule
// ====================================================================
// Comprehensive Testbench for ROM
// ====================================================================
`timescale 1ns/1ps
module tb_rom_32bit;
// Test signals
reg clk;
reg chipselect;
reg [3:0] address;
wire [31:0] data_array;
wire [31:0] data_case;
// Test variables
integer i;
integer error_count;
rom_32bit_case rom_case (
.clk(clk),
.chipselect(chipselect),
.address(address),
.data(data_case)
);
// Clock generation - 10ns period
initial begin
clk = 0;
forever #5 clk = ~clk;
end
$display("====================================================================");
$display("32-bit ROM Testbench - ModelSim Simulation");
$display("Testing both Array and Case implementations");
$display("====================================================================");
chipselect = 1;
#5; // Small delay
// Check results
if (data_array == expected_data[i] && data_case == expected_data[i]) begin
$display("0x%X | 0x%08X | 0x%08X | 0x%08X | PASS",
address, expected_data[i], data_array, data_case);
end else begin
$display("0x%X | 0x%08X | 0x%08X | 0x%08X | FAIL",
address, expected_data[i], data_array, data_case);
error_count = error_count + 1;
end
end
chipselect = 1;
#10;
$display("CS=1, Addr=0x5 | Data: 0x%08X", data_array);
chipselect = 0;
#10;
$display("CS=0, Addr=0x5 | Data: 0x%08X", data_array);
chipselect = 1;
#10;
$display("CS=1, Addr=0x5 | Data: 0x%08X", data_array);
// Summary
$display("\
n====================================================================");
$display("Test Summary:");
$display("Total Errors: %0d", error_count);
if (error_count == 0) begin
$display("*** ALL TESTS PASSED ***");
end else begin
$display("*** %0d TESTS FAILED ***", error_count);
end
$display("====================================================================");
#50;
$finish;
end
endmodule
// ====================================================================
// Simple ROM Usage Example
// ====================================================================
module rom_usage_example;
initial begin
clk = 0;
forever #5 clk = ~clk;
end
initial begin
cs = 0; addr = 0;
#10;
cs = 1;
addr = 4'hA; // Read address 0xA
#10;
$display("Reading address 0xA: Data = 0x%08X", data);
#20;
$finish;
end
endmodule