Files
Basys3RiscV/rtl/uart.v
T
brad ArantandClaude Opus 5.5 5930b958b4 Basys 3 RISC-V computer
The Basys 3 Z-80 computer with the Z-80 replaced by an RV32IM processor
written from scratch (rtl/rv32_cpu.v): multi-cycle, 90 MHz, CSRs and
traps, interrupts, two hardware triggers and a resumable NMI for the
monitor. Passes riscv-tests rv32ui/um/mi.

rv32_system.v keeps all the Z-80 machine's peripherals, with I/O port N
at FFFFFF00h + N, 80 KB of RAM with BASIC preloaded, and a 32 KB monitor
ROM. BASIC, the five games and the monitor (with an RV32 disassembler,
assembler, breakpoints and single step) are ported to RISC-V GCC. Python
emulator and tests in tools/; full-system simulation in sim/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 18:09:18 -08:00

107 lines
3.0 KiB
Verilog

`timescale 1ns / 1ps
// 8N1 UART receiver and transmitter. div = clock frequency / baud rate.
module uart_rx (
input clk,
input rst,
input [13:0] div,
input rx,
output reg [7:0] data,
output reg valid
);
localparam IDLE = 3'd0, START = 3'd1, DATA = 3'd2, STOP = 3'd3, BREAK = 3'd4;
reg [2:0] sync = 3'b111;
reg [2:0] st;
reg [13:0] cnt;
reg [2:0] bitn;
reg [7:0] sh;
wire rxs = sync[2];
always @(posedge clk) sync <= {sync[1:0], rx};
always @(posedge clk) begin
valid <= 1'b0;
if (rst) begin
st <= IDLE;
end else case (st)
IDLE:
if (!rxs) begin
st <= START;
cnt <= {1'b0, div[13:1]}; // sample mid-bit
end
START:
if (cnt == 0) begin
if (!rxs) begin
st <= DATA;
cnt <= div - 1'b1;
bitn <= 3'd0;
end else
st <= IDLE; // glitch, not a start bit
end else
cnt <= cnt - 1'b1;
DATA:
if (cnt == 0) begin
sh <= {rxs, sh[7:1]};
cnt <= div - 1'b1;
bitn <= bitn + 1'b1;
if (bitn == 3'd7) st <= STOP;
end else
cnt <= cnt - 1'b1;
STOP:
if (cnt == 0) begin
if (rxs) begin
data <= sh;
valid <= 1'b1;
st <= IDLE;
end else
st <= BREAK; // bad stop bit: drop it
end else
cnt <= cnt - 1'b1;
BREAK:
if (rxs) st <= IDLE; // wait for the line to go idle
default:
st <= IDLE;
endcase
end
endmodule
module uart_tx (
input clk,
input rst,
input [13:0] div,
input [7:0] data,
input start,
output reg tx = 1'b1,
output reg busy
);
reg [9:0] sh;
reg [3:0] n;
reg [13:0] cnt;
always @(posedge clk) begin
if (rst) begin
tx <= 1'b1;
busy <= 1'b0;
end else if (!busy) begin
if (start) begin
sh <= {1'b1, data, 1'b0};
n <= 4'd0;
cnt <= 14'd0;
busy <= 1'b1;
end
end else if (cnt == 0) begin
if (n == 4'd10)
busy <= 1'b0;
else begin
tx <= sh[0];
sh <= {1'b1, sh[9:1]};
n <= n + 1'b1;
cnt <= div - 1'b1;
end
end else
cnt <= cnt - 1'b1;
end
endmodule