|
| 1 | +`timescale 100ns/1ns |
| 2 | +`default_nettype none |
| 3 | + |
| 4 | +`include "Uart8.v" |
| 5 | + |
| 6 | +module test; |
| 7 | + |
| 8 | +localparam CLOCK_FREQ = 12000000; // Alhambra board |
| 9 | +localparam SIM_STEP_FREQ = 1 / 0.0000001 / 2; // this sim timescale 100ns |
| 10 | + |
| 11 | +// for the simulation timeline: |
| 12 | +// ratio SIM_STEP_FREQ MHz / CLOCK_FREQ MHz gives the output waveform in proper time |
| 13 | +// (*but note all clocks and the timeline are approximate due to rounding) |
| 14 | +localparam SIM_TIMESTEP_FACTOR = SIM_STEP_FREQ / CLOCK_FREQ; |
| 15 | + |
| 16 | +reg clk; |
| 17 | +reg en_1; |
| 18 | +reg rx; |
| 19 | +wire rxBusy_2; |
| 20 | +wire rxDone_2; |
| 21 | +wire rxErr_2; |
| 22 | +reg [7:0] txByte_1; |
| 23 | +wire [7:0] rxByte_2; |
| 24 | + |
| 25 | +Uart8 #(.CLOCK_RATE(CLOCK_FREQ)) uart( |
| 26 | + .clk(clk), |
| 27 | + |
| 28 | + // rx interface |
| 29 | + .rxEn(en_1), |
| 30 | + .rx(rx), |
| 31 | + .rxBusy(rxBusy_2), |
| 32 | + .rxDone(rxDone_2), |
| 33 | + .rxErr(rxErr_2), |
| 34 | + .out(rxByte_2) |
| 35 | + |
| 36 | + // tx interface (unused) |
| 37 | +); |
| 38 | + |
| 39 | +initial clk = 1'b0; |
| 40 | + |
| 41 | +always #SIM_TIMESTEP_FACTOR clk = ~clk; |
| 42 | + |
| 43 | +initial begin |
| 44 | + $dumpfile(`DUMP_FILE_NAME); |
| 45 | + $dumpvars(0, test); |
| 46 | + |
| 47 | +// #65 == 1 rx clock period (approximately) at 9600 baud |
| 48 | +#240 |
| 49 | + en_1 = 1'b1; |
| 50 | + txByte_1 = 8'b11010110; |
| 51 | + rx = 1'b0; |
| 52 | + |
| 53 | + $display(" tx data: %8b", txByte_1); |
| 54 | +#160 |
| 55 | + rx = 1'b1; |
| 56 | +#360 |
| 57 | + rx = 1'b0; |
| 58 | +#1075 // instead of #1042, this makes transmit clock sync with receive clock (a 3% difference) |
| 59 | + rx = txByte_1[0]; |
| 60 | + |
| 61 | + $display("%7.2fms | rx first bit: %1b", $realtime/10000, rx); |
| 62 | +#1075 |
| 63 | + rx = txByte_1[1]; |
| 64 | + |
| 65 | + $display("%7.2fms | rx next bit: %1b", $realtime/10000, rx); |
| 66 | +#1075 |
| 67 | + rx = txByte_1[2]; |
| 68 | + |
| 69 | + $display("%7.2fms | rx next bit: %1b", $realtime/10000, rx); |
| 70 | +#1075 |
| 71 | + rx = txByte_1[3]; |
| 72 | + |
| 73 | + $display("%7.2fms | rx next bit: %1b", $realtime/10000, rx); |
| 74 | +#1075 |
| 75 | + rx = txByte_1[4]; |
| 76 | + |
| 77 | + $display("%7.2fms | rx next bit: %1b", $realtime/10000, rx); |
| 78 | +#1075 |
| 79 | + rx = txByte_1[5]; |
| 80 | + |
| 81 | + $display("%7.2fms | rx next bit: %1b", $realtime/10000, rx); |
| 82 | +#1075 |
| 83 | + rx = txByte_1[6]; |
| 84 | + |
| 85 | + $display("%7.2fms | rx next bit: %1b", $realtime/10000, rx); |
| 86 | +#1075 |
| 87 | + rx = txByte_1[7]; |
| 88 | + |
| 89 | + $display("%7.2fms | rx last bit: %1b", $realtime/10000, rx); |
| 90 | +#530 |
| 91 | + rx = 1'b1; |
| 92 | +#530 |
| 93 | + rx = 1'b0; |
| 94 | + |
| 95 | + $display("%7.4fms | end of stop bit | start of start bit: %1b", $realtime/10000, rx); |
| 96 | +#530 |
| 97 | + rx = 1'b1; |
| 98 | + |
| 99 | + $display("%7.4fms | rx start bit glitch: %1b", $realtime/10000, rx); |
| 100 | +#545 |
| 101 | + rx = txByte_1[0]; |
| 102 | + |
| 103 | + $display("%7.2fms | rx first bit: %1b", $realtime/10000, rx); |
| 104 | +#1075 |
| 105 | + rx = txByte_1[1]; |
| 106 | + |
| 107 | + $display("%7.2fms | rx next bit: %1b", $realtime/10000, rx); |
| 108 | +#1075 |
| 109 | + rx = txByte_1[2]; |
| 110 | +#1120 |
| 111 | + |
| 112 | + $finish(); |
| 113 | +end |
| 114 | + |
| 115 | +endmodule |
0 commit comments