Skip to content

Commit

Permalink
feat: validate signals
Browse files Browse the repository at this point in the history
  • Loading branch information
Elizabeth-0 committed Jan 19, 2025
1 parent 5064aed commit 8ce50da
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 114 deletions.
4 changes: 4 additions & 0 deletions src/tt_um_waves.v
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ module tt_um_waves (
assign uio_out[5] = ena; // Enable signal
assign uio_out[6] = tri_wave_out[0]; // Example wave output

assign uo_out[2:0] = wave_select; // Map wave_select to uo_out[2:0]
assign uo_out[3] = white_noise_en; // Map white_noise_en to uo_out[3]
assign uo_out[7:4] = freq_select[3:0]; // Map lower 4 bits of freq_select to uo_out[7:4]


endmodule

Expand Down
85 changes: 39 additions & 46 deletions test/tb.v
Original file line number Diff line number Diff line change
@@ -1,70 +1,63 @@
`timescale 1ns/1ps

module tb_uart_wave;

reg clk; // Clock signal
reg rst_n; // Reset signal (active low)
reg rx; // UART RX signal
wire [2:0] wave_select; // Selected wave
wire white_noise_en; // White noise enable

// Instantiate the UART receiver module
uart_receiver uut (
module tb;
reg clk;
reg rst_n;
reg rx;
wire [7:0] uo_out;

// Instancia del diseño
tt_um_waves uut (
.clk(clk),
.rst_n(rst_n),
.rx(rx),
.freq_select(), // Not used for this test
.wave_select(wave_select),
.white_noise_en(white_noise_en)
.uo_out(uo_out)
);

// Clock generation
initial clk = 0;
always #20 clk = ~clk; // 25 MHz clock (40 ns period)
// Generación de reloj de 25 MHz
initial begin
clk = 0;
forever #20 clk = ~clk; // 40 ns periodo = 25 MHz
end

// UART transmission task
task send_uart_byte(input [7:0] data);
// Tarea para enviar un byte por UART
task send_uart_byte;
input [7:0] data;
integer i;
begin
rx = 0; // Start bit
#8680; // 115200 baud rate = ~8680 ns per bit
// Start bit
rx = 1'b0;
#8680; // BIT_PERIOD

// Data bits
for (i = 0; i < 8; i = i + 1) begin
rx = data[i];
#8680; // Data bits
#8680; // BIT_PERIOD
end
rx = 1; // Stop bit
#8680;

// Stop bit
rx = 1'b1;
#8680; // BIT_PERIOD
end
endtask

// Testbench sequence
// Secuencia de prueba
initial begin
// Initialize signals
rx = 1;
// Reset
rst_n = 0;

// Apply reset
rx = 1;
#100;
rst_n = 1;

// Send 'T' for Triangle wave
#1000;
send_uart_byte(8'h54);

// Wait and check the output
#10000;
if (wave_select !== 3'b000) $display("Test failed: Expected 3'b000, got %b", wave_select);
else $display("Test passed: Triangle wave selected.");

// Send 'S' for Sawtooth wave
send_uart_byte(8'h53);
// Enviar 'T' para onda triangular
send_uart_byte(8'h54); // ASCII 'T'
#20000; // Esperar a que se procese
$display("uo_out: %b", uo_out); // wave_select debería ser 000 (onda triangular)

// Wait and check the output
#10000;
if (wave_select !== 3'b001) $display("Test failed: Expected 3'b001, got %b", wave_select);
else $display("Test passed: Sawtooth wave selected.");
// Enviar 'N' para habilitar ruido blanco
send_uart_byte(8'h4E); // ASCII 'N'
#20000; // Esperar a que se procese
$display("uo_out: %b", uo_out); // white_noise_en debería ser 1

// End simulation
// Fin de simulación
$finish;
end
endmodule
102 changes: 34 additions & 68 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,50 @@

import cocotb
from cocotb.clock import Clock
from cocotb.triggers import RisingEdge, Timer

# UART Baud Rate: 115200 bits/s => Bit period ~ 8680 ns
BAUD_RATE = 115200
BIT_PERIOD = int(1e9 / BAUD_RATE) # in nanoseconds


async def send_uart_byte(dut, data):
"""
Simulate sending a UART byte to the RX line.
- Start bit: 0
- Data bits: 8 bits
- Stop bit: 1
"""
dut.rx.value = 0 # Start bit
await Timer(BIT_PERIOD, units="ns")

# Send data bits
for i in range(8):
dut.rx.value = (data >> i) & 1
await Timer(BIT_PERIOD, units="ns")

dut.rx.value = 1 # Stop bit
await Timer(BIT_PERIOD, units="ns")
from cocotb.triggers import ClockCycles, Timer

BIT_PERIOD = 8680 # ns (para 115200 baudios)

@cocotb.test()
async def test_uart_wave_selection(dut):
"""
Test UART functionality to ensure proper wave selection.
"""
# Start the clock (25 MHz -> 40 ns period)
clock = Clock(dut.clk, 40, units="ns")
async def test_uart(dut):
# Configuración inicial: reloj de 25 MHz
clock = Clock(dut.clk, 40, units="ns") # 40 ns periodo = 25 MHz
cocotb.start_soon(clock.start())

# Reset the design
# Reset
dut.rst_n.value = 0
dut.rx.value = 1 # Idle state for UART
await Timer(100, units="ns") # Wait for 100 ns
await ClockCycles(dut.clk, 10)
dut.rst_n.value = 1
await Timer(100, units="ns") # Wait for the reset to complete

# Send 'T' for Triangle wave (ASCII 0x54)
await send_uart_byte(dut, 0x54)
await Timer(10 * BIT_PERIOD, units="ns") # Wait for processing

# Check wave_select output
assert dut.wave_select.value == 0b000, f"Failed: Expected wave_select=000, got {dut.wave_select.value}"
dut._log.info("Triangle wave selected successfully!")
# Enviar 'T' (onda triangular)
await send_uart_byte(dut, 0x54) # ASCII 'T'
await ClockCycles(dut.clk, 100)
wave_select = dut.uo_out.value & 0x07 # Bits 2:0 de uo_out
assert wave_select == 0, f"wave_select incorrecto: {wave_select}"

# Send 'S' for Sawtooth wave (ASCII 0x53)
await send_uart_byte(dut, 0x53)
await Timer(10 * BIT_PERIOD, units="ns") # Wait for processing
# Enviar 'N' (habilitar ruido blanco)
await send_uart_byte(dut, 0x4E) # ASCII 'N'
await ClockCycles(dut.clk, 100)
white_noise_en = (dut.uo_out.value >> 3) & 0x01 # Bit 3 de uo_out
assert white_noise_en == 1, f"white_noise_en incorrecto: {white_noise_en}"

# Check wave_select output
assert dut.wave_select.value == 0b001, f"Failed: Expected wave_select=001, got {dut.wave_select.value}"
dut._log.info("Sawtooth wave selected successfully!")
# Enviar 'F' (deshabilitar ruido blanco)
await send_uart_byte(dut, 0x46) # ASCII 'F'
await ClockCycles(dut.clk, 100)
white_noise_en = (dut.uo_out.value >> 3) & 0x01 # Bit 3 de uo_out
assert white_noise_en == 0, f"white_noise_en incorrecto: {white_noise_en}"

# Send 'W' for Sine wave (ASCII 0x57)
await send_uart_byte(dut, 0x57)
await Timer(10 * BIT_PERIOD, units="ns") # Wait for processing

# Check wave_select output
assert dut.wave_select.value == 0b011, f"Failed: Expected wave_select=011, got {dut.wave_select.value}"
dut._log.info("Sine wave selected successfully!")

# Send 'N' to enable white noise (ASCII 0x4E)
await send_uart_byte(dut, 0x4E)
await Timer(10 * BIT_PERIOD, units="ns") # Wait for processing

# Check white_noise_en output
assert dut.white_noise_en.value == 1, f"Failed: Expected white_noise_en=1, got {dut.white_noise_en.value}"
dut._log.info("White noise enabled successfully!")
async def send_uart_byte(dut, data):
"""Envía un byte al UART"""
# Start bit
dut.rx.value = 0
await Timer(BIT_PERIOD, units="ns")

# Send 'F' to disable white noise (ASCII 0x46)
await send_uart_byte(dut, 0x46)
await Timer(10 * BIT_PERIOD, units="ns") # Wait for processing
# Data bits
for i in range(8):
dut.rx.value = (data >> i) & 1
await Timer(BIT_PERIOD, units="ns")

# Check white_noise_en output
assert dut.white_noise_en.value == 0, f"Failed: Expected white_noise_en=0, got {dut.white_noise_en.value}"
dut._log.info("White noise disabled successfully!")
# Stop bit
dut.rx.value = 1
await Timer(BIT_PERIOD, units="ns")

0 comments on commit 8ce50da

Please sign in to comment.