From c6db6ea23a0e51535f3239dc9ab7c29b128f91ef Mon Sep 17 00:00:00 2001 From: shrutikbalwan Date: Sat, 7 Nov 2026 23:24:10 +0530 Subject: [PATCH] Implement Wishbone memory and peripheral subsystems --- Makefile | 16 ++ docs/ARCHITECTURE.md | 6 +- docs/BUS_SPECIFICATION.md | 18 +- docs/IMPLEMENTATION_ROADMAP.md | 2 +- docs/MEMORY_MAP.md | 30 +-- docs/MEMORY_SUBSYSTEM.md | 24 +++ docs/PERIPHERAL_SUBSYSTEM.md | 122 +++++++++++ docs/RESET_AND_CLOCK_SPEC.md | 4 +- hw/rtl/bus/wb_address_decoder.sv | 22 ++ hw/rtl/bus/wb_arbiter.sv | 83 ++++++++ hw/rtl/bus/wb_cpu_adapter.sv | 66 ++++++ hw/rtl/bus/wb_error_slave.sv | 23 +++ hw/rtl/bus/wb_interconnect.sv | 84 ++++++++ hw/rtl/cpu/cpu_core.sv | 8 +- hw/rtl/memory/boot_rom.sv | 53 +++++ hw/rtl/memory/memory_subsystem.sv | 36 ++++ hw/rtl/memory/wb_sram.sv | 42 ++++ hw/rtl/peripherals/interrupt_controller.sv | 85 ++++++++ hw/rtl/peripherals/peripheral_subsystem.sv | 89 ++++++++ hw/rtl/peripherals/wb_debug_perf.sv | 55 +++++ hw/rtl/peripherals/wb_gpio.sv | 61 ++++++ hw/rtl/peripherals/wb_i2c.sv | 91 +++++++++ hw/rtl/peripherals/wb_power_ctrl.sv | 59 ++++++ hw/rtl/peripherals/wb_spi.sv | 70 +++++++ hw/rtl/peripherals/wb_timer.sv | 58 ++++++ hw/rtl/peripherals/wb_uart.sv | 92 +++++++++ hw/rtl/peripherals/wb_watchdog.sv | 51 +++++ hw/tb/boot_image.hex | 15 ++ hw/tb/cpu_tb.sv | 2 +- hw/tb/memory_subsystem_tb.sv | 87 ++++++++ hw/tb/peripheral_cpu_irq_tb.sv | 64 ++++++ hw/tb/peripheral_irq_boot.hex | 42 ++++ hw/tb/peripheral_subsystem_tb.sv | 224 +++++++++++++++++++++ hw/tb/wb_cpu_adapter_tb.sv | 54 +++++ hw/tb/wb_interconnect_tb.sv | 164 +++++++++++++++ scripts/synth.ys | 21 ++ verification/tb/cpu_tb.sv | 2 +- 37 files changed, 1994 insertions(+), 31 deletions(-) create mode 100644 docs/MEMORY_SUBSYSTEM.md create mode 100644 docs/PERIPHERAL_SUBSYSTEM.md create mode 100644 hw/rtl/bus/wb_address_decoder.sv create mode 100644 hw/rtl/bus/wb_arbiter.sv create mode 100644 hw/rtl/bus/wb_cpu_adapter.sv create mode 100644 hw/rtl/bus/wb_error_slave.sv create mode 100644 hw/rtl/bus/wb_interconnect.sv create mode 100644 hw/rtl/memory/boot_rom.sv create mode 100644 hw/rtl/memory/memory_subsystem.sv create mode 100644 hw/rtl/memory/wb_sram.sv create mode 100644 hw/rtl/peripherals/interrupt_controller.sv create mode 100644 hw/rtl/peripherals/peripheral_subsystem.sv create mode 100644 hw/rtl/peripherals/wb_debug_perf.sv create mode 100644 hw/rtl/peripherals/wb_gpio.sv create mode 100644 hw/rtl/peripherals/wb_i2c.sv create mode 100644 hw/rtl/peripherals/wb_power_ctrl.sv create mode 100644 hw/rtl/peripherals/wb_spi.sv create mode 100644 hw/rtl/peripherals/wb_timer.sv create mode 100644 hw/rtl/peripherals/wb_uart.sv create mode 100644 hw/rtl/peripherals/wb_watchdog.sv create mode 100644 hw/tb/boot_image.hex create mode 100644 hw/tb/memory_subsystem_tb.sv create mode 100644 hw/tb/peripheral_cpu_irq_tb.sv create mode 100644 hw/tb/peripheral_irq_boot.hex create mode 100644 hw/tb/peripheral_subsystem_tb.sv create mode 100644 hw/tb/wb_cpu_adapter_tb.sv create mode 100644 hw/tb/wb_interconnect_tb.sv diff --git a/Makefile b/Makefile index 930f75f..f5b569a 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,8 @@ VERILATOR ?= verilator BUILD_DIR ?= build CPU_RTL = hw/rtl/cpu/alu.sv hw/rtl/cpu/regfile.sv hw/rtl/cpu/rvc_decompress.sv hw/rtl/cpu/cpu_core.sv +BUS_RTL = hw/rtl/bus/wb_error_slave.sv hw/rtl/bus/wb_address_decoder.sv hw/rtl/bus/wb_arbiter.sv hw/rtl/bus/wb_interconnect.sv hw/rtl/bus/wb_cpu_adapter.sv +PERIPHERAL_RTL = hw/rtl/peripherals/wb_uart.sv hw/rtl/peripherals/wb_gpio.sv hw/rtl/peripherals/wb_spi.sv hw/rtl/peripherals/wb_i2c.sv hw/rtl/peripherals/wb_timer.sv hw/rtl/peripherals/wb_watchdog.sv hw/rtl/peripherals/interrupt_controller.sv hw/rtl/peripherals/wb_debug_perf.sv hw/rtl/peripherals/wb_power_ctrl.sv hw/rtl/peripherals/peripheral_subsystem.sv .PHONY: all sim lint synth test-cocotb clean @@ -20,10 +22,24 @@ sim: | $(BUILD_DIR) $(VVP) $(BUILD_DIR)/cpu_tb $(IVERILOG) -g2012 -Wall -s cpu_tb -o $(BUILD_DIR)/cpu_hex_tb $(CPU_RTL) hw/tb/cpu_tb.sv $(VVP) $(BUILD_DIR)/cpu_hex_tb + $(IVERILOG) -g2012 -Wall -s wb_interconnect_tb -o $(BUILD_DIR)/wb_interconnect_tb $(BUS_RTL) hw/tb/wb_interconnect_tb.sv + $(VVP) $(BUILD_DIR)/wb_interconnect_tb + $(IVERILOG) -g2012 -Wall -s wb_cpu_adapter_tb -o $(BUILD_DIR)/wb_cpu_adapter_tb $(BUS_RTL) hw/tb/wb_cpu_adapter_tb.sv + $(VVP) $(BUILD_DIR)/wb_cpu_adapter_tb + $(IVERILOG) -g2012 -Wall -s memory_subsystem_tb -o $(BUILD_DIR)/memory_subsystem_tb $(CPU_RTL) hw/rtl/bus/wb_cpu_adapter.sv hw/rtl/memory/wb_sram.sv hw/rtl/memory/boot_rom.sv hw/rtl/memory/memory_subsystem.sv hw/tb/memory_subsystem_tb.sv + $(VVP) $(BUILD_DIR)/memory_subsystem_tb + $(IVERILOG) -g2012 -Wall -s peripheral_subsystem_tb -o $(BUILD_DIR)/peripheral_subsystem_tb $(PERIPHERAL_RTL) hw/tb/peripheral_subsystem_tb.sv + $(VVP) $(BUILD_DIR)/peripheral_subsystem_tb + $(IVERILOG) -g2012 -Wall -s peripheral_cpu_irq_tb -o $(BUILD_DIR)/peripheral_cpu_irq_tb $(CPU_RTL) hw/rtl/bus/wb_cpu_adapter.sv hw/rtl/memory/wb_sram.sv hw/rtl/memory/boot_rom.sv hw/rtl/memory/memory_subsystem.sv $(PERIPHERAL_RTL) hw/tb/peripheral_cpu_irq_tb.sv + $(VVP) $(BUILD_DIR)/peripheral_cpu_irq_tb lint: $(VERILATOR) --lint-only --Wall -Wno-fatal --top-module counter hw/rtl/counter.sv $(VERILATOR) --lint-only --Wall -Wno-fatal --top-module cpu_core $(CPU_RTL) + $(VERILATOR) --lint-only --Wall -Wno-fatal --top-module wb_interconnect $(BUS_RTL) + $(VERILATOR) --lint-only --Wall -Wno-fatal --top-module wb_cpu_adapter $(BUS_RTL) + $(VERILATOR) --lint-only --Wall -Wno-fatal --top-module memory_subsystem hw/rtl/memory/wb_sram.sv hw/rtl/memory/boot_rom.sv hw/rtl/memory/memory_subsystem.sv + $(VERILATOR) --lint-only --Wall -Wno-fatal --top-module peripheral_subsystem $(PERIPHERAL_RTL) synth: | $(BUILD_DIR) $(YOSYS) -s scripts/synth.ys diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index d39ab03..4650c29 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -2,7 +2,7 @@ ## Status and design decisions -This document describes the target SoC architecture. The Phase 02 RV32I core and counter are implemented; the remaining diagram blocks are architectural targets and must not be inferred as implemented. Decisions marked **provisional** need review before their implementation phases. +This document describes the target SoC architecture. The CPU, counter, Wishbone fabric, Phase 05 behavioral memory, and initial Phase 06 UART/GPIO/SPI/I2C/timer/watchdog/interrupt/debug/performance/power-request collection are implemented. Other diagram blocks remain architectural targets and must not be inferred as implemented. Decisions marked **provisional** need review before their implementation phases. ## Block diagram @@ -18,7 +18,7 @@ This document describes the target SoC architecture. The Phase 02 RV32I core and debug, counters, power control ``` -The diagram indicates intended connectivity only. The final master topology and whether DMA has two independent masters or one shared master are review decisions. +The diagram indicates intended connectivity except for implemented blocks documented in the corresponding subsystem specifications. The final master topology and whether DMA has two independent masters or one shared master are review decisions. ## CPU @@ -31,7 +31,7 @@ The diagram indicates intended connectivity only. The final master topology and **Provisional recommendation:** Wishbone B4 Classic, 32-bit address/data, byte selects, one outstanding transfer per granted master. A central decoder maps one selected target per transfer. CPU and DMA access are arbitrated; fairness and maximum wait are specified in the bus contract. The firewall checks DMA address, direction, and transfer size before target selection. PMP checks CPU accesses; bus firewall rules do not replace PMP. -Targets are boot ROM, SRAM, security control/crypto, AI accelerator, and peripheral register banks. ROM and SRAM may be integrated into a shared target or separate targets, but each must preserve the memory map and fault behavior. +Targets are boot ROM, SRAM, security control/crypto, AI accelerator, and peripheral register banks. The current [peripheral subsystem](PERIPHERAL_SUBSYSTEM.md) implements UART, GPIO, SPI/I2C masters, machine timer, interrupt controller, watchdog, performance observation counters, and software clock/power requests at their specified Wishbone pages. External debug halt/step, physical clock gating, and other peripheral targets remain future work. ROM and SRAM may be integrated into a shared target or separate targets, but each must preserve the memory map and fault behavior. ## Memory and protection diff --git a/docs/BUS_SPECIFICATION.md b/docs/BUS_SPECIFICATION.md index 2943742..4de0598 100644 --- a/docs/BUS_SPECIFICATION.md +++ b/docs/BUS_SPECIFICATION.md @@ -1,10 +1,10 @@ # Wishbone-B4 Bus Specification -This is the proposed SoC interface contract. No Wishbone interface exists in the current RTL. +This specification defines the implemented Phase 04 subsystem. The RTL implements the classic, single-outstanding subset described below. It does not implement pipelined B4 (`CTI`/`BTE`) or `RTY`. ## Selected profile -**Provisional:** Wishbone B4 Classic, synchronous, single 32-bit address and 32-bit data path, byte addressing, little-endian lane order, and at most one outstanding transfer per master. `CYC`, `STB`, `WE`, `ADR`, `DAT`, and `SEL` are used; `ACK`, `ERR`, and optionally `RTY` are slave responses. `CTI`/`BTE` and pipelined multiple outstanding transfers are not used in the initial profile. Whether retry is supported remains a decision; the recommended initial contract is no `RTY` and bounded `ACK` or `ERR`. +**Implemented:** Wishbone B4 Classic, synchronous, 32-bit byte address and data, little-endian lanes, one global outstanding transfer. Signals are `CYC`, `STB`, `WE`, `ADR`, `DAT_W`, and `SEL` from master; slave returns `DAT_R`, `ACK`, or `ERR`. `CTI`, `BTE`, and `RTY` are absent. ## Transfer rules @@ -12,18 +12,22 @@ This is the proposed SoC interface contract. No Wishbone interface exists in the - Exactly one of `ACK` or `ERR` terminates a transfer. They must never be asserted together. A slave must not produce a response without a selected request. - `SEL[n]` selects byte lane `n`; `SEL=0000` is invalid and returns `ERR`. Byte lane 0 corresponds to the least-significant byte at the lowest address. - On a successful write, only selected byte lanes change. A read returns the 32-bit aligned word; CPU load extraction/sign extension is performed by the CPU or its data path. -- A transfer may not be silently split by a slave. Any supported unaligned access policy must be handled above the slave and documented. The initial recommendation is to return an access error for unsupported unaligned word transfers. -- Address decode selects no more than one slave. Unmapped and prohibited transactions return `ERR` within a bounded number of clocks. +- All accesses are 32-bit words; `ADR[1:0]` are preserved for target decode but CPU adapter aligns data addresses to word boundaries. CPU byte lanes are carried in `SEL`; lane 0 is the least-significant byte. `SEL=0` returns `ERR`. +- The CPU adapter holds its request fields until termination, sends `SEL=1111` for reads, preserves write byte strobes, and maps Wishbone `ERR` to the CPU ready/error response. +- Address decode selects exactly one of ROM (`0x0000_0000`–`0x0000_0FFF`), SRAM (`0x1000_0000`–`0x1000_7FFF`) or peripheral window (`0x4000_0000`–`0x4000_FFFF`). Other addresses return `ERR`. The Phase 06 `peripheral_subsystem` refines its window to implemented UART, GPIO, timer, interrupt-controller, and watchdog pages; unimplemented pages return `ERR`. +- The arbiter is round-robin between CPU master 0 and DMA master 1. Only one master is forwarded at once. Grant persists until termination, master withdrawal, reset, or timeout. A response is delivered only to the owner. After completion, priority rotates to the other master. +- Default timeout is 16 clocks from grant. If no `ACK`/`ERR` arrives, the owner receives a one-cycle `ERR`; the target request is withdrawn. Reset asynchronously clears arbitration state and error response state, aborting the transaction without a late acknowledgement. +- The unmapped/invalid-select error target returns `ERR` on the cycle after request recognition. `ACK` and `ERR` are mutually exclusive. No response is valid without a request owned by the arbiter. - Reset aborts in-flight requests and clears response outputs. Masters restart only after reset release. No stale `ACK` may terminate a post-reset request. ## Arbitration and protection -CPU and DMA requests are arbitrated. The proposed arbiter is round-robin with bounded service when all requests target responsive slaves. Locking/atomic bus cycles are out of scope while the A extension is excluded. CPU accesses are checked by PMP. DMA accesses are checked by a firewall using programmed allowed ranges and read/write permissions; default policy is deny until configured. +CPU and DMA requests are arbitrated round-robin. Locking/atomic bus cycles are out of scope while the A extension is excluded. PMP and the DMA firewall are not yet connected to this interconnect. ## CDC and timing All initial bus participants share a synchronous clock. A future asynchronous peripheral domain must cross through a reviewed CDC bridge; direct unsynchronized Wishbone signals are prohibited. Response latency and timeout thresholds must be parameters with a documented default before integration. -## Verification requirements +## Verification and formal properties -Check stable request fields under stall, one-hot decode, no response without request, mutually exclusive termination, reset abort, byte select behavior, unmapped error, protection denial, and bounded completion. Add assertions for these protocol properties and randomized backpressure tests before bus integration is accepted. +The bus testbench checks single-master transfers, simultaneous requests, contention, delayed/backpressured targets, byte enables, decode boundaries, unmapped and empty-select errors, reset abort, ordering, and timeout. `FORMAL` assertions in arbiter/adapter/interconnect check owner routing, stable stalled fields, one-hot target selection, and termination exclusivity. These assertions have not yet been run in a formal engine. The current testbench uses independent target state machines and independent master drivers; additional randomized seeds and formal proof remain future verification work. diff --git a/docs/IMPLEMENTATION_ROADMAP.md b/docs/IMPLEMENTATION_ROADMAP.md index 8f7632b..d780157 100644 --- a/docs/IMPLEMENTATION_ROADMAP.md +++ b/docs/IMPLEMENTATION_ROADMAP.md @@ -10,7 +10,7 @@ Phases are gated in order. A specification entry is not an implementation claim. | 03 — CPU verification closure | Reference-model and instruction-family verification, assertions and coverage | Required supported-profile tests pass; unsupported encodings trap; coverage gaps documented and accepted | | 04 — Bus and memory | Wishbone fabric, arbitration, ROM, 32 KiB SRAM controller, timeout/error behavior | Protocol assertions, contention/backpressure, address boundary, ROM-write and unmapped-error tests pass | | 05 — Protection and interrupts | PMP, DMA firewall, interrupt/timer controller, watchdog | Access-control properties and negative tests pass; reset/interrupt behavior reviewed | -| 06 — Peripherals and debug | UART, GPIO, SPI, I2C, debug/performance counters, clock/power control | Register-level tests, interrupt tests, lint/synthesis and clock/reset review pass | +| 06 — Peripherals and debug | UART, GPIO, SPI, I2C, debug/performance counters, clock/power control | Initial register blocks and CPU-level integration tests pass. Lint/synthesis and physical clock/reset review remain build-flow/integration gates; physical gating and halt/step transport are explicitly outside the initial register-block scope. | | 07 — Security subsystem | AES-128, SHA-256, TRNG interface, key vault, secure boot policy | Known-answer and negative tests, lifecycle tests, threat review; no security claims beyond evidence | | 08 — AI and DMA | INT8/INT4 accelerator and two DMA channels | Arithmetic corner cases, overflow/saturation, alignment, firewall and concurrency tests pass | | 09 — SoC integration | Integrate verified modules, firmware, TinyML runtime, boot image | End-to-end simulation and software tests reproducible; all address/interrupt/reset contracts consistent | diff --git a/docs/MEMORY_MAP.md b/docs/MEMORY_MAP.md index da6e297..cb50176 100644 --- a/docs/MEMORY_MAP.md +++ b/docs/MEMORY_MAP.md @@ -1,33 +1,33 @@ # SEAL-RV 2.0 Memory Map -All addresses are byte addresses. Windows below are proposed and reserved for the architecture; they are not implemented in the current repository. The interconnect must reject accesses outside a target's implemented registers and must not mirror addresses unless a later revision explicitly defines aliases. +All addresses are byte addresses. Boot ROM and main SRAM are implemented by Phase 05. UART, GPIO, SPI, I2C, machine timer, external interrupt controller, watchdog, debug/performance counters, and power/clock requests are implemented by the Phase 06 peripheral subsystem. Other peripheral windows remain reserved and return an error. The interconnect must reject accesses outside a target's implemented range and must not mirror addresses unless a later revision explicitly defines aliases. | Region | Base | Limit (inclusive) | Size | Access and purpose | |---|---:|---:|---:|---| -| Boot ROM | `0x0000_0000` | `0x0000_0FFF` | 4 KiB | Read-only instruction/data; reset entry | -| Main SRAM | `0x1000_0000` | `0x1000_7FFF` | 32 KiB | Read/write, SECDED-protected logical data | -| UART | `0x4000_0000` | `0x4000_0FFF` | 4 KiB window | Serial port registers | -| GPIO | `0x4000_1000` | `0x4000_1FFF` | 4 KiB window | GPIO control/data | -| SPI | `0x4000_2000` | `0x4000_2FFF` | 4 KiB window | SPI controller | -| I2C | `0x4000_3000` | `0x4000_3FFF` | 4 KiB window | I2C controller | -| Machine timer | `0x4000_4000` | `0x4000_4FFF` | 4 KiB window | `mtime`, `mtimecmp`, software interrupt | -| External interrupt controller | `0x4000_5000` | `0x4000_5FFF` | 4 KiB window | Pending, enable, priority, claim/complete | +| Boot ROM | `0x0000_0000` | `0x0000_0FFF` | 4 KiB | Read-only instruction/data; reset entry; one-cycle Wishbone read | +| Main SRAM | `0x1000_0000` | `0x1000_7FFF` | 32 KiB | Read/write behavioral SRAM; one-cycle Wishbone response; byte writes | +| UART | `0x4000_0000` | `0x4000_0FFF` | 4 KiB window | 8-N-1 serial; see [peripheral register map](PERIPHERAL_SUBSYSTEM.md) | +| GPIO | `0x4000_1000` | `0x4000_1FFF` | 4 KiB window | Synchronized GPIO and IRQ; see [peripheral register map](PERIPHERAL_SUBSYSTEM.md) | +| SPI | `0x4000_2000` | `0x4000_2FFF` | 4 KiB window | SPI master; see [peripheral register map](PERIPHERAL_SUBSYSTEM.md) | +| I2C | `0x4000_3000` | `0x4000_3FFF` | 4 KiB window | Single-controller open-drain master; see [peripheral register map](PERIPHERAL_SUBSYSTEM.md) | +| Machine timer | `0x4000_4000` | `0x4000_4FFF` | 4 KiB window | 64-bit `mtime`, `mtimecmp`, MTIP; see [peripheral register map](PERIPHERAL_SUBSYSTEM.md) | +| External interrupt controller | `0x4000_5000` | `0x4000_5FFF` | 4 KiB window | UART/GPIO/watchdog pending, masks, priorities, claim/complete; see [peripheral register map](PERIPHERAL_SUBSYSTEM.md) | | DMA | `0x4000_6000` | `0x4000_6FFF` | 4 KiB window | Two channel control/status banks | | AES/SHA engines | `0x4000_7000` | `0x4000_7FFF` | 4 KiB window | Crypto command/status/data | | Key vault | `0x4000_8000` | `0x4000_8FFF` | 4 KiB window | Restricted key lifecycle/control | | AI accelerator | `0x4000_9000` | `0x4000_9FFF` | 4 KiB window | INT8/INT4 command/status/data | -| Debug/performance | `0x4000_A000` | `0x4000_AFFF` | 4 KiB window | Debug and counters | -| Power management | `0x4000_B000` | `0x4000_BFFF` | 4 KiB window | Clock enables, sleep, wake status | +| Debug/performance | `0x4000_A000` | `0x4000_AFFF` | 4 KiB window | Cycle, retire, bus, IRQ counters and last PC; see [peripheral register map](PERIPHERAL_SUBSYSTEM.md) | +| Power management | `0x4000_B000` | `0x4000_BFFF` | 4 KiB window | Clock requests, sleep/wake status, software reset; see [peripheral register map](PERIPHERAL_SUBSYSTEM.md) | | TRNG interface | `0x4000_C000` | `0x4000_CFFF` | 4 KiB window | Entropy data/status/health | -| Watchdog | `0x4000_D000` | `0x4000_DFFF` | 4 KiB window | Watchdog control/status | +| Watchdog | `0x4000_D000` | `0x4000_DFFF` | 4 KiB window | Programmable reset watchdog; see [peripheral register map](PERIPHERAL_SUBSYSTEM.md) | | PMP/firewall control | `0x4000_E000` | `0x4000_EFFF` | 4 KiB window | Locked policy configuration/status | ## Access rules -- Register windows use aligned 32-bit registers at offsets documented by each peripheral specification. The initial bus supports byte enables; each register must define whether partial writes are accepted, merged, or rejected. +- Register windows use aligned 32-bit registers at offsets documented by each peripheral specification. The initial bus supports byte enables; partial-write behavior is defined in [PERIPHERAL_SUBSYSTEM.md](PERIPHERAL_SUBSYSTEM.md). - Boot ROM writes return an error. SRAM accesses outside the 32 KiB range return an error. Unmapped addresses return an error and set an observable fault cause. - Execute permission is denied for MMIO by default. ROM and SRAM execute permissions are governed by CPU privilege/PMP policy. - DMA may access SRAM and explicitly permitted memory windows only. DMA access to ROM, key-vault state, debug controls, or protected peripheral registers is denied by default. -- The memory map is fixed for Phase 02 unless review approves a revision. Address decode must use the complete address so high-address aliases cannot reach a valid target. +- Address decode uses the complete address so high-address aliases cannot reach a valid target. -Per-register offsets, reset values, privilege, and side effects remain open until each peripheral is designed. +DMA, crypto, AI accelerator, TRNG, and PMP/firewall register behavior remains to be implemented. Physical clock gating, debug halt/step, and external debug transport are not part of the initial Phase 06 RTL. diff --git a/docs/MEMORY_SUBSYSTEM.md b/docs/MEMORY_SUBSYSTEM.md new file mode 100644 index 0000000..1dc599a --- /dev/null +++ b/docs/MEMORY_SUBSYSTEM.md @@ -0,0 +1,24 @@ +# SEAL-RV Memory Subsystem + +Phase 05 implements the initial 32-bit little-endian memory system. Addresses are byte addresses. + +| Region | Base | Last byte | Size | Behavior | +|---|---:|---:|---:|---| +| Boot ROM | `0x0000_0000` | `0x0000_0FFF` | 4 KiB | Read-only; CPU reset fetch address | +| Main SRAM | `0x1000_0000` | `0x1000_7FFF` | 32 KiB | Read/write, byte-lane writes | + +Other addresses return Wishbone `ERR`; ROM writes and empty `SEL` return `ERR`. Decode uses the full address and does not mirror. The existing CPU rejects misaligned halfword and word operations before issuing a bus request. The Wishbone port accepts aligned 32-bit words with any nonzero lane mask; stores update the indicated bytes. `DAT_R` is the aligned 32-bit word and CPU load logic selects/sign extends subwords. + +## Timing and reset + +The Wishbone slave samples a request at a rising edge and asserts exactly one of `ACK` or `ERR` during the following cycle, while `CYC` and `STB` remain asserted: one-cycle response latency. Master fields must remain stable until termination. Back-to-back transactions are supported when the master drops and reasserts the request between transfers. Asynchronous active-low reset clears pending responses and aborts in-flight accesses. Memory arrays are not reset. ROM initializes from `hw/tb/boot_image.hex`; its unused words are NOP-filled for simulation. SRAM may load a simulation image through `SRAM_INIT_FILE`, otherwise contents are unspecified until written. The default image path is relative to the simulation working directory. + +The CPU fetch interface predates the Wishbone bus and has no wait signal. `fetch_data` is a combinational 32-bit window with its low halfword at `fetch_addr`, including addresses in the upper half of an aligned word; instruction fetch therefore has zero-cycle combinational latency. Out-of-ROM fetch addresses return a NOP and do not alias ROM. The core's current reset PC is zero. The separate Wishbone ROM port uses one-cycle response latency. + +## Simulation image and smoke program + +`hw/tb/boot_image.hex` begins with compressed NOPs at addresses 0 and 2 to exercise upper-halfword instruction fetch, then sets `sp` inside the SRAM window, pushes and pops 42, writes and reads a separate data word, and exercises byte and halfword stores and loads before looping. `hw/tb/memory_subsystem_tb.sv` runs that image on `cpu_core` through `wb_cpu_adapter`. It also checks first and last valid ROM/SRAM words, the first invalid address after each region, high-address unmapped decode, all byte lanes, byte and halfword read-after-write through the CPU, consecutive Wishbone write cycles, empty selects, ROM write protection, reset cancellation of a pending response, SRAM retention across reset, and absence of stale responses. Run with `make sim` where Icarus Verilog is installed. + +## ASIC macro replacement (SKY130) + +`wb_sram.sv` and `boot_rom.sv` are behavioral simulation models, not ASIC memory implementations. Replace the array storage with technology wrappers instantiated from the selected SKY130-compatible SRAM compiler/macro, keeping wrappers separate from these behavioral modules. The integration must document the chosen macro dimensions, byte-write support or read/modify/write adaptation, read-during-write behavior, port count, clock polarity, output latency, setup/hold and timing constraints, power pins, test/MBIST access, and any required initialization strategy. Adapt the wrapper to preserve the one-cycle Wishbone contract or update the bus/CPU wait-state contract and verification together. ROM contents need a mask-ROM or loaded nonvolatile implementation; the simulation `$readmemh` mechanism does not synthesize as a production ROM. SRAM reset must not imply clearing the physical macro. No ECC is implemented in this initial phase; the prior architectural proposal for SECDED remains future work. diff --git a/docs/PERIPHERAL_SUBSYSTEM.md b/docs/PERIPHERAL_SUBSYSTEM.md new file mode 100644 index 0000000..e58b031 --- /dev/null +++ b/docs/PERIPHERAL_SUBSYSTEM.md @@ -0,0 +1,122 @@ +# SEAL-RV Phase 06 Peripheral Subsystem + +`hw/rtl/peripherals/peripheral_subsystem.sv` is a one-target Wishbone-B4 Classic peripheral block. Connect its `irq_timer` and `irq_external` outputs to `cpu_core.irq_timer` and `cpu_core.irq_external`, and connect the CPU's `pc` and new `retire` pulse to the debug inputs. `reset_request` is active high and sticky until system reset; feed it to a reset controller that latches/qualifies recovery rather than directly gating `rst_n`. The target ACKs valid accesses one clock after acceptance. A zero select, unaligned register address, invalid offset, read-only write, or access to an unimplemented page returns ERR. Registers are 32-bit little-endian; writes with byte enables merge selected bytes unless identified as W1C or command registers. + +## Register maps + +All offsets below are relative to the corresponding 4 KiB window in [MEMORY_MAP.md](MEMORY_MAP.md). + +### UART — `0x4000_0000` + +| Offset | Name | Access | Definition | +|---:|---|---|---| +| `0x00` | TXDATA / RXDATA | W / R | Write sends low byte when TX enabled and idle; read returns last received byte and clears RX valid/overrun | +| `0x04` | RXDATA | R | Receive data alias; clears RX valid/overrun | +| `0x08` | STATUS | R | bit 0 TX busy, bit 1 RX valid, bit 2 RX overrun | +| `0x0C` | CONTROL | RW | bit 0 TX enable, bit 1 RX enable, bit 2 TX-empty IRQ enable, bit 3 RX-valid IRQ enable | +| `0x10` | DIVIDER | RW | System clocks per serial bit; reset value is rounded `CLK_HZ/115200`, minimum 4; writes below 4 clamp to 4 | + +Serial format is 8 data bits, no parity, one stop bit, LSB first. TX data writes while busy or disabled return ERR. RX samples the synchronized input near bit centers; a complete valid frame sets RX valid, a new unread frame sets overrun. UART IRQ is a level OR of the enabled TX-empty and RX-valid conditions. The default `CLK_HZ` parameter is 50 MHz (divider 434, approximately 115207 baud); software can set the divider for the actual system clock. + +### GPIO — `0x4000_1000` + +| Offset | Name | Access | Definition | +|---:|---|---|---| +| `0x00` | DATA | R/W | Read synchronized pin inputs; write output latch | +| `0x04` | DIRECTION | RW | 1 drives the corresponding output; 0 is input | +| `0x08` | IRQ_ENABLE | RW | Per-pin interrupt mask | +| `0x0C` | IRQ_TYPE | RW | 1 edge-sensitive, 0 level-sensitive | +| `0x10` | IRQ_POLARITY | RW | Edge: 1 rising / 0 falling; level: 1 high / 0 low | +| `0x14` | IRQ_PENDING | R/W1C | Sticky edge status OR active level conditions; selected written ones clear edge bits | + +Each input crosses two synchronizer flip-flops. Reset direction and interrupt enables are zero; reset polarity is zero. Set polarity bits high when low inputs should not assert level interrupts. `irq_gpio` is the OR of enabled pending/active pins. + +### Machine timer — `0x4000_4000` + +| Offset | Name | Access | Definition | +|---:|---|---|---| +| `0x00` | MTIME_LO | R/W | `mtime[31:0]` | +| `0x04` | MTIME_HI | R/W | `mtime[63:32]` | +| `0x08` | MTIMECMP_LO | R/W | `mtimecmp[31:0]` | +| `0x0C` | MTIMECMP_HI | R/W | `mtimecmp[63:32]` | +| `0x10` | CONTROL | R/W | bit 0 enables MTIP | + +`mtime` increments once per system clock and is not reset by ordinary register writes. `mtimecmp` resets to all ones; CONTROL resets enabled. MTIP is asserted while enabled and `mtime >= mtimecmp`. Software should write the high compare word before the low word when moving a compare from its reset value. + +### SPI master — `0x4000_2000` + +| Offset | Name | Access | Definition | +|---:|---|---|---| +| `0x00` | DATA | W/R | Write starts one byte when enabled and idle; read returns last byte received and clears valid | +| `0x04` | CONTROL | RW | bit 0 enable, bit 1 CPOL, bit 2 CPHA | +| `0x08` | DIVIDER | RW | System clocks per SCK half-period, minimum 2 | +| `0x0C` | STATUS | R | bit 0 busy, bit 1 RX valid | + +Transfers are eight bits, MSB first, one chip select, and support SPI modes 0–3. DATA writes while disabled or busy return ERR. STATUS is read-only. `spi_cs_n` is high when idle and low for a transfer. + +### I2C master — `0x4000_3000` + +| Offset | Name | Access | Definition | +|---:|---|---|---| +| `0x00` | DATA | RW | TX byte; read returns last received byte and clears RX valid | +| `0x04` | COMMAND | W | `1` START, `2` STOP, `4` write byte, `8` read byte, bit 4 requests NACK after read | +| `0x08` | CONTROL | RW | bit 0 enable | +| `0x0C` | DIVIDER | RW | System clocks per SCL half-period, minimum 2 | +| `0x10` | STATUS | R | bit 0 busy, bit 1 done, bit 2 transmitted-byte NACK, bit 3 RX valid | + +This is a single-controller byte engine. SDA/SCL pins are open drain: `*_oe=1` means actively pull low; `*_oe=0` means release for the external pull-up. It supports START, STOP, byte write with ACK sampling, and byte read followed by ACK or NACK. The controller waits for SCL high during clock-high phases to allow clock stretching. Repeated-start sequencing and multi-controller arbitration are not implemented. + +### Interrupt controller — `0x4000_5000` + +| Offset | Name | Access | Definition | +|---:|---|---|---| +| `0x00` | PENDING | R | bit 0 timer level; bit 1 UART; bit 2 GPIO; bit 3 watchdog pending | +| `0x04` | ENABLE | RW | bit 0 timer/MTIP mask; bits 1–3 external-source masks | +| `0x08` | THRESHOLD | RW | 3-bit exclusive threshold for external sources | +| `0x0C` | CLAIM/COMPLETE | R/W | Read claims and returns highest eligible external source; write source ID completes it | +| `0x10` | UART_PRIORITY | RW | 3-bit priority, reset 3 | +| `0x14` | GPIO_PRIORITY | RW | 3-bit priority, reset 2 | +| `0x18` | WATCHDOG_PRIORITY | RW | 3-bit priority, reset 1 | + +External IDs are UART=1, GPIO=2, watchdog=3. Larger priority wins; ties favor lower ID. A source is eligible when enabled and its priority is strictly greater than threshold. Reset masks all sources and sets threshold 0. MTIP is passed separately to the CPU timer input, matching the present core interface; the CPU gives MEIP priority over MTIP when both are pending. External pending is sticky until claim/complete and reasserts while the source remains high. + +### Watchdog — `0x4000_D000` + +| Offset | Name | Access | Definition | +|---:|---|---|---| +| `0x00` | CONTROL | RW | bit 0 enable; enabling reloads count | +| `0x04` | TIMEOUT | RW | Timeout in system clocks | +| `0x08` | COUNT | R | Remaining clocks | +| `0x0C` | SERVICE | W | Write `0x51F15EED` to reload and clear expiration; wrong key requests reset | + +Watchdog reset request and watchdog interrupt assert when the counter expires. Expiration and invalid service are sticky until system reset or a valid service write. Reset request must be routed through the SoC reset controller; this block does not reset itself or other modules directly. + +### Debug/performance counters — `0x4000_A000` + +| Offset | Name | Access | Definition | +|---:|---|---|---| +| `0x00` | CONTROL | RW | bit 0 enables counters; reset enabled | +| `0x04/0x08` | CYCLE_LO/HI | RW | 64-bit system clock count | +| `0x0C/0x10` | RETIRED_LO/HI | RW | 64-bit architectural retire count from `cpu_core.retire` | +| `0x14` | BUS_COUNT | RW | Wishbone request count | +| `0x18` | IRQ_COUNT | RW | Rising-edge count of timer or external IRQ | +| `0x1C` | LAST_PC | R | PC sampled on the last retire pulse | + +Counters reset to zero and can be read or written by machine-mode software. This is an observation block; it does not provide debug halt, step, or an external debug transport. The CPU's standard cycle/retired CSRs remain available independently. + +### Power/clock requests — `0x4000_B000` + +| Offset | Name | Access | Definition | +|---:|---|---|---| +| `0x00` | CONTROL | RW | bit 0 sleep request; enabled wake event clears it | +| `0x04` | CLOCK_ENABLE | RW | bits 0–4 request UART, GPIO, SPI/I2C, timer, and debug clocks; reset `0x1F` | +| `0x08` | WAKE_ENABLE | RW | bits 0 timer, 1 external IRQ, 2 UART, 3 GPIO, 4 watchdog, 5 software wake | +| `0x0C` | WAKE_STATUS | R/W1C | Latched wake events in the same bit positions | +| `0x10` | SOFTWARE_RESET | W | Full-word key `0x52535401` raises sticky reset request | +| `0x14` | SOFTWARE_WAKE | W | Write bit 0 to latch software wake and clear sleep request | + +Clock enables are requests only. The initial SoC shares one clock; a later integration must use characterized integrated clock-gating cells with safe enable capture and must keep wake logic active. Sleep requests do not gate clocks inside this register block. Wake status is set-dominant when a new event coincides with a W1C clear. Reset requests from power control and watchdog are ORed at the subsystem output. + +## Verification + +`hw/tb/peripheral_subsystem_tb.sv` checks all register maps, byte enables, UART TX waveform and loopback RX, GPIO synchronization/direction/IRQ, SPI modes 0–3, I2C START/STOP/data/NACK waveforms, interrupt mask/threshold/priority/claim/complete, timer compare and MTIP, watchdog service/expiry, performance counters, power wake/clock/reset controls, and error cases. `hw/tb/peripheral_cpu_irq_tb.sv` runs a ROM program through the CPU, memory system, adapter, and peripheral target. The program transmits `Hi`, configures GPIO, enables MTIE/MIE, receives and handles a machine timer interrupt, then enables the watchdog and verifies reset recovery. Run both as part of `mingw32-make sim` (or `make sim` on systems with GNU Make). diff --git a/docs/RESET_AND_CLOCK_SPEC.md b/docs/RESET_AND_CLOCK_SPEC.md index 57eef38..5394da5 100644 --- a/docs/RESET_AND_CLOCK_SPEC.md +++ b/docs/RESET_AND_CLOCK_SPEC.md @@ -1,6 +1,6 @@ # Reset and Clock Specification -This is the proposed system policy. Current RTL has an asynchronous active-low reset on the PC only; the register file has no reset and there is no system clock controller. +This is the system policy. Peripheral power-control RTL exposes sleep, wake, clock-enable, and software-reset requests. It does not gate the shared clock; reset requests must be consumed by a system reset controller. ## Reset @@ -22,4 +22,4 @@ This is the proposed system policy. Current RTL has an asynchronous active-low r ## Existing implementation difference -The current `cpu_core` uses `always_ff @(posedge clk or negedge rst_n)` for PC reset, so its PC reset is asynchronous assertion, active-low. The register file state is unreset. This is the observed RTL baseline, not full-system behavior. +The current `cpu_core` uses `always_ff @(posedge clk or negedge rst_n)` for PC reset, so its PC reset is asynchronous assertion, active-low. The register file state is unreset. Peripheral reset is asynchronous active-low. Clock-enable outputs are requests only; no clock is gated in the Phase 06 peripheral subsystem. This is the RTL baseline, not a completed physical reset/clock implementation. diff --git a/hw/rtl/bus/wb_address_decoder.sv b/hw/rtl/bus/wb_address_decoder.sv new file mode 100644 index 0000000..2451222 --- /dev/null +++ b/hw/rtl/bus/wb_address_decoder.sv @@ -0,0 +1,22 @@ +// Full-address, one-hot decode for the fixed ROM/SRAM/peripheral target ports. +module wb_address_decoder #( + parameter logic [31:0] ROM_BASE = 32'h0000_0000, + parameter logic [31:0] ROM_MASK = 32'hFFFF_F000, + parameter logic [31:0] SRAM_BASE = 32'h1000_0000, + parameter logic [31:0] SRAM_MASK = 32'hFFFF_8000, + parameter logic [31:0] PERIPH_BASE = 32'h4000_0000, + parameter logic [31:0] PERIPH_MASK = 32'hFFFF_0000 +) ( + input logic [31:0] adr, + output logic [2:0] select, + output logic unmapped +); + logic [2:0] raw_select; + always_comb begin + raw_select[0] = ((adr & ROM_MASK) == (ROM_BASE & ROM_MASK)); + raw_select[1] = ((adr & SRAM_MASK) == (SRAM_BASE & SRAM_MASK)); + raw_select[2] = ((adr & PERIPH_MASK) == (PERIPH_BASE & PERIPH_MASK)); + end + assign unmapped = !(|raw_select) || ((raw_select & (raw_select - 3'b001)) != 3'b000); + assign select = unmapped ? 3'b000 : raw_select; +endmodule diff --git a/hw/rtl/bus/wb_arbiter.sv b/hw/rtl/bus/wb_arbiter.sv new file mode 100644 index 0000000..e6c5374 --- /dev/null +++ b/hw/rtl/bus/wb_arbiter.sv @@ -0,0 +1,83 @@ +// Two-master Wishbone B4 Classic arbiter. One transfer may be outstanding. +// The selected owner's request is held until ACK, ERR, reset, or timeout. +module wb_arbiter #( + parameter integer TIMEOUT_CYCLES = 16 +) ( + input logic clk, + input logic rst_n, + input logic m0_cyc, m0_stb, m0_we, + input logic [31:0] m0_adr, m0_dat_w, + input logic [3:0] m0_sel, + output logic [31:0] m0_dat_r, + output logic m0_ack, m0_err, + input logic m1_cyc, m1_stb, m1_we, + input logic [31:0] m1_adr, m1_dat_w, + input logic [3:0] m1_sel, + output logic [31:0] m1_dat_r, + output logic m1_ack, m1_err, + output logic s_cyc, s_stb, s_we, + output logic [31:0] s_adr, s_dat_w, + output logic [3:0] s_sel, + input logic [31:0] s_dat_r, + input logic s_ack, s_err +); + localparam integer TIMEOUT_LIMIT = (TIMEOUT_CYCLES < 1) ? 1 : TIMEOUT_CYCLES; + localparam integer COUNT_W = (TIMEOUT_LIMIT < 2) ? 1 : $clog2(TIMEOUT_LIMIT + 1); + logic busy, owner, next_owner; + logic [COUNT_W-1:0] wait_count; + logic timeout; + assign s_cyc = busy && (owner ? m1_cyc : m0_cyc); + assign s_stb = busy && (owner ? m1_stb : m0_stb); + assign s_we = busy && (owner ? m1_we : m0_we); + assign s_adr = busy ? (owner ? m1_adr : m0_adr) : 32'b0; + assign s_dat_w = busy ? (owner ? m1_dat_w : m0_dat_w) : 32'b0; + assign s_sel = busy ? (owner ? m1_sel : m0_sel) : 4'b0; + assign m0_dat_r = busy && !owner ? s_dat_r : 32'b0; + assign m1_dat_r = busy && owner ? s_dat_r : 32'b0; + assign m0_ack = busy && !owner && s_ack; + assign m1_ack = busy && owner && s_ack; + assign m0_err = busy && !owner && (s_err | timeout); + assign m1_err = busy && owner && (s_err | timeout); + + assign timeout = busy && (owner ? (m1_cyc && m1_stb) : (m0_cyc && m0_stb)) && + (wait_count >= TIMEOUT_LIMIT-1) && !s_ack && !s_err; + + always_ff @(posedge clk or negedge rst_n) begin + if (!rst_n) begin + busy <= 1'b0; + owner <= 1'b0; + next_owner <= 1'b0; + wait_count <= '0; + end else if (!busy) begin + wait_count <= '0; + if (m0_cyc && m0_stb && m1_cyc && m1_stb) begin + owner <= next_owner; + busy <= 1'b1; + end else if (m0_cyc && m0_stb) begin + owner <= 1'b0; + busy <= 1'b1; + end else if (m1_cyc && m1_stb) begin + owner <= 1'b1; + busy <= 1'b1; + end + end else if (!(owner ? (m1_cyc && m1_stb) : (m0_cyc && m0_stb)) || s_ack || s_err || timeout) begin + busy <= 1'b0; + wait_count <= '0; + next_owner <= ~owner; + end else if (wait_count < TIMEOUT_LIMIT) begin + wait_count <= wait_count + 1'b1; + end + end + +`ifdef FORMAL + default clocking cb @(posedge clk); endclocking + assert property (m0_ack |-> !m0_err); + assert property (m1_ack |-> !m1_err); + assert property ((m0_ack || m0_err) |-> busy && !owner); + assert property ((m1_ack || m1_err) |-> busy && owner); + assert property (busy && !(s_ack || s_err || timeout) |=> $stable(owner)); + assert property (m0_cyc && m0_stb && !m0_ack && !m0_err |=> $stable({m0_cyc,m0_stb,m0_we,m0_adr,m0_dat_w,m0_sel})); + assert property (m1_cyc && m1_stb && !m1_ack && !m1_err |=> $stable({m1_cyc,m1_stb,m1_we,m1_adr,m1_dat_w,m1_sel})); + assert property ((s_ack || s_err) |-> busy && s_cyc && s_stb); +`endif +endmodule diff --git a/hw/rtl/bus/wb_cpu_adapter.sv b/hw/rtl/bus/wb_cpu_adapter.sv new file mode 100644 index 0000000..3140ab9 --- /dev/null +++ b/hw/rtl/bus/wb_cpu_adapter.sv @@ -0,0 +1,66 @@ +// Adapts the CPU's ready/error data port to a Wishbone Classic master. +// The CPU interface has no retry: ERR returns ready=1 and error=1 for one cycle. +module wb_cpu_adapter ( + input logic clk, + input logic rst_n, + input logic cpu_valid, + input logic cpu_write, + input logic [31:0] cpu_addr, + input logic [31:0] cpu_wdata, + input logic [3:0] cpu_wstrb, + output logic cpu_ready, + output logic [31:0] cpu_rdata, + output logic cpu_error, + output logic wb_cyc, + output logic wb_stb, + output logic wb_we, + output logic [31:0] wb_adr, + output logic [31:0] wb_dat_w, + output logic [3:0] wb_sel, + input logic [31:0] wb_dat_r, + input logic wb_ack, + input logic wb_err +); + logic active; + logic [31:0] saved_addr, saved_data; + logic [3:0] saved_sel; + logic saved_write; + + always_comb begin + wb_cyc = active; + wb_stb = active; + wb_we = saved_write; + wb_adr = saved_addr; + wb_dat_w = saved_data; + wb_sel = saved_sel; + cpu_ready = active && (wb_ack || wb_err); + cpu_rdata = wb_dat_r; + cpu_error = active && wb_err; + end + + always_ff @(posedge clk or negedge rst_n) begin + if (!rst_n) begin + active <= 1'b0; + saved_addr <= 32'b0; + saved_data <= 32'b0; + saved_sel <= 4'b0; + saved_write <= 1'b0; + end else if (!active) begin + if (cpu_valid) begin + active <= 1'b1; + saved_addr <= {cpu_addr[31:2], 2'b00}; + saved_data <= cpu_wdata; + saved_sel <= cpu_write ? cpu_wstrb : 4'hf; + saved_write <= cpu_write; + end + end else if (wb_ack || wb_err) begin + active <= 1'b0; + end + end + +`ifdef FORMAL + default clocking cb @(posedge clk); endclocking + assert property (active && !wb_ack && !wb_err |=> $stable({wb_we,wb_adr,wb_dat_w,wb_sel})); + assert property (wb_ack |-> !wb_err); +`endif +endmodule diff --git a/hw/rtl/bus/wb_error_slave.sv b/hw/rtl/bus/wb_error_slave.sv new file mode 100644 index 0000000..48014fc --- /dev/null +++ b/hw/rtl/bus/wb_error_slave.sv @@ -0,0 +1,23 @@ +// One-cycle terminating error slave for unmapped or otherwise rejected cycles. +module wb_error_slave ( + input logic clk, + input logic rst_n, + input logic cyc, + input logic stb, + output logic ack, + output logic err, + output logic [31:0] dat_r +); + logic pending; + always_comb begin + ack = 1'b0; + err = pending && cyc && stb; + dat_r = 32'b0; + end + always_ff @(posedge clk or negedge rst_n) begin + if (!rst_n) + pending <= 1'b0; + else + pending <= cyc && stb; + end +endmodule diff --git a/hw/rtl/bus/wb_interconnect.sv b/hw/rtl/bus/wb_interconnect.sv new file mode 100644 index 0000000..91e9582 --- /dev/null +++ b/hw/rtl/bus/wb_interconnect.sv @@ -0,0 +1,84 @@ +// Two-master, three-target Wishbone B4 Classic interconnect. +// Masters: m0 is CPU, m1 is DMA. Targets: s0 ROM, s1 SRAM, s2 peripherals. +// A single global transaction may be active; all target clocks are synchronous. +module wb_interconnect #( + parameter integer TIMEOUT_CYCLES = 16 +) ( + input logic clk, + input logic rst_n, + input logic m0_cyc, m0_stb, m0_we, + input logic [31:0] m0_adr, m0_dat_w, + input logic [3:0] m0_sel, + output logic [31:0] m0_dat_r, + output logic m0_ack, m0_err, + input logic m1_cyc, m1_stb, m1_we, + input logic [31:0] m1_adr, m1_dat_w, + input logic [3:0] m1_sel, + output logic [31:0] m1_dat_r, + output logic m1_ack, m1_err, + output logic [2:0] s_cyc, s_stb, s_we, + output logic [95:0] s_adr, + output logic [95:0] s_dat_w, + output logic [11:0] s_sel, + input logic [95:0] s_dat_r, + input logic [2:0] s_ack, s_err +); + logic d_cyc, d_stb, d_we; + logic [31:0] d_adr, d_dat_w, d_dat_r; + logic [3:0] d_sel; + logic d_ack, d_err; + logic [2:0] target; + logic unmapped; + logic error_ack, error_err; + logic [31:0] error_rdata; + logic target_ack, target_err; + + wb_arbiter #(.TIMEOUT_CYCLES(TIMEOUT_CYCLES)) arbiter ( + .clk, .rst_n, + .m0_cyc, .m0_stb, .m0_we, .m0_adr, .m0_dat_w, .m0_sel, + .m0_dat_r, .m0_ack, .m0_err, + .m1_cyc, .m1_stb, .m1_we, .m1_adr, .m1_dat_w, .m1_sel, + .m1_dat_r, .m1_ack, .m1_err, + .s_cyc(d_cyc), .s_stb(d_stb), .s_we(d_we), .s_adr(d_adr), .s_dat_w(d_dat_w), .s_sel(d_sel), + .s_dat_r(d_dat_r), .s_ack(d_ack), .s_err(d_err) + ); + + wb_address_decoder decoder (.adr(d_adr), .select(target), .unmapped); + wb_error_slave error_target ( + .clk, .rst_n, + .cyc(d_cyc && (unmapped || d_sel == 4'b0)), + .stb(d_stb && (unmapped || d_sel == 4'b0)), + .ack(error_ack), .err(error_err), .dat_r(error_rdata) + ); + + assign s_cyc[0] = d_cyc && target[0] && !unmapped && (d_sel != 0); + assign s_cyc[1] = d_cyc && target[1] && !unmapped && (d_sel != 0); + assign s_cyc[2] = d_cyc && target[2] && !unmapped && (d_sel != 0); + assign s_stb[0] = d_stb && target[0] && !unmapped && (d_sel != 0); + assign s_stb[1] = d_stb && target[1] && !unmapped && (d_sel != 0); + assign s_stb[2] = d_stb && target[2] && !unmapped && (d_sel != 0); + assign s_we = {3{d_we}} & s_stb; + assign s_adr = {3{d_adr}}; + assign s_dat_w = {3{d_dat_w}}; + assign s_sel = {3{d_sel}}; + assign d_dat_r = (unmapped || d_sel == 0) ? error_rdata : + target[0] ? s_dat_r[31:0] : + target[1] ? s_dat_r[63:32] : + target[2] ? s_dat_r[95:64] : 32'b0; + assign target_ack = (unmapped || d_sel == 0) ? error_ack : + target[0] ? s_ack[0] : target[1] ? s_ack[1] : target[2] ? s_ack[2] : 1'b0; + assign target_err = (unmapped || d_sel == 0) ? error_err : + target[0] ? s_err[0] : target[1] ? s_err[1] : target[2] ? s_err[2] : 1'b0; + // If a misbehaving target raises both, terminate as an error only. + assign d_ack = d_cyc && d_stb && target_ack && !target_err; + assign d_err = d_cyc && d_stb && target_err; + +`ifdef FORMAL + default clocking cb @(posedge clk); endclocking + assert property ($onehot0(s_cyc)); + assert property ($onehot0(s_stb)); + assert property ((|s_ack) |-> $onehot(s_ack)); + assert property ((|s_err) |-> $onehot(s_err)); + assert property ((|s_ack) |-> !(|s_err)); +`endif +endmodule diff --git a/hw/rtl/cpu/cpu_core.sv b/hw/rtl/cpu/cpu_core.sv index c534aae..3b9d10f 100644 --- a/hw/rtl/cpu/cpu_core.sv +++ b/hw/rtl/cpu/cpu_core.sv @@ -22,7 +22,8 @@ module cpu_core ( output logic trap_valid, output logic [31:0] trap_pc, output logic [31:0] trap_cause, - output logic [31:0] trap_value + output logic [31:0] trap_value, + output logic retire ); localparam logic [6:0] OPCODE_LOAD = 7'b0000011; localparam logic [6:0] OPCODE_MISC = 7'b0001111; @@ -319,6 +320,11 @@ module cpu_core ( dmem_valid=(state==ST_MEM); dmem_write=mem_is_store; dmem_addr=mem_aligned_addr; dmem_wdata=mem_write_data; dmem_wstrb=mem_write_strobe; end + // One-cycle architectural retirement pulse for observability/performance + // counters. Memory instructions retire only when their bus operation ends. + assign retire = rst_n && !trap_take && + ((state==ST_EXEC && dec_legal && !dec_exception && !dec_is_load && !dec_is_store) || + (state==ST_MEM && dmem_ready && !dmem_error)); always_comb begin trap_take = 1'b0; diff --git a/hw/rtl/memory/boot_rom.sv b/hw/rtl/memory/boot_rom.sv new file mode 100644 index 0000000..8b886fe --- /dev/null +++ b/hw/rtl/memory/boot_rom.sv @@ -0,0 +1,53 @@ +// 4 KiB read-only image. Wishbone reads ACK in one clock; writes ERR. +// fetch_addr/fetch_data provide the CPU's existing combinational fetch window. +module boot_rom #( + parameter INIT_FILE = "hw/tb/boot_image.hex" +) ( + input logic clk, rst_n, + input logic cyc, stb, we, + input logic [31:0] adr, dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack, err, + input logic [31:0] fetch_addr, + output logic [31:0] fetch_data +); + logic [31:0] mem [0:1023]; + logic pending, pending_err; + logic [31:0] response; + logic [9:0] fetch_word_index; + integer i; + initial begin + for (i=0; i<1024; i=i+1) mem[i] = 32'h0000_0013; + $readmemh(INIT_FILE, mem); + end + // Present a 32-bit fetch window whose low halfword is at fetch_addr, + // including RV32C instructions starting in the upper half of a word. + // The fetch port has no fault handshake; out-of-ROM fetches return NOP. + assign fetch_word_index = fetch_addr[11:2]; + always_comb begin + fetch_data = 32'h0000_0013; + if (fetch_addr[31:12] == 0) begin + fetch_data = mem[fetch_word_index]; + if (fetch_addr[1]) begin + if (fetch_word_index != 10'd1023) + fetch_data = {mem[fetch_word_index + 10'd1][15:0], mem[fetch_word_index][31:16]}; + else + fetch_data = {16'h0013, mem[fetch_word_index][31:16]}; + end + end + end + assign dat_r = response; + assign ack = pending && !pending_err && cyc && stb; + assign err = pending && pending_err && cyc && stb; + always_ff @(posedge clk or negedge rst_n) begin + if (!rst_n) begin pending<=0; pending_err<=0; response<=0; end + else begin + pending <= cyc && stb; + if (cyc && stb) begin + pending_err <= we || sel==0 || adr[31:12]!=0; + response <= (!we && sel!=0 && adr[31:12]==0) ? mem[adr[11:2]] : 0; + end + end + end +endmodule diff --git a/hw/rtl/memory/memory_subsystem.sv b/hw/rtl/memory/memory_subsystem.sv new file mode 100644 index 0000000..74773d2 --- /dev/null +++ b/hw/rtl/memory/memory_subsystem.sv @@ -0,0 +1,36 @@ +// Combined CPU instruction fetch and Wishbone memory slave. +module memory_subsystem #( + parameter ROM_INIT_FILE = "hw/tb/boot_image.hex", + parameter SRAM_INIT_FILE = "" +) ( + input logic clk, rst_n, + input logic [31:0] fetch_addr, + output logic [31:0] fetch_data, + input logic cyc, stb, we, + input logic [31:0] adr, dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack, err +); + logic [31:0] rdata_rom, rdata_sram; + logic ack_rom, err_rom, ack_sram, err_sram; + logic rom_sel, sram_sel; + assign rom_sel = adr[31:12] == 20'h00000; + assign sram_sel = adr[31:15] == 17'h02000; // 0x1000_0000 / 0x8000 + + boot_rom #(.INIT_FILE(ROM_INIT_FILE)) rom ( + .clk, .rst_n, .cyc(cyc && rom_sel), .stb(stb && rom_sel), .we, .adr, .dat_w, .sel, + .dat_r(rdata_rom), .ack(ack_rom), .err(err_rom), .fetch_addr, .fetch_data + ); + wb_sram #(.INIT_FILE(SRAM_INIT_FILE)) sram ( + .clk, .rst_n, .cyc(cyc && sram_sel), .stb(stb && sram_sel), .we, .adr, .dat_w, .sel, + .dat_r(rdata_sram), .ack(ack_sram), .err(err_sram) + ); + logic pending_unmapped; + always_ff @(posedge clk or negedge rst_n) + if (!rst_n) pending_unmapped <= 0; + else pending_unmapped <= cyc && stb && !rom_sel && !sram_sel; + assign dat_r = rom_sel ? rdata_rom : sram_sel ? rdata_sram : 32'b0; + assign ack = rom_sel ? ack_rom : sram_sel ? ack_sram : 1'b0; + assign err = rom_sel ? err_rom : sram_sel ? err_sram : pending_unmapped && cyc && stb; +endmodule diff --git a/hw/rtl/memory/wb_sram.sv b/hw/rtl/memory/wb_sram.sv new file mode 100644 index 0000000..3f9b038 --- /dev/null +++ b/hw/rtl/memory/wb_sram.sv @@ -0,0 +1,42 @@ +// Behavioral 32 KiB little-endian SRAM, Wishbone Classic, one-cycle response. +// ASIC flows must replace this module with a characterized SRAM wrapper. +module wb_sram #( + parameter INIT_FILE = "" +) ( + input logic clk, rst_n, + input logic cyc, stb, we, + input logic [31:0] adr, dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack, err +); + logic [31:0] mem [0:8191]; + logic pending; + logic [31:0] response; + logic response_err; + logic [12:0] word_index; + integer lane; + + initial if (INIT_FILE != "") $readmemh(INIT_FILE, mem); + assign word_index = adr[14:2]; + assign dat_r = response; + assign ack = pending && !response_err && cyc && stb; + assign err = pending && response_err && cyc && stb; + + always_ff @(posedge clk or negedge rst_n) begin + if (!rst_n) begin pending <= 1'b0; response <= 0; response_err <= 0; end + else begin + pending <= cyc && stb; + if (cyc && stb) begin + response_err <= (sel == 0) || (adr[31:15] != 17'h02000); + if (sel != 0 && adr[31:15] == 17'h02000) begin + if (we) begin + for (lane=0; lane<4; lane=lane+1) + if (sel[lane]) mem[word_index][lane*8 +: 8] <= dat_w[lane*8 +: 8]; + end + response <= mem[word_index]; + end else response <= 0; + end + end + end +endmodule diff --git a/hw/rtl/peripherals/interrupt_controller.sv b/hw/rtl/peripherals/interrupt_controller.sv new file mode 100644 index 0000000..d8bf41f --- /dev/null +++ b/hw/rtl/peripherals/interrupt_controller.sv @@ -0,0 +1,85 @@ +// Compact PLIC-like controller at 0x4000_5000. +// Timer is routed to the CPU MTIP input; UART/GPIO/watchdog share MEIP. +// External source IDs: UART=1, GPIO=2, watchdog=3. Higher numeric priority +// wins; equal priorities choose the lowest ID. Threshold is exclusive. +module interrupt_controller #( + parameter logic [31:0] BASE_ADDR=32'h4000_5000 +) ( + input logic clk, rst_n, + input logic cyc, stb, we, + input logic [31:0] adr, dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack, err, + input logic irq_timer_in, irq_uart_in, irq_gpio_in, irq_watchdog_in, + output logic irq_timer, irq_external +); + logic [2:0] pending_ext; + logic [3:0] enable_ext; + logic [2:0] priority_uart,priority_gpio,priority_watchdog,threshold; + logic pending,pending_err,accept,saved_we; + logic [31:0] response,saved_adr,saved_data; + logic [3:0] saved_sel; + logic [2:0] eligible; + logic [1:0] claim_id; + logic [2:0] clear_mask; + logic [2:0] best_priority; + logic [31:0] word_value; + function automatic [31:0] merge_bytes(input [31:0] old_v,input [31:0] new_v,input [3:0] lanes); + integer b; begin merge_bytes=old_v; for(b=0;b<4;b=b+1) if(lanes[b]) merge_bytes[b*8 +: 8]=new_v[b*8 +: 8]; end + endfunction + assign accept=cyc&&stb&&(!pending||({we,adr,dat_w,sel}!={saved_we,saved_adr,saved_data,saved_sel})); + assign dat_r=response;assign ack=pending&&!pending_err&&cyc&&stb;assign err=pending&&pending_err&&cyc&&stb; + assign irq_timer=irq_timer_in&&enable_ext[0]; + always_comb begin + eligible[0]=pending_ext[0]&&enable_ext[1]&&(priority_uart>threshold); + eligible[1]=pending_ext[1]&&enable_ext[2]&&(priority_gpio>threshold); + eligible[2]=pending_ext[2]&&enable_ext[3]&&(priority_watchdog>threshold); + claim_id=0; best_priority=0; + if(eligible[0]) begin claim_id=1; best_priority=priority_uart; end + if(eligible[1]&&(claim_id==0||priority_gpio>best_priority)) begin claim_id=2; best_priority=priority_gpio; end + if(eligible[2]&&(claim_id==0||priority_watchdog>best_priority)) begin claim_id=3; best_priority=priority_watchdog; end + clear_mask=0; + if(accept&&adr[11:0]==12'h00c) begin + if(!we&&claim_id!=0) clear_mask[claim_id-1'b1]=1'b1; + else if(we&&sel[0]&&dat_w[7:0]>=1&&dat_w[7:0]<=3) clear_mask[dat_w[1:0]-1'b1]=1'b1; + end + end + assign irq_external=|eligible; + always_comb begin + case(adr[11:0]) + 12'h000: word_value={28'b0,pending_ext[2],pending_ext[1],pending_ext[0],irq_timer_in}; + 12'h004: word_value={28'b0,enable_ext}; + 12'h008: word_value={29'b0,threshold}; + 12'h00c: word_value={30'b0,claim_id}; + 12'h010: word_value={29'b0,priority_uart}; + 12'h014: word_value={29'b0,priority_gpio}; + 12'h018: word_value={29'b0,priority_watchdog}; + default: word_value=0; + endcase + end + always_ff @(posedge clk or negedge rst_n) begin + if(!rst_n) begin + pending_ext<=0;enable_ext<=0;threshold<=0;priority_uart<=3;priority_gpio<=2;priority_watchdog<=1; + pending<=0;pending_err<=0;response<=0;saved_adr<=0;saved_data<=0;saved_sel<=0;saved_we<=0; + end else begin + pending_ext <= (pending_ext & ~clear_mask) | {irq_watchdog_in,irq_gpio_in,irq_uart_in}; + if(!accept&&pending) pending<=0; + else if(accept) begin + pending<=1;saved_adr<=adr;saved_data<=dat_w;saved_sel<=sel;saved_we<=we;response<=word_value; + pending_err <= (sel==0)||(adr[1:0]!=0)||(adr[31:12]!=BASE_ADDR[31:12])|| + !((adr[11:0]==0)||(adr[11:0]==4)||(adr[11:0]==8)||(adr[11:0]==12)||(adr[11:0]==16)||(adr[11:0]==20)||(adr[11:0]==24)); + if(we&&adr[11:0]==12'h000) pending_err<=1; + if(we&&sel!=0&&adr[31:12]==BASE_ADDR[31:12]) case(adr[11:0]) + 12'h004: enable_ext<=merge_bytes({28'b0,enable_ext},dat_w,sel); + 12'h008: threshold<=merge_bytes({29'b0,threshold},dat_w,sel); + 12'h00c: begin end + 12'h010: priority_uart<=merge_bytes({29'b0,priority_uart},dat_w,sel); + 12'h014: priority_gpio<=merge_bytes({29'b0,priority_gpio},dat_w,sel); + 12'h018: priority_watchdog<=merge_bytes({29'b0,priority_watchdog},dat_w,sel); + default: begin end + endcase + end + end + end +endmodule diff --git a/hw/rtl/peripherals/peripheral_subsystem.sv b/hw/rtl/peripherals/peripheral_subsystem.sv new file mode 100644 index 0000000..00f6270 --- /dev/null +++ b/hw/rtl/peripherals/peripheral_subsystem.sv @@ -0,0 +1,89 @@ +// Wishbone target for Phase 06 peripherals. Connect irq_timer/irq_external +// directly to the matching cpu_core inputs. Watchdog reset_request is active +// high and should be combined with the SoC reset controller. +module peripheral_subsystem #( + parameter integer CLK_HZ=50_000_000, + parameter integer GPIO_WIDTH=32 +) ( + input logic clk, rst_n, + input logic cyc, stb, we, + input logic [31:0] adr, dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack, err, + output logic uart_tx, + input logic uart_rx, + input logic [GPIO_WIDTH-1:0] gpio_in, + output logic [GPIO_WIDTH-1:0] gpio_out, gpio_oe, + output logic spi_sck,spi_cs_n,spi_mosi, + input logic spi_miso, + input logic i2c_scl_i,i2c_sda_i, + output logic i2c_scl_o,i2c_scl_oe,i2c_sda_o,i2c_sda_oe, + input logic [31:0] cpu_pc, + input logic cpu_retire, + output logic irq_timer, irq_external, reset_request, + output logic sleep_request, + output logic [4:0] clock_enable_req, + output logic [5:0] wake_status +); + logic uart_sel,gpio_sel,spi_sel,i2c_sel,timer_sel,irqc_sel,wd_sel,debug_sel,power_sel,known_sel; + logic [31:0] uart_r,gpio_r,spi_r,i2c_r,timer_r,irqc_r,wd_r,debug_r,power_r; + logic uart_ack,uart_err,gpio_ack,gpio_err,spi_ack,spi_err,i2c_ack,i2c_err,timer_ack,timer_err,irqc_ack,irqc_err,wd_ack,wd_err,debug_ack,debug_err,power_ack,power_err; + logic irq_uart,irq_gpio,irq_watchdog,irq_timer_raw,irq_spi,irq_i2c,soft_reset_request,wd_reset_request,bus_event; + logic bus_seen,bus_saved_we; + logic [31:0] bus_saved_adr,bus_saved_data; + logic [3:0] bus_saved_sel; + logic unmapped_pending; + assign uart_sel=(adr[31:12]==20'h40000); + assign gpio_sel=(adr[31:12]==20'h40001); + assign spi_sel=(adr[31:12]==20'h40002); + assign i2c_sel=(adr[31:12]==20'h40003); + assign timer_sel=(adr[31:12]==20'h40004); + assign irqc_sel=(adr[31:12]==20'h40005); + assign wd_sel=(adr[31:12]==20'h4000d); + assign debug_sel=(adr[31:12]==20'h4000a); + assign power_sel=(adr[31:12]==20'h4000b); + assign known_sel=uart_sel|gpio_sel|spi_sel|i2c_sel|timer_sel|irqc_sel|wd_sel|debug_sel|power_sel; + wb_uart #(.CLK_HZ(CLK_HZ)) uart(.clk,.rst_n,.cyc(cyc&&uart_sel),.stb(stb&&uart_sel),.we,.adr,.dat_w,.sel, + .dat_r(uart_r),.ack(uart_ack),.err(uart_err),.uart_tx,.uart_rx,.irq_uart); + wb_gpio #(.WIDTH(GPIO_WIDTH)) gpio(.clk,.rst_n,.cyc(cyc&&gpio_sel),.stb(stb&&gpio_sel),.we,.adr,.dat_w,.sel, + .dat_r(gpio_r),.ack(gpio_ack),.err(gpio_err),.gpio_in,.gpio_out,.gpio_oe,.irq_gpio); + wb_spi spi(.clk,.rst_n,.cyc(cyc&&spi_sel),.stb(stb&&spi_sel),.we,.adr,.dat_w,.sel,.dat_r(spi_r),.ack(spi_ack),.err(spi_err), + .spi_sck,.spi_cs_n,.spi_mosi,.spi_miso,.irq_spi); + wb_i2c i2c(.clk,.rst_n,.cyc(cyc&&i2c_sel),.stb(stb&&i2c_sel),.we,.adr,.dat_w,.sel,.dat_r(i2c_r),.ack(i2c_ack),.err(i2c_err), + .scl_i(i2c_scl_i),.sda_i(i2c_sda_i),.scl_o(i2c_scl_o),.scl_oe(i2c_scl_oe),.sda_o(i2c_sda_o),.sda_oe(i2c_sda_oe),.irq_i2c); + wb_timer timer(.clk,.rst_n,.cyc(cyc&&timer_sel),.stb(stb&&timer_sel),.we,.adr,.dat_w,.sel, + .dat_r(timer_r),.ack(timer_ack),.err(timer_err),.irq_timer(irq_timer_raw)); + wb_watchdog watchdog(.clk,.rst_n,.cyc(cyc&&wd_sel),.stb(stb&&wd_sel),.we,.adr,.dat_w,.sel, + .dat_r(wd_r),.ack(wd_ack),.err(wd_err),.irq_watchdog,.reset_request(wd_reset_request)); + interrupt_controller intc(.clk,.rst_n,.cyc(cyc&&irqc_sel),.stb(stb&&irqc_sel),.we,.adr,.dat_w,.sel, + .dat_r(irqc_r),.ack(irqc_ack),.err(irqc_err),.irq_timer_in(irq_timer_raw),.irq_uart_in(irq_uart), + .irq_gpio_in(irq_gpio),.irq_watchdog_in(irq_watchdog),.irq_timer,.irq_external); + wb_debug_perf debug_regs(.clk,.rst_n,.cyc(cyc&&debug_sel),.stb(stb&&debug_sel),.we,.adr,.dat_w,.sel, + .dat_r(debug_r),.ack(debug_ack),.err(debug_err),.cpu_retire,.cpu_pc,.bus_event,.irq_event(irq_timer|irq_external)); + wb_power_ctrl power(.clk,.rst_n,.cyc(cyc&&power_sel),.stb(stb&&power_sel),.we,.adr,.dat_w,.sel, + .dat_r(power_r),.ack(power_ack),.err(power_err),.wake_sources({wd_reset_request,irq_gpio,irq_uart,irq_external,irq_timer_raw}), + .sleep_request,.clock_enable_req,.wake_status,.reset_request(soft_reset_request)); + assign reset_request=wd_reset_request|soft_reset_request; + assign bus_event=cyc&&stb&&(!bus_seen||({we,adr,dat_w,sel}!={bus_saved_we,bus_saved_adr,bus_saved_data,bus_saved_sel})); + always_ff @(posedge clk or negedge rst_n) + if(!rst_n)begin bus_seen<=0;bus_saved_we<=0;bus_saved_adr<=0;bus_saved_data<=0;bus_saved_sel<=0;end + else if(!cyc||!stb)bus_seen<=0; + else if(bus_event)begin bus_seen<=1;bus_saved_we<=we;bus_saved_adr<=adr;bus_saved_data<=dat_w;bus_saved_sel<=sel;end + always_ff @(posedge clk or negedge rst_n) + if(!rst_n) unmapped_pending<=0; + else unmapped_pending<=cyc&&stb&&!known_sel; + always_comb begin + dat_r=0;ack=0;err=0; + if(uart_sel) begin dat_r=uart_r;ack=uart_ack;err=uart_err;end + else if(gpio_sel) begin dat_r=gpio_r;ack=gpio_ack;err=gpio_err;end + else if(spi_sel) begin dat_r=spi_r;ack=spi_ack;err=spi_err;end + else if(i2c_sel) begin dat_r=i2c_r;ack=i2c_ack;err=i2c_err;end + else if(timer_sel) begin dat_r=timer_r;ack=timer_ack;err=timer_err;end + else if(irqc_sel) begin dat_r=irqc_r;ack=irqc_ack;err=irqc_err;end + else if(wd_sel) begin dat_r=wd_r;ack=wd_ack;err=wd_err;end + else if(debug_sel) begin dat_r=debug_r;ack=debug_ack;err=debug_err;end + else if(power_sel) begin dat_r=power_r;ack=power_ack;err=power_err;end + else err=unmapped_pending&&cyc&&stb; + end +endmodule diff --git a/hw/rtl/peripherals/wb_debug_perf.sv b/hw/rtl/peripherals/wb_debug_perf.sv new file mode 100644 index 0000000..e064829 --- /dev/null +++ b/hw/rtl/peripherals/wb_debug_perf.sv @@ -0,0 +1,55 @@ +// Machine-mode performance/debug observation counters at 0x4000_A000. +// This block provides counters/last-PC only; no halt or single-step transport. +module wb_debug_perf #( + parameter logic [31:0] BASE_ADDR=32'h4000_a000 +) ( + input logic clk,rst_n, + input logic cyc,stb,we, + input logic [31:0] adr,dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack,err, + input logic cpu_retire, + input logic [31:0] cpu_pc, + input logic bus_event,irq_event +); + logic enabled,pending,response_err,accept,saved_we,irq_prev; + logic [31:0] response,saved_adr,saved_data,last_pc; + logic [3:0] saved_sel; + logic [63:0] cycle_count,retired_count; + logic [31:0] bus_count,irq_count; + function automatic [31:0] merge_bytes(input [31:0] old_v,input [31:0] new_v,input [3:0] lanes); + integer b;begin merge_bytes=old_v;for(b=0;b<4;b=b+1)if(lanes[b])merge_bytes[b*8 +: 8]=new_v[b*8 +: 8];end + endfunction + assign accept=cyc&&stb&&(!pending||({we,adr,dat_w,sel}!={saved_we,saved_adr,saved_data,saved_sel})); + assign dat_r=response;assign ack=pending&&!response_err&&cyc&&stb;assign err=pending&&response_err&&cyc&&stb; + always_ff @(posedge clk or negedge rst_n)begin + if(!rst_n)begin enabled<=1;pending<=0;response_err<=0;response<=0;saved_adr<=0;saved_data<=0;saved_sel<=0;saved_we<=0;cycle_count<=0;retired_count<=0;bus_count<=0;irq_count<=0;last_pc<=0;irq_prev<=0;end + else begin + irq_prev<=irq_event; + if(enabled)begin + cycle_count<=cycle_count+1'b1; + if(cpu_retire)begin retired_count<=retired_count+1'b1;last_pc<=cpu_pc;end + if(bus_event)bus_count<=bus_count+1'b1; + if(irq_event&&!irq_prev)irq_count<=irq_count+1'b1; + end + if(!accept&&pending)pending<=0; + else if(accept)begin + pending<=1;saved_adr<=adr;saved_data<=dat_w;saved_sel<=sel;saved_we<=we;response<=0; + response_err<=(sel==0)||(adr[1:0]!=0)||(adr[31:12]!=BASE_ADDR[31:12])|| + !((adr[11:0]==0)||(adr[11:0]==4)||(adr[11:0]==8)||(adr[11:0]==12)||(adr[11:0]==16)||(adr[11:0]==20)||(adr[11:0]==24)||(adr[11:0]==28)); + if(adr[31:12]==BASE_ADDR[31:12]&&adr[1:0]==0&&sel!=0)case(adr[11:0]) + 12'h000:begin response<={31'b0,enabled};if(we&&sel[0])enabled<=dat_w[0];end + 12'h004:begin response<=cycle_count[31:0];if(we)cycle_count[31:0]<=merge_bytes(cycle_count[31:0],dat_w,sel);end + 12'h008:begin response<=cycle_count[63:32];if(we)cycle_count[63:32]<=merge_bytes(cycle_count[63:32],dat_w,sel);end + 12'h00c:begin response<=retired_count[31:0];if(we)retired_count[31:0]<=merge_bytes(retired_count[31:0],dat_w,sel);end + 12'h010:begin response<=retired_count[63:32];if(we)retired_count[63:32]<=merge_bytes(retired_count[63:32],dat_w,sel);end + 12'h014:begin response<=bus_count;if(we)bus_count<=merge_bytes(bus_count,dat_w,sel);end + 12'h018:begin response<=irq_count;if(we)irq_count<=merge_bytes(irq_count,dat_w,sel);end + 12'h01c:begin response<=last_pc;if(we)response_err<=1;end + default:begin end + endcase + end + end + end +endmodule diff --git a/hw/rtl/peripherals/wb_gpio.sv b/hw/rtl/peripherals/wb_gpio.sv new file mode 100644 index 0000000..e908c00 --- /dev/null +++ b/hw/rtl/peripherals/wb_gpio.sv @@ -0,0 +1,61 @@ +// 32-pin GPIO at 0x4000_1000. Inputs pass through two synchronizer flops. +// IRQ_TYPE=1 selects edge mode (rising if IRQ_POL=1, falling otherwise); +// IRQ_TYPE=0 selects level mode (high if IRQ_POL=1, low otherwise). +module wb_gpio #( + parameter integer WIDTH=32, + parameter logic [31:0] BASE_ADDR=32'h4000_1000 +) ( + input logic clk, rst_n, + input logic cyc, stb, we, + input logic [31:0] adr, dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack, err, + input logic [WIDTH-1:0] gpio_in, + output logic [WIDTH-1:0] gpio_out, + output logic [WIDTH-1:0] gpio_oe, + output logic irq_gpio +); + logic [WIDTH-1:0] sync1,sync2,previous,output_reg,direction,irq_enable,irq_type,irq_polarity,irq_pending; + logic pending,response_err,accept,saved_we; + logic [31:0] response,saved_adr,saved_data; + logic [3:0] saved_sel; + logic [WIDTH-1:0] edge_event,level_event; + logic [31:0] lane_mask; + logic [WIDTH-1:0] clear_pending_mask; + function automatic [31:0] merge_bytes(input [31:0] old_v,input [31:0] new_v,input [3:0] lanes); + integer b; begin merge_bytes=old_v; for(b=0;b<4;b=b+1) if(lanes[b]) merge_bytes[b*8 +: 8]=new_v[b*8 +: 8]; end + endfunction + assign accept=cyc&&stb&&(!pending||({we,adr,dat_w,sel}!={saved_we,saved_adr,saved_data,saved_sel})); + assign dat_r=response; assign ack=pending&&!response_err&&cyc&&stb; assign err=pending&&response_err&&cyc&&stb; + assign gpio_out=output_reg; assign gpio_oe=direction; + assign lane_mask={{8{sel[3]}},{8{sel[2]}},{8{sel[1]}},{8{sel[0]}}}; + assign clear_pending_mask=(accept&&we&&(adr[11:0]==12'h014)) ? (dat_w[WIDTH-1:0]&lane_mask[WIDTH-1:0]) : {WIDTH{1'b0}}; + assign edge_event=irq_type & (((sync2 & ~previous & irq_polarity)) | ((~sync2 & previous & ~irq_polarity))); + assign level_event=(~irq_type) & ((sync2 & irq_polarity) | (~sync2 & ~irq_polarity)); + assign irq_gpio=|(irq_enable & (irq_pending | level_event)); + always_ff @(posedge clk or negedge rst_n) begin + if(!rst_n) begin sync1<='0;sync2<='0;previous<='0;output_reg<='0;direction<='0;irq_enable<='0;irq_type<='0;irq_polarity<='0;irq_pending<='0;pending<=0;response_err<=0;response<=0;saved_adr<=0;saved_data<=0;saved_sel<=0;saved_we<=0;end + else begin + sync1<=gpio_in;sync2<=sync1;previous<=sync2; + irq_pending <= (irq_pending & ~clear_pending_mask) | edge_event; + if(!accept&&pending) pending<=0; + else if(accept) begin + pending<=1;saved_adr<=adr;saved_data<=dat_w;saved_sel<=sel;saved_we<=we;response<=0; + response_err <= (sel==0)||(adr[1:0]!=0)||(adr[31:12]!=BASE_ADDR[31:12])|| + !((adr[11:0]==0)||(adr[11:0]==4)||(adr[11:0]==8)||(adr[11:0]==12)||(adr[11:0]==16)||(adr[11:0]==20)); + if(adr[31:12]==BASE_ADDR[31:12]&&adr[1:0]==0&&sel!=0) begin + case(adr[11:0]) + 12'h000: begin response<={{(32-WIDTH){1'b0}},sync2}; if(we) output_reg<=merge_bytes({{(32-WIDTH){1'b0}},output_reg},dat_w,sel); end + 12'h004: begin response<={{(32-WIDTH){1'b0}},direction}; if(we) direction<=merge_bytes({{(32-WIDTH){1'b0}},direction},dat_w,sel); end + 12'h008: begin response<={{(32-WIDTH){1'b0}},irq_enable}; if(we) irq_enable<=merge_bytes({{(32-WIDTH){1'b0}},irq_enable},dat_w,sel); end + 12'h00c: begin response<={{(32-WIDTH){1'b0}},irq_type}; if(we) irq_type<=merge_bytes({{(32-WIDTH){1'b0}},irq_type},dat_w,sel); end + 12'h010: begin response<={{(32-WIDTH){1'b0}},irq_polarity}; if(we) irq_polarity<=merge_bytes({{(32-WIDTH){1'b0}},irq_polarity},dat_w,sel); end + 12'h014: response<={{(32-WIDTH){1'b0}},irq_pending | level_event}; + default: begin end + endcase + end + end + end + end +endmodule diff --git a/hw/rtl/peripherals/wb_i2c.sv b/hw/rtl/peripherals/wb_i2c.sv new file mode 100644 index 0000000..082464c --- /dev/null +++ b/hw/rtl/peripherals/wb_i2c.sv @@ -0,0 +1,91 @@ +// Single-controller, byte-oriented I2C master at 0x4000_3000. +// SDA/SCL are open-drain: *_oe=1 actively pulls the line low; *_oe=0 releases it. +module wb_i2c #( + parameter logic [31:0] BASE_ADDR=32'h4000_3000 +) ( + input logic clk,rst_n, + input logic cyc,stb,we, + input logic [31:0] adr,dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack,err, + input logic scl_i,sda_i, + output logic scl_o,scl_oe,sda_o,sda_oe, + output logic irq_i2c +); + localparam logic [3:0] IDLE=0,START=1,STOP_LOW=2,STOP_HIGH=3,BIT_SETUP=4,BIT_HIGH=5,ACK_SETUP=6,ACK_HIGH=7; + logic enabled,busy,done,nack,rx_valid,read_nack,i2c_read; + logic [31:0] divider,count,response,saved_adr,saved_data; + logic [3:0] saved_sel; + logic saved_we,pending,response_err,accept; + logic [3:0] state; + logic [7:0] tx_data,rx_shift,rx_data; + logic [2:0] bit_index; + function automatic [31:0] merge_bytes(input [31:0] old_v,input [31:0] new_v,input [3:0] lanes); + integer b;begin merge_bytes=old_v;for(b=0;b<4;b=b+1)if(lanes[b])merge_bytes[b*8 +: 8]=new_v[b*8 +: 8];end + endfunction + assign accept=cyc&&stb&&(!pending||({we,adr,dat_w,sel}!={saved_we,saved_adr,saved_data,saved_sel})); + assign dat_r=response;assign ack=pending&&!response_err&&cyc&&stb;assign err=pending&&response_err&&cyc&&stb; + assign scl_o=1'b0;assign sda_o=1'b0;assign irq_i2c=done|nack|rx_valid; + always_ff @(posedge clk or negedge rst_n)begin + if(!rst_n)begin enabled<=0;busy<=0;done<=0;nack<=0;rx_valid<=0;read_nack<=0;i2c_read<=0;divider<=4;count<=0;response<=0;saved_adr<=0;saved_data<=0;saved_sel<=0;saved_we<=0;pending<=0;response_err<=0;state<=IDLE;tx_data<=0;rx_shift<=0;rx_data<=0;bit_index<=0;scl_oe<=0;sda_oe<=0;end + else begin + if(!accept&&pending)pending<=0; + else if(accept)begin + pending<=1;saved_adr<=adr;saved_data<=dat_w;saved_sel<=sel;saved_we<=we;response<=0; + response_err<=(sel==0)||(adr[1:0]!=0)||(adr[31:12]!=BASE_ADDR[31:12])||!((adr[11:0]==0)||(adr[11:0]==4)||(adr[11:0]==8)||(adr[11:0]==12)||(adr[11:0]==16)); + if(adr[31:12]==BASE_ADDR[31:12]&&adr[1:0]==0&&sel!=0)case(adr[11:0]) + 12'h000:begin response<={24'b0,rx_data};if(we&&sel[0])tx_data<=dat_w[7:0];else if(!we&&sel[0])rx_valid<=0;end + 12'h004:begin response<=0;if(!we)response_err<=1;else if(busy||!enabled)response_err<=1;else begin + done<=0;nack<=0;count<=0;busy<=1; + case(dat_w[3:0]) + 4'b0001:begin state<=START;scl_oe<=0;sda_oe<=1;end // START + 4'b0010:begin state<=STOP_LOW;scl_oe<=1;sda_oe<=1;end // STOP + 4'b0100:begin state<=BIT_SETUP;i2c_read<=0;bit_index<=7;rx_shift<=0;sda_oe<=~tx_data[7];scl_oe<=1;end // WRITE byte + 4'b1000:begin state<=BIT_SETUP;i2c_read<=1;bit_index<=7;rx_shift<=0;read_nack<=dat_w[4];sda_oe<=0;scl_oe<=1;end // READ; bit 4: NACK response + default:begin busy<=0;state<=IDLE;response_err<=1;end + endcase + end end + 12'h008:begin response<={31'b0,enabled};if(we&&!busy&&sel[0])enabled<=dat_w[0];else if(we&&busy)response_err<=1;end + 12'h00c:begin response<=divider;if(we&&!busy)begin if(merge_bytes(divider,dat_w,sel)<2)divider<=2;else divider<=merge_bytes(divider,dat_w,sel);end else if(we&&busy)response_err<=1;end + 12'h010:begin response<={28'b0,rx_valid,nack,done,busy};if(we)response_err<=1;else begin done<=0;nack<=0;end end + default:begin end + endcase + end + if(busy)begin + if(state==START)begin + if(count+1>=divider)begin count<=0;scl_oe<=1;state<=IDLE;busy<=0;done<=1;end else count<=count+1'b1; + end else if(state==STOP_LOW)begin + if(count+1>=divider)begin count<=0;scl_oe<=0;state<=STOP_HIGH;end else count<=count+1'b1; + end else if(state==STOP_HIGH)begin + if(scl_i)begin if(count+1>=divider)begin count<=0;sda_oe<=0;state<=IDLE;busy<=0;done<=1;end else count<=count+1'b1;end + end else if(state==BIT_SETUP)begin + if(count+1>=divider)begin count<=0;scl_oe<=0;state<=BIT_HIGH;end else count<=count+1'b1; + end else if(state==BIT_HIGH)begin + if(scl_i)begin + if(count+1>=divider)begin + count<=0;scl_oe<=1; + if(!i2c_read)begin + if(bit_index==0)begin sda_oe<=0;state<=ACK_SETUP;end + else begin bit_index<=bit_index-1'b1;sda_oe<=~(tx_data[bit_index-1'b1]);state<=BIT_SETUP;end + end else begin + rx_shift[bit_index]<=sda_i; + if(bit_index==0)begin sda_oe<=~read_nack;state<=ACK_SETUP;end + else begin bit_index<=bit_index-1'b1;state<=BIT_SETUP;end + end + end else count<=count+1'b1; + end + end else if(state==ACK_SETUP)begin + if(count+1>=divider)begin count<=0;scl_oe<=0;state<=ACK_HIGH;end else count<=count+1'b1; + end else if(state==ACK_HIGH)begin + if(scl_i)begin if(count+1>=divider)begin + count<=0;scl_oe<=1;sda_oe<=0; + if(!i2c_read)nack<=sda_i; + else begin rx_data<=rx_shift;rx_valid<=1;end + state<=IDLE;busy<=0;done<=1; + end else count<=count+1'b1;end + end + end + end + end +endmodule diff --git a/hw/rtl/peripherals/wb_power_ctrl.sv b/hw/rtl/peripherals/wb_power_ctrl.sv new file mode 100644 index 0000000..8254e40 --- /dev/null +++ b/hw/rtl/peripherals/wb_power_ctrl.sv @@ -0,0 +1,59 @@ +// Power/clock request and wake-status registers at 0x4000_B000. +// Clock enables are requests for glitch-free ICG integration; this block never +// gates a clock directly. Reset requests are sticky until system reset. +module wb_power_ctrl #( + parameter logic [31:0] BASE_ADDR=32'h4000_b000, + parameter logic [31:0] RESET_KEY=32'h52535401 +) ( + input logic clk,rst_n, + input logic cyc,stb,we, + input logic [31:0] adr,dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack,err, + input logic [4:0] wake_sources, + output logic sleep_request, + output logic [4:0] clock_enable_req, + output logic [5:0] wake_status, + output logic reset_request +); + localparam logic [4:0] VALID_CLOCKS=5'b1_1111; + logic [5:0] wake_enable; + logic pending,response_err,accept,saved_we; + logic [31:0] response,saved_adr,saved_data; + logic [3:0] saved_sel; + logic [5:0] selected_wake,clear_wake; + logic [31:0] lane_mask; + function automatic [31:0] merge_bytes(input [31:0] old_v,input [31:0] new_v,input [3:0] lanes); + integer b;begin merge_bytes=old_v;for(b=0;b<4;b=b+1)if(lanes[b])merge_bytes[b*8 +: 8]=new_v[b*8 +: 8];end + endfunction + assign accept=cyc&&stb&&(!pending||({we,adr,dat_w,sel}!={saved_we,saved_adr,saved_data,saved_sel})); + assign dat_r=response;assign ack=pending&&!response_err&&cyc&&stb;assign err=pending&&response_err&&cyc&&stb; + assign lane_mask={{8{sel[3]}},{8{sel[2]}},{8{sel[1]}},{8{sel[0]}}}; + always_comb begin + selected_wake={1'b0,wake_sources}&wake_enable; + clear_wake=0; + if(accept&&we&&adr[11:0]==12'h00c)clear_wake=dat_w[5:0]&lane_mask[5:0]; + end + always_ff @(posedge clk or negedge rst_n)begin + if(!rst_n)begin sleep_request<=0;clock_enable_req<=VALID_CLOCKS;wake_enable<=0;wake_status<=0;reset_request<=0;pending<=0;response_err<=0;response<=0;saved_adr<=0;saved_data<=0;saved_sel<=0;saved_we<=0;end + else begin + wake_status<=(wake_status&~clear_wake)|selected_wake; + if(|selected_wake)sleep_request<=0; + if(!accept&&pending)pending<=0; + else if(accept)begin + pending<=1;saved_adr<=adr;saved_data<=dat_w;saved_sel<=sel;saved_we<=we;response<=0; + response_err<=(sel==0)||(adr[1:0]!=0)||(adr[31:12]!=BASE_ADDR[31:12])||!((adr[11:0]==0)||(adr[11:0]==4)||(adr[11:0]==8)||(adr[11:0]==12)||(adr[11:0]==16)||(adr[11:0]==20)); + if(adr[31:12]==BASE_ADDR[31:12]&&adr[1:0]==0&&sel!=0)case(adr[11:0]) + 12'h000:begin response<={31'b0,sleep_request};if(we&&sel[0])sleep_request<=dat_w[0];end + 12'h004:begin response<={27'b0,clock_enable_req};if(we)clock_enable_req<=merge_bytes({27'b0,clock_enable_req},dat_w,sel)&VALID_CLOCKS;end + 12'h008:begin response<={26'b0,wake_enable};if(we)wake_enable<=merge_bytes({26'b0,wake_enable},dat_w,sel);end + 12'h00c:begin response<={26'b0,wake_status};end + 12'h010:begin response<=0;if(!we||sel!=4'hf||dat_w!=RESET_KEY)response_err<=1;else reset_request<=1;end + 12'h014:begin response<=0;if(we&&sel[0]&&dat_w[0])begin wake_status[5]<=1;sleep_request<=0;end else response_err<=1;end + default:begin end + endcase + end + end + end +endmodule diff --git a/hw/rtl/peripherals/wb_spi.sv b/hw/rtl/peripherals/wb_spi.sv new file mode 100644 index 0000000..3b22d11 --- /dev/null +++ b/hw/rtl/peripherals/wb_spi.sv @@ -0,0 +1,70 @@ +// Byte-oriented SPI master, mode 0-3, MSB first, at 0x4000_2000. +// DIVIDER specifies system clocks per SCK half-period (minimum 2). +module wb_spi #( + parameter logic [31:0] BASE_ADDR=32'h4000_2000 +) ( + input logic clk,rst_n, + input logic cyc,stb,we, + input logic [31:0] adr,dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack,err, + output logic spi_sck,spi_cs_n,spi_mosi, + input logic spi_miso, + output logic irq_spi +); + logic enabled,cpol_cfg,cpha_cfg,cpol,cpha,busy,rx_valid; + logic [31:0] divider,count,response,saved_adr,saved_data; + logic [3:0] saved_sel; + logic saved_we,pending,response_err,accept; + logic [7:0] tx_shift,rx_shift,rx_data; + logic [3:0] bit_index; + function automatic [31:0] merge_bytes(input [31:0] old_v,input [31:0] new_v,input [3:0] lanes); + integer b; begin merge_bytes=old_v;for(b=0;b<4;b=b+1)if(lanes[b])merge_bytes[b*8 +: 8]=new_v[b*8 +: 8];end + endfunction + assign accept=cyc&&stb&&(!pending||({we,adr,dat_w,sel}!={saved_we,saved_adr,saved_data,saved_sel})); + assign dat_r=response;assign ack=pending&&!response_err&&cyc&&stb;assign err=pending&&response_err&&cyc&&stb; + assign irq_spi=rx_valid; + always_ff @(posedge clk or negedge rst_n) begin + if(!rst_n) begin + enabled<=0;cpol_cfg<=0;cpha_cfg<=0;divider<=2;count<=0;response<=0;saved_adr<=0;saved_data<=0;saved_sel<=0;saved_we<=0;pending<=0;response_err<=0; + tx_shift<=0;rx_shift<=0;rx_data<=0;bit_index<=0;busy<=0;rx_valid<=0;spi_sck<=0;spi_cs_n<=1;spi_mosi<=0;cpol<=0;cpha<=0; + end else begin + if(!accept&&pending)pending<=0; + else if(accept)begin + pending<=1;saved_adr<=adr;saved_data<=dat_w;saved_sel<=sel;saved_we<=we;response<=0; + response_err<=(sel==0)||(adr[1:0]!=0)||(adr[31:12]!=BASE_ADDR[31:12])||!((adr[11:0]==0)||(adr[11:0]==4)||(adr[11:0]==8)||(adr[11:0]==12)); + if(adr[31:12]==BASE_ADDR[31:12]&&adr[1:0]==0&&sel!=0)case(adr[11:0]) + 12'h000:begin response<={24'b0,rx_data};if(we)begin + if(!enabled||busy)response_err<=1; + else begin tx_shift<=dat_w[7:0];rx_shift<=0;bit_index<=0;busy<=1;rx_valid<=0;count<=0;cpol<=cpol_cfg;cpha<=cpha_cfg;spi_sck<=cpol_cfg;spi_cs_n<=0;spi_mosi<=cpha_cfg?1'b0:dat_w[7];end + end else if(sel[0])rx_valid<=0;end + 12'h004:begin response<={29'b0,cpha_cfg,cpol_cfg,enabled};if(we&&sel[0]&&!busy)begin enabled<=dat_w[0];cpol_cfg<=dat_w[1];cpha_cfg<=dat_w[2];end else if(we&&busy)response_err<=1;end + 12'h008:begin response<=divider;if(we&&!busy)begin if(merge_bytes(divider,dat_w,sel)<2)divider<=2;else divider<=merge_bytes(divider,dat_w,sel);end else if(we&&busy)response_err<=1;end + 12'h00c:begin response<={30'b0,rx_valid,busy};if(we)response_err<=1;end + default:begin end + endcase + end + if(busy)begin + if(count+1>=divider)begin + count<=0; + if(spi_sck==cpol)begin + spi_sck<=~cpol; + if(cpha)spi_mosi<=tx_shift[7]; + else rx_shift<={rx_shift[6:0],spi_miso}; + end else begin + spi_sck<=cpol; + if(cpha)rx_shift<={rx_shift[6:0],spi_miso}; + if(bit_index==7)begin + busy<=0;spi_cs_n<=1;spi_mosi<=0; + rx_data<=cpha?{rx_shift[6:0],spi_miso}:rx_shift;rx_valid<=1; + end else begin + bit_index<=bit_index+1'b1;tx_shift<={tx_shift[6:0],1'b0}; + if(!cpha)spi_mosi<=tx_shift[6]; + end + end + end else count<=count+1'b1; + end + end + end +endmodule diff --git a/hw/rtl/peripherals/wb_timer.sv b/hw/rtl/peripherals/wb_timer.sv new file mode 100644 index 0000000..5485880 --- /dev/null +++ b/hw/rtl/peripherals/wb_timer.sv @@ -0,0 +1,58 @@ +// Machine timer at 0x4000_4000. mtime advances once per clk; all bus +// accesses have a registered one-cycle response. +module wb_timer #( + parameter logic [31:0] BASE_ADDR = 32'h4000_4000 +) ( + input logic clk, rst_n, + input logic cyc, stb, we, + input logic [31:0] adr, dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack, err, + output logic irq_timer +); + logic [63:0] mtime, mtimecmp; + logic irq_enable; + logic pending, response_err; + logic [31:0] response, saved_adr, saved_data; + logic [3:0] saved_sel; + logic saved_we; + logic accept; + logic [31:0] merged; + + function automatic [31:0] merge_bytes(input [31:0] old_v, input [31:0] new_v, input [3:0] lanes); + integer b; + begin merge_bytes=old_v; for (b=0;b<4;b=b+1) if (lanes[b]) merge_bytes[b*8 +: 8]=new_v[b*8 +: 8]; end + endfunction + assign accept = cyc && stb && (!pending || ({we,adr,dat_w,sel} != {saved_we,saved_adr,saved_data,saved_sel})); + assign dat_r=response; + assign ack=pending && !response_err && cyc && stb; + assign err=pending && response_err && cyc && stb; + assign irq_timer=irq_enable && (mtime >= mtimecmp); + always_ff @(posedge clk or negedge rst_n) begin + if (!rst_n) begin + mtime<=0; mtimecmp<=64'hffff_ffff_ffff_ffff; irq_enable<=1'b1; + pending<=0; response_err<=0; response<=0; saved_adr<=0; saved_data<=0; saved_sel<=0; saved_we<=0; + end else begin + if (!accept && pending) pending<=0; + else if (accept) begin + pending<=1; saved_adr<=adr; saved_data<=dat_w; saved_sel<=sel; saved_we<=we; + response_err <= (sel==0) || (adr[1:0]!=0) || (adr[31:12] != BASE_ADDR[31:12]) || + !((adr[11:0]==12'h000)||(adr[11:0]==12'h004)||(adr[11:0]==12'h008)|| + (adr[11:0]==12'h00c)||(adr[11:0]==12'h010)); + response<=0; + if (adr[31:12]==BASE_ADDR[31:12] && adr[1:0]==0 && sel!=0) begin + case (adr[11:0]) + 12'h000: begin response<=mtime[31:0]; if (we) mtime[31:0]<=merge_bytes(mtime[31:0],dat_w,sel); end + 12'h004: begin response<=mtime[63:32]; if (we) mtime[63:32]<=merge_bytes(mtime[63:32],dat_w,sel); end + 12'h008: begin response<=mtimecmp[31:0]; if (we) mtimecmp[31:0]<=merge_bytes(mtimecmp[31:0],dat_w,sel); end + 12'h00c: begin response<=mtimecmp[63:32]; if (we) mtimecmp[63:32]<=merge_bytes(mtimecmp[63:32],dat_w,sel); end + 12'h010: begin response<={31'b0,irq_enable}; if (we && sel[0]) irq_enable<=dat_w[0]; end + default: begin end + endcase + end + end + if (!(accept && we && (adr[11:0]==12'h000 || adr[11:0]==12'h004))) mtime<=mtime+64'd1; + end + end +endmodule diff --git a/hw/rtl/peripherals/wb_uart.sv b/hw/rtl/peripherals/wb_uart.sv new file mode 100644 index 0000000..71dd934 --- /dev/null +++ b/hw/rtl/peripherals/wb_uart.sv @@ -0,0 +1,92 @@ +// 8-N-1 UART at 0x4000_0000. DIVIDER is system clocks per serial bit; +// reset divisor is rounded from CLK_HZ/115200 (minimum four clocks). +module wb_uart #( + parameter integer CLK_HZ=50_000_000, + parameter logic [31:0] BASE_ADDR=32'h4000_0000, + parameter integer DEFAULT_DIV=(CLK_HZ/115200 < 4 ? 4 : (CLK_HZ+57600)/115200) +) ( + input logic clk, rst_n, + input logic cyc, stb, we, + input logic [31:0] adr, dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack, err, + output logic uart_tx, + input logic uart_rx, + output logic irq_uart +); + localparam logic [1:0] RX_IDLE=0,RX_START=1,RX_DATA=2,RX_STOP=3; + logic [31:0] divider, response, saved_adr, saved_data; + logic [3:0] saved_sel; + logic saved_we,pending,response_err,accept; + logic [7:0] tx_shift,rx_shift,rx_data; + logic tx_busy,rx_valid,rx_overrun,tx_enable,rx_enable,tx_irq_enable,rx_irq_enable; + logic [3:0] tx_bit; + logic [1:0] rx_state; + logic [3:0] rx_bit; + logic [31:0] tx_count,rx_count; + logic rx_meta,rx_sync; + function automatic [31:0] merge_bytes(input [31:0] old_v,input [31:0] new_v,input [3:0] lanes); + integer b; begin merge_bytes=old_v; for(b=0;b<4;b=b+1) if(lanes[b]) merge_bytes[b*8 +: 8]=new_v[b*8 +: 8]; end + endfunction + assign accept=cyc&&stb&&(!pending||({we,adr,dat_w,sel}!={saved_we,saved_adr,saved_data,saved_sel})); + assign dat_r=response; assign ack=pending&&!response_err&&cyc&&stb; assign err=pending&&response_err&&cyc&&stb; + assign irq_uart=(rx_irq_enable&&rx_valid)||(tx_irq_enable&&!tx_busy); + always_ff @(posedge clk or negedge rst_n) begin + if(!rst_n) begin rx_meta<=1;rx_sync<=1; end + else begin rx_meta<=uart_rx;rx_sync<=rx_meta; end + end + always_ff @(posedge clk or negedge rst_n) begin + if(!rst_n) begin + divider<=DEFAULT_DIV;response<=0;saved_adr<=0;saved_data<=0;saved_sel<=0;saved_we<=0;pending<=0;response_err<=0; + uart_tx<=1;tx_shift<=0;tx_busy<=0;tx_bit<=0;tx_count<=0; + rx_state<=RX_IDLE;rx_bit<=0;rx_count<=0;rx_shift<=0;rx_data<=0;rx_valid<=0;rx_overrun<=0; + tx_enable<=1;rx_enable<=1;tx_irq_enable<=0;rx_irq_enable<=0; + end else begin + if(!accept&&pending) pending<=0; + else if(accept) begin + pending<=1;saved_adr<=adr;saved_data<=dat_w;saved_sel<=sel;saved_we<=we;response<=0; + response_err <= (sel==0)||(adr[1:0]!=0)||(adr[31:12]!=BASE_ADDR[31:12])|| + !((adr[11:0]==0)||(adr[11:0]==4)||(adr[11:0]==8)||(adr[11:0]==12)||(adr[11:0]==16)); + if(adr[31:12]==BASE_ADDR[31:12]&&adr[1:0]==0&&sel!=0) case(adr[11:0]) + 12'h000: begin response<={24'b0,rx_data}; if(we) begin + if(tx_enable&&!tx_busy) begin tx_shift<=dat_w[7:0];tx_busy<=1;tx_bit<=0;tx_count<=0;uart_tx<=0; end + else response_err<=1; + end else if(sel[0]) begin rx_valid<=0;rx_overrun<=0;end end + 12'h004: begin response<={24'b0,rx_data}; if(!we&&sel[0]) begin rx_valid<=0;rx_overrun<=0;end else if(we) response_err<=1; end + 12'h008: begin response<={29'b0,rx_overrun,rx_valid,tx_busy}; if(we) response_err<=1; end + 12'h00c: begin response<={28'b0,rx_irq_enable,tx_irq_enable,rx_enable,tx_enable}; if(we) begin + if(sel[0]) begin tx_enable<=dat_w[0];rx_enable<=dat_w[1];tx_irq_enable<=dat_w[2];rx_irq_enable<=dat_w[3];end + end end + 12'h010: begin response<=divider; if(we) divider<=merge_bytes(divider,dat_w,sel); end + default: begin end + endcase + end + if(tx_busy) begin + if(tx_count+1>=divider) begin + tx_count<=0; + if(tx_bit==0) begin uart_tx<=tx_shift[0];tx_bit<=1; end + else if(tx_bit<8) begin uart_tx<=tx_shift[tx_bit];tx_bit<=tx_bit+1'b1; end + else if(tx_bit==8) begin uart_tx<=1;tx_bit<=9; end + else begin tx_busy<=0;tx_bit<=0; end + end else tx_count<=tx_count+1'b1; + end + case(rx_state) + RX_IDLE: if(rx_enable&&!rx_sync) begin rx_state<=RX_START;rx_count<=divider>>1;rx_bit<=0; end + default: if(rx_count!=0) rx_count<=rx_count-1'b1; + else case(rx_state) + RX_START: if(!rx_sync) begin rx_state<=RX_DATA;rx_count<=divider-1'b1;rx_bit<=0; end else rx_state<=RX_IDLE; + RX_DATA: begin + rx_shift<={rx_sync,rx_shift[7:1]};rx_count<=divider-1'b1; + if(rx_bit==7) begin rx_state<=RX_STOP;rx_bit<=0;end else rx_bit<=rx_bit+1'b1; + end + RX_STOP: begin + if(rx_sync) begin if(rx_valid) rx_overrun<=1;rx_data<=rx_shift;rx_valid<=1;end + rx_state<=RX_IDLE; + end + default: rx_state<=RX_IDLE; + endcase + endcase + end + end +endmodule diff --git a/hw/rtl/peripherals/wb_watchdog.sv b/hw/rtl/peripherals/wb_watchdog.sv new file mode 100644 index 0000000..487d8d0 --- /dev/null +++ b/hw/rtl/peripherals/wb_watchdog.sv @@ -0,0 +1,51 @@ +// Watchdog at 0x4000_D000. Servicing requires 0x51F15EED at SERVICE. +module wb_watchdog #( + parameter logic [31:0] BASE_ADDR = 32'h4000_d000, + parameter logic [31:0] SERVICE_KEY = 32'h51f1_5eed +) ( + input logic clk, rst_n, + input logic cyc, stb, we, + input logic [31:0] adr, dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack, err, + output logic irq_watchdog, + output logic reset_request +); + logic enabled, pending, response_err; + logic [31:0] timeout_cycles, count, response; + logic [31:0] saved_adr, saved_data; + logic [3:0] saved_sel; + logic saved_we, accept; + function automatic [31:0] merge_bytes(input [31:0] old_v,input [31:0] new_v,input [3:0] lanes); + integer b; begin merge_bytes=old_v; for (b=0;b<4;b=b+1) if(lanes[b]) merge_bytes[b*8 +: 8]=new_v[b*8 +: 8]; end + endfunction + assign accept=cyc&&stb&&(!pending||({we,adr,dat_w,sel}!={saved_we,saved_adr,saved_data,saved_sel})); + assign dat_r=response; assign ack=pending&&!response_err&&cyc&&stb; assign err=pending&&response_err&&cyc&&stb; + assign irq_watchdog=reset_request; + always_ff @(posedge clk or negedge rst_n) begin + if(!rst_n) begin enabled<=0;timeout_cycles<=0;count<=0;reset_request<=0;pending<=0;response_err<=0;response<=0;saved_adr<=0;saved_data<=0;saved_sel<=0;saved_we<=0;end + else begin + if(!accept&&pending) pending<=0; + else if(accept) begin + pending<=1;saved_adr<=adr;saved_data<=dat_w;saved_sel<=sel;saved_we<=we;response<=0; + response_err <= (sel==0)||(adr[1:0]!=0)||(adr[31:12]!=BASE_ADDR[31:12])|| + !((adr[11:0]==0)||(adr[11:0]==4)||(adr[11:0]==8)||(adr[11:0]==12)); + if(adr[31:12]==BASE_ADDR[31:12]&&adr[1:0]==0&&sel!=0) case(adr[11:0]) + 12'h000: begin response<={31'b0,enabled}; if(we&&sel[0]) begin enabled<=dat_w[0];count<=timeout_cycles;reset_request<=0;end end + 12'h004: begin response<=timeout_cycles; if(we) timeout_cycles<=merge_bytes(timeout_cycles,dat_w,sel); end + 12'h008: begin response<=count; if(we) response_err<=1; end + 12'h00c: begin response<=0; if(!we) response_err<=1; else begin + if(sel==4'hf&&dat_w==SERVICE_KEY) begin count<=timeout_cycles;reset_request<=0;end + else reset_request<=1; + end end + default: begin end + endcase + end + if(enabled&&!reset_request&&!(accept&&we&&(adr[11:0]==0||adr[11:0]==12'h00c))) begin + if(count<=1) begin count<=0;reset_request<=1;end + else count<=count-1'b1; + end + end + end +endmodule diff --git a/hw/tb/boot_image.hex b/hw/tb/boot_image.hex new file mode 100644 index 0000000..1c7885c --- /dev/null +++ b/hw/tb/boot_image.hex @@ -0,0 +1,15 @@ +00010001 +02a00093 +10008137 +ffc10113 +00112023 +00012283 +100001b7 +0011a023 +0001a203 +001180a3 +0011c303 +ffe00393 +00719123 +00219403 +0000006f diff --git a/hw/tb/cpu_tb.sv b/hw/tb/cpu_tb.sv index 446f917..29378e3 100644 --- a/hw/tb/cpu_tb.sv +++ b/hw/tb/cpu_tb.sv @@ -15,7 +15,7 @@ module cpu_tb; .dmem_addr(dmem_addr), .dmem_wdata(dmem_wdata), .dmem_wstrb(dmem_wstrb), .dmem_ready(dmem_ready), .dmem_rdata(dmem_rdata), .dmem_error(dmem_error), .trap_valid(trap_valid), .trap_pc(trap_pc), - .trap_cause(trap_cause), .trap_value(trap_value) + .trap_cause(trap_cause), .trap_value(trap_value), .retire() ); initial begin diff --git a/hw/tb/memory_subsystem_tb.sv b/hw/tb/memory_subsystem_tb.sv new file mode 100644 index 0000000..891b1aa --- /dev/null +++ b/hw/tb/memory_subsystem_tb.sv @@ -0,0 +1,87 @@ +`timescale 1ns/1ps +module memory_subsystem_tb; + logic clk=0, rst_n=0; + always #5 clk=~clk; + logic [31:0] pc, instr; + logic dv,dw,dr,de,cyc,stb,we,ack,err; + logic [31:0] da,dwd,drd,ba,bwd,brd; + logic [3:0] ds,sel; + logic acyc,astb,awe; + logic [31:0] aba,abwd; + logic [3:0] asel; + logic manual_bus; + wire mcyc = manual_bus ? cyc : acyc; + wire mstb = manual_bus ? stb : astb; + wire mwe = manual_bus ? we : awe; + wire [31:0] mba = manual_bus ? ba : aba; + wire [31:0] mbwd = manual_bus ? bwd : abwd; + wire [3:0] msel = manual_bus ? sel : asel; + logic trap_valid; + logic [31:0] trap_pc,trap_cause,trap_value; + cpu_core cpu(.clk,.rst_n,.pc,.instr,.irq_external(1'b0),.irq_timer(1'b0), + .dmem_valid(dv),.dmem_write(dw),.dmem_addr(da),.dmem_wdata(dwd),.dmem_wstrb(ds), + .dmem_ready(dr),.dmem_rdata(drd),.dmem_error(de),.trap_valid,.trap_pc,.trap_cause,.trap_value,.retire()); + wb_cpu_adapter adapter(.clk,.rst_n,.cpu_valid(dv),.cpu_write(dw),.cpu_addr(da),.cpu_wdata(dwd),.cpu_wstrb(ds), + .cpu_ready(dr),.cpu_rdata(drd),.cpu_error(de),.wb_cyc(acyc),.wb_stb(astb),.wb_we(awe),.wb_adr(aba),.wb_dat_w(abwd),.wb_sel(asel),.wb_dat_r(brd),.wb_ack(ack),.wb_err(err)); + memory_subsystem #(.ROM_INIT_FILE("hw/tb/boot_image.hex")) mem(.clk,.rst_n,.fetch_addr(pc),.fetch_data(instr), + .cyc(mcyc),.stb(mstb),.we(mwe),.adr(mba),.dat_w(mbwd),.sel(msel),.dat_r(brd),.ack,.err); + task automatic wb_access(input logic wr,input logic [31:0] a,input logic [31:0] d,input logic [3:0] s, + input logic expect_err,input logic [31:0] expected); + integer timeout; + begin + @(negedge clk); cyc=1; stb=1; we=wr; ba=a; bwd=d; sel=s; timeout=0; + while (!ack && !err && timeout<5) begin @(posedge clk); #1; timeout=timeout+1; end + if (timeout==5) $fatal(1,"memory response timeout at %h",a); + if (err !== expect_err) $fatal(1,"wrong error at %h",a); + if (!wr && !expect_err && brd !== expected) $fatal(1,"read %h got %h expected %h",a,brd,expected); + @(negedge clk); cyc=0; stb=0; we=0; + end + endtask + task automatic wb_back_to_back_writes; + begin + @(negedge clk); cyc=1; stb=1; we=1; ba=32'h1000_0010; bwd=32'h1234_5678; sel=4'hf; + @(posedge clk); #1; if (!ack || err) $fatal(1,"first back-to-back write failed"); + @(negedge clk); ba=32'h1000_0014; bwd=32'h89ab_cdef; + @(posedge clk); #1; if (!ack || err) $fatal(1,"second back-to-back write failed"); + @(negedge clk); cyc=0; stb=0; we=0; + wb_access(0,32'h1000_0010,0,4'hf,0,32'h1234_5678); + wb_access(0,32'h1000_0014,0,4'hf,0,32'h89ab_cdef); + end + endtask + initial begin + cyc=0; stb=0; we=0; ba=0; bwd=0; sel=0; + manual_bus=0; + repeat(3) @(negedge clk); rst_n=1; + repeat(40) @(posedge clk); + if (cpu.rf.regs[4] !== 42 || cpu.rf.regs[5] !== 42 || cpu.rf.regs[6] !== 42 || + cpu.rf.regs[8] !== 32'hffff_fffe) + $fatal(1,"ROM program did not verify SRAM stack/data/subword access: x4=%h x5=%h x6=%h x8=%h", + cpu.rf.regs[4],cpu.rf.regs[5],cpu.rf.regs[6],cpu.rf.regs[8]); + if (trap_valid) $fatal(1,"unexpected CPU trap %h",trap_cause); + manual_bus=1; + wb_access(0,32'h1000_0000,0,4'hf,0,32'hfffe_2a2a); + wb_access(1,32'h1000_7ffc,32'b0,4'hf,0,0); + wb_access(1,32'h1000_7fff,32'hAA00_0000,4'h8,0,0); + wb_access(0,32'h1000_7ffc,0,4'hf,0,32'hAA00_0000); + wb_access(1,32'h1000_7ffc,32'h0000_55cc,4'h3,0,0); + wb_access(0,32'h1000_7ffc,0,4'hf,0,32'hAA00_55cc); + wb_access(0,32'h0000_0ffc,0,4'hf,0,32'h0000_0013); + wb_access(1,32'h0000_0000,0,4'hf,1,0); + wb_access(0,32'h1000_8000,0,4'hf,1,0); + wb_access(0,32'h2000_0000,0,4'hf,1,0); + wb_access(0,32'h1000_0000,0,0,1,0); + wb_back_to_back_writes(); + // Reset while a response is pending: cancel the response, retain SRAM. + @(negedge clk); cyc=1; stb=1; we=0; ba=32'h1000_0010; sel=4'hf; + @(posedge clk); #1; if (!ack) $fatal(1,"reset-abort setup failed"); + @(negedge clk); rst_n=0; #1; + if (ack || err) $fatal(1,"reset failed to clear pending response"); + cyc=0; stb=0; + repeat(2) @(negedge clk); + rst_n=1; + repeat(2) @(negedge clk); + if (ack || err) $fatal(1,"stale response after reset"); + wb_access(0,32'h1000_0010,0,4'hf,0,32'h1234_5678); + $display("memory_subsystem_tb PASS"); $finish; + end +endmodule diff --git a/hw/tb/peripheral_cpu_irq_tb.sv b/hw/tb/peripheral_cpu_irq_tb.sv new file mode 100644 index 0000000..08fa478 --- /dev/null +++ b/hw/tb/peripheral_cpu_irq_tb.sv @@ -0,0 +1,64 @@ +`timescale 1ns/1ps +module peripheral_cpu_irq_tb; + logic clk=0,rst_n=0; + always #5 clk=~clk; + logic [31:0] pc,instr; + logic dvalid,dwrite,dready,derror,trap_valid,retire; + logic [31:0] daddr,dwdata,drdata,trap_pc,trap_cause,trap_value; + logic [3:0] dwstrb; + logic wb_cyc,wb_stb,wb_we,wb_ack,wb_err; + logic [31:0] wb_adr,wb_wdata,wb_rdata; + logic [3:0] wb_sel; + logic p_ack,p_err,m_ack,m_err; + logic [31:0] p_rdata,m_rdata; + logic uart_tx; + logic irq_timer,irq_external,wd_reset; + logic [31:0] gpio_in=0,gpio_out,gpio_oe; + wire psel=wb_adr[31:16]==16'h4000; + logic timer_trap_seen=0; + logic [7:0] received_text [0:1]; + integer received_count=0; + cpu_core cpu(.clk,.rst_n,.pc,.instr,.irq_external,.irq_timer,.dmem_valid(dvalid),.dmem_write(dwrite), + .dmem_addr(daddr),.dmem_wdata(dwdata),.dmem_wstrb(dwstrb),.dmem_ready(dready),.dmem_rdata(drdata),.dmem_error(derror), + .trap_valid,.trap_pc,.trap_cause,.trap_value,.retire); + wb_cpu_adapter adapter(.clk,.rst_n,.cpu_valid(dvalid),.cpu_write(dwrite),.cpu_addr(daddr),.cpu_wdata(dwdata),.cpu_wstrb(dwstrb), + .cpu_ready(dready),.cpu_rdata(drdata),.cpu_error(derror),.wb_cyc,.wb_stb,.wb_we,.wb_adr,.wb_dat_w(wb_wdata),.wb_sel, + .wb_dat_r(wb_rdata),.wb_ack,.wb_err); + memory_subsystem #(.ROM_INIT_FILE("hw/tb/peripheral_irq_boot.hex")) mem(.clk,.rst_n,.fetch_addr(pc),.fetch_data(instr), + .cyc(wb_cyc&&!psel),.stb(wb_stb&&!psel),.we(wb_we),.adr(wb_adr),.dat_w(wb_wdata),.sel(wb_sel), + .dat_r(m_rdata),.ack(m_ack),.err(m_err)); + peripheral_subsystem #(.CLK_HZ(921600)) periph(.clk,.rst_n,.cyc(wb_cyc&&psel),.stb(wb_stb&&psel),.we(wb_we), + .adr(wb_adr),.dat_w(wb_wdata),.sel(wb_sel),.dat_r(p_rdata),.ack(p_ack),.err(p_err),.uart_tx,.uart_rx(uart_tx), + .gpio_in,.gpio_out,.gpio_oe,.spi_sck(),.spi_cs_n(),.spi_mosi(),.spi_miso(1'b1),.i2c_scl_i(1'b1),.i2c_sda_i(1'b1), + .i2c_scl_o(),.i2c_scl_oe(),.i2c_sda_o(),.i2c_sda_oe(),.cpu_pc(pc),.cpu_retire(retire), + .irq_timer,.irq_external,.reset_request(wd_reset),.sleep_request(),.clock_enable_req(),.wake_status()); + assign wb_ack=psel?p_ack:m_ack; + assign wb_err=psel?p_err:m_err; + assign wb_rdata=psel?p_rdata:m_rdata; + always @(posedge clk) if(trap_valid&&trap_cause==32'h8000_0007) timer_trap_seen<=1; + always @(posedge clk) if(rst_n&&periph.uart.rx_state==2'd3&&periph.uart.rx_count==0&&periph.uart.rx_sync) begin + if(received_count<2) received_text[received_count]<=periph.uart.rx_shift; + received_count<=received_count+1; + end + integer cycles; + initial begin + repeat(3) @(negedge clk);rst_n=1; + cycles=0; + while(cpu.rf.regs[8]!==32'd1 && cycles<1000) begin @(posedge clk);#1;cycles=cycles+1; end + if(cpu.rf.regs[8]!==32'd1)$fatal(1,"CPU did not service timer interrupt; pc=%h mie=%h mstatus=%h",pc,cpu.mie,cpu.mstatus); + if(!timer_trap_seen)$fatal(1,"machine timer trap notification was not observed"); + if(received_count<2||received_text[0]!==8'h48||received_text[1]!==8'h69) + $fatal(1,"bare-metal UART text mismatch, received count=%0d bytes=%h %h",received_count,received_text[0],received_text[1]); + if(gpio_oe!==32'h1||gpio_out!==32'h1)$fatal(1,"bare-metal GPIO configuration failed"); + while(!wd_reset && cycles<1200) begin @(posedge clk);#1;cycles=cycles+1; end + if(!wd_reset)$fatal(1,"bare-metal watchdog did not request recovery"); + // Model the SoC reset controller consuming the sticky watchdog request. + @(negedge clk);rst_n=0; + repeat(3) @(negedge clk); + if(wd_reset)$fatal(1,"watchdog request did not clear under system reset"); + if(gpio_oe!==0||gpio_out!==0)$fatal(1,"peripheral reset did not restore GPIO reset state"); + rst_n=1;#1; + if(pc!==0)$fatal(1,"CPU did not restart from reset vector after watchdog recovery"); + $display("peripheral_cpu_irq_tb PASS (%0d cycles, timer trap, UART, GPIO, watchdog recovery)",cycles);$finish; + end +endmodule diff --git a/hw/tb/peripheral_irq_boot.hex b/hw/tb/peripheral_irq_boot.hex new file mode 100644 index 0000000..edb37df --- /dev/null +++ b/hw/tb/peripheral_irq_boot.hex @@ -0,0 +1,42 @@ +40000537 +04800593 +00b52023 +00852603 +00167613 +fe061ce3 +06900593 +00b52023 +400016b7 +00100713 +00e6a223 +00e6a023 +40004137 +00000193 +00312623 +12c00193 +00312423 +40005237 +00100493 +00922223 +08800313 +30531073 +08000293 +30429073 +00800393 +30039073 +0000006f +00000013 +00000013 +00000013 +00000013 +00000013 +00000013 +00000013 +30401073 +00140413 +4000d7b7 +02800813 +0107a223 +00100813 +0107a023 +30200073 diff --git a/hw/tb/peripheral_subsystem_tb.sv b/hw/tb/peripheral_subsystem_tb.sv new file mode 100644 index 0000000..1281bc5 --- /dev/null +++ b/hw/tb/peripheral_subsystem_tb.sv @@ -0,0 +1,224 @@ +`timescale 1ns/1ps +module peripheral_subsystem_tb; + logic clk=0,rst_n=0; + always #5 clk=~clk; + logic cyc,stb,we; + logic [31:0] adr,dat_w,dat_r; + logic [3:0] sel; + logic ack,err,uart_tx; + logic [31:0] gpio_in; + wire [31:0] gpio_out,gpio_oe; + wire irq_timer,irq_external,reset_request; + wire uart_rx=uart_tx; + logic spi_miso=1; + wire spi_sck,spi_cs_n,spi_mosi; + wire i2c_scl_oe,i2c_sda_oe,i2c_scl_o,i2c_sda_o; + wire i2c_scl_i=!i2c_scl_oe; + wire i2c_sda_i=!i2c_sda_oe; + logic [31:0] cpu_pc=0; + logic cpu_retire=0; + logic i2c_ack_seen=0,i2c_nack_seen=0; + wire sleep_request; + wire [4:0] clock_enable_req; + wire [5:0] wake_status; + always @(posedge clk) if(dut.i2c.state==4'd7&&dut.i2c.i2c_read) begin + if(dut.i2c.read_nack) begin if(i2c_sda_oe!==0)$fatal(1,"I2C NACK must release SDA");i2c_nack_seen<=1;end + else begin if(i2c_sda_oe!==1)$fatal(1,"I2C ACK must pull SDA low");i2c_ack_seen<=1;end + end + peripheral_subsystem #(.CLK_HZ(921600)) dut(.clk,.rst_n,.cyc,.stb,.we,.adr,.dat_w,.sel,.dat_r,.ack,.err, + .uart_tx,.uart_rx,.gpio_in,.gpio_out,.gpio_oe,.spi_sck,.spi_cs_n,.spi_mosi,.spi_miso,.i2c_scl_i,.i2c_sda_i, + .i2c_scl_o,.i2c_scl_oe,.i2c_sda_o,.i2c_sda_oe,.cpu_pc,.cpu_retire,.irq_timer,.irq_external,.reset_request, + .sleep_request,.clock_enable_req,.wake_status); + + task automatic access(input logic write_en,input logic [31:0] address,input logic [31:0] value, + input logic [3:0] lanes,input logic expect_error,input logic [31:0] expected); + integer n; + begin + @(negedge clk);cyc=1;stb=1;we=write_en;adr=address;dat_w=value;sel=lanes;n=0; + while(!ack&&!err&&n<5) begin @(posedge clk);#1;n=n+1;end + if(n==5)$fatal(1,"timeout at %h",address); + if(err!==expect_error)$fatal(1,"error mismatch at %h err=%b expected=%b",address,err,expect_error); + if(!write_en&&!expect_error&&expected!=32'hdead_beef&&dat_r!==expected) + $fatal(1,"read %h=%h expected %h",address,dat_r,expected); + @(negedge clk);cyc=0;stb=0;we=0; + end + endtask + task automatic wait_clocks(input integer n); + integer i; begin for(i=0;i>b)&1'b1))$fatal(1,"UART TX data bit %0d incorrect",b); + end + repeat(8) @(posedge clk); #1; + if(uart_tx!==1)$fatal(1,"UART stop bit missing"); + wait_clocks(20); + access(0,32'h4000_0008,0,4'hf,0,32'h2); // RX valid + access(0,32'h4000_0004,0,4'hf,0,32'ha5); // receive data clears valid + access(1,32'h4000_0008,0,4'hf,1,0); // STATUS is read-only + + // GPIO direction/output, synchronized input, rising-edge interrupt. + access(1,32'h4000_1004,32'h1,4'hf,0,0); + access(1,32'h4000_1000,32'h1,4'hf,0,0); + if(gpio_out!==1||gpio_oe!==1)$fatal(1,"GPIO output/direction failed"); + access(1,32'h4000_100c,32'h2,4'hf,0,0); // edge mode on pin 1 + access(1,32'h4000_1010,32'hffff_ffff,4'hf,0,0); // high level / rising edge polarity + access(1,32'h4000_1008,32'h2,4'hf,0,0); // enable IRQ pin 1 + access(0,32'h4000_1004,0,4'hf,0,1); + access(0,32'h4000_1008,0,4'hf,0,2); + access(0,32'h4000_100c,0,4'hf,0,2); + access(0,32'h4000_1010,0,4'hf,0,32'hffff_ffff); + gpio_in[1]=1; + wait_clocks(6); + access(0,32'h4000_1000,0,4'hf,0,32'h2); + access(0,32'h4000_1014,0,4'hf,0,32'h2); + + // SPI master modes 0-3, byte transfer, chip select and RX path. + access(1,32'h4000_2008,2,4'hf,0,0); + access(0,32'h4000_2008,0,4'hf,0,2); + for(b=0;b<4;b=b+1)begin + access(1,32'h4000_2004,32'h1|(((b>>1)&1)<<1)|((b&1)<<2),4'hf,0,0); + access(1,32'h4000_2000,32'ha5,4'h1,0,0); + wait_clocks(40); + if(spi_cs_n!==1||spi_sck!==((b>>1)&1'b1))$fatal(1,"SPI mode %0d did not return to idle",b); + access(0,32'h4000_200c,0,4'hf,0,32'h2); + access(0,32'h4000_2000,0,4'hf,0,32'hff); + end + + // I2C open-drain START, byte write/NACK, byte read/NACK, and STOP. + access(1,32'h4000_3008,1,4'h1,0,0); + access(1,32'h4000_300c,2,4'hf,0,0); + access(0,32'h4000_3008,0,4'hf,0,1); + access(0,32'h4000_300c,0,4'hf,0,2); + access(1,32'h4000_3004,1,4'h1,0,0); + wait_clocks(5); + if(i2c_sda_oe!==1||i2c_scl_oe!==1)$fatal(1,"I2C START waveform invalid"); + access(1,32'h4000_3000,8'ha0,4'h1,0,0); + access(1,32'h4000_3004,4,4'h1,0,0); + wait_clocks(45); + access(0,32'h4000_3010,0,4'hf,0,32'h6); // done + NACK (no target on bus) + access(1,32'h4000_3004,8'h08,4'h1,0,0); // read byte and ACK + wait_clocks(45); + access(0,32'h4000_3000,0,4'h1,0,32'hff); + if(!i2c_ack_seen)$fatal(1,"I2C read ACK waveform not observed"); + access(1,32'h4000_3004,8'h18,4'h1,0,0); // read byte and NACK + wait_clocks(45); + access(0,32'h4000_3000,0,4'h1,0,32'hff); + if(!i2c_nack_seen)$fatal(1,"I2C read NACK waveform not observed"); + access(1,32'h4000_3004,2,4'h1,0,0); + wait_clocks(5); + if(i2c_scl_oe!==0||i2c_sda_oe!==0)$fatal(1,"I2C STOP did not release open-drain lines"); + + // Debug/performance counters and sampled retirement PC. + cpu_pc=32'h1234_5678;cpu_retire=1;wait_clocks(1);cpu_retire=0; + access(0,32'h4000_a000,0,4'hf,0,1); + access(0,32'h4000_a00c,0,4'hf,0,32'h1); + access(0,32'h4000_a010,0,4'hf,0,0); + access(0,32'h4000_a01c,0,4'hf,0,32'h1234_5678); + access(0,32'h4000_a014,0,4'hf,0,32'hdead_beef); + if(dat_r==0)$fatal(1,"debug bus counter did not increment: read=%0d internal=%0d event=%b",dat_r,dut.debug_regs.bus_count,dut.bus_event); + access(0,32'h4000_a018,0,4'hf,0,32'hdead_beef); + access(0,32'h4000_a004,0,4'hf,0,32'hdead_beef); + if(dat_r==0)$fatal(1,"debug cycle counter did not increment"); + access(0,32'h4000_a008,0,4'hf,0,0); + access(1,32'h4000_a01c,0,4'hf,1,0); // sampled PC is read-only + + // Clock requests and interrupt-driven sleep/wake behavior. + access(0,32'h4000_b004,0,4'hf,0,32'h1f); + access(1,32'h4000_b004,32'h15,4'hf,0,0); + access(0,32'h4000_b004,0,4'hf,0,32'h15); + access(1,32'h4000_b008,32'h21,4'hf,0,0); // timer and software wake + access(0,32'h4000_b008,0,4'hf,0,32'h21); + access(1,32'h4000_b000,1,4'h1,0,0); + if(!sleep_request)$fatal(1,"sleep request was not set"); + + // Interrupt map, threshold masking, default priorities, claim/complete. + access(1,32'h4000_5004,32'hf,4'hf,0,0); + access(1,32'h4000_5000,1,4'hf,1,0); // PENDING is read-only + access(0,32'h4000_5000,0,4'hf,0,32'h6); // UART/GPIO pending sources + access(1,32'h4000_5008,3,4'hf,0,0); + #1;if(irq_external)$fatal(1,"threshold must mask priority 3"); + access(1,32'h4000_5008,2,4'hf,0,0); + #1;if(!irq_external)$fatal(1,"enabled pending external IRQ not asserted"); + access(0,32'h4000_5010,0,4'hf,0,3); + access(0,32'h4000_5014,0,4'hf,0,2); + access(0,32'h4000_5018,0,4'hf,0,1); + access(0,32'h4000_500c,0,4'hf,0,1); // UART has highest priority + access(1,32'h4000_500c,1,4'h1,0,0); + access(1,32'h4000_1014,2,4'hf,0,0); + access(0,32'h4000_5000,0,4'hf,0,32'h4); // GPIO stays latched until completion + access(1,32'h4000_500c,2,4'h1,0,0); + access(0,32'h4000_5000,0,4'hf,0,0); + + // Timer mtime/mtimecmp and machine timer interrupt routing. + access(0,32'h4000_4000,0,4'hf,0,32'hdead_beef); + access(0,32'h4000_4000,0,4'hf,0,32'hdead_beef); + saved_mtime=dat_r; + access(1,32'h4000_400c,0,4'hf,0,0); + access(1,32'h4000_4008,saved_mtime+30,4'hf,0,0); + access(1,32'h4000_4010,1,4'h1,0,0); + access(1,32'h4000_5004,32'hf,4'hf,0,0); + access(1,32'h4000_5008,0,4'hf,0,0); + wait_clocks(40); + if(!irq_timer)$fatal(1,"mtimecmp did not assert machine timer IRQ"); + if(sleep_request||!wake_status[0])$fatal(1,"timer did not wake sleep request"); + access(1,32'h4000_4010,0,4'h1,0,0); // remove level source before clearing latched wake + access(1,32'h4000_b00c,1,4'h1,0,0); + access(0,32'h4000_b00c,0,4'hf,0,0); + access(1,32'h4000_b000,1,4'h1,0,0); + access(1,32'h4000_b014,1,4'h1,0,0); + if(sleep_request||!wake_status[5])$fatal(1,"software wake event not latched"); + access(1,32'h4000_b00c,32'h20,4'h1,0,0); + access(0,32'h4000_4000,0,4'hf,0,32'hdead_beef); + if(dat_r==0)$fatal(1,"mtime did not increment"); + access(0,32'h4000_4004,0,4'hf,0,32'hdead_beef); + + // Software reset request, consumed by the system reset controller. + access(1,32'h4000_b010,32'h52535401,4'hf,0,0); + if(!reset_request)$fatal(1,"software reset request not asserted"); + @(negedge clk);rst_n=0;repeat(3)@(negedge clk); + if(reset_request||clock_enable_req!==5'h1f)$fatal(1,"power/reset state failed reset"); + rst_n=1; + + // Watchdog programming, authenticated service and expiry/reset request. + access(1,32'h4000_5004,8,4'hf,0,0); // enable watchdog's external interrupt source + access(1,32'h4000_d004,50,4'hf,0,0); + access(0,32'h4000_d004,0,4'hf,0,50); + access(1,32'h4000_d008,1,4'hf,1,0); // COUNT is read-only + access(0,32'h4000_d00c,0,4'hf,1,0); // SERVICE is write-only + access(1,32'h4000_d000,1,4'h1,0,0); + wait_clocks(20); + access(1,32'h4000_d00c,32'h51f15eed,4'hf,0,0); + wait_clocks(30); + if(reset_request)$fatal(1,"valid watchdog service did not reload timeout"); + wait_clocks(30); + if(!reset_request||!irq_external)$fatal(1,"watchdog expiry did not request reset/interrupt"); + access(0,32'h4000_d008,0,4'hf,0,0); + + // Bad/unmapped accesses and invalid selects terminate with ERR. + access(0,32'h4000_0020,0,4'hf,1,0); + access(0,32'h4000_0000,0,0,1,0); + access(0,32'h4000_2020,0,4'hf,1,0); + access(0,32'h4000_3020,0,4'hf,1,0); + access(0,32'h4000_a020,0,4'hf,1,0); + access(0,32'h4000_b018,0,4'hf,1,0); + access(0,32'h5000_0000,0,4'hf,1,0); + $display("peripheral_subsystem_tb PASS");$finish; + end +endmodule diff --git a/hw/tb/wb_cpu_adapter_tb.sv b/hw/tb/wb_cpu_adapter_tb.sv new file mode 100644 index 0000000..d047ecf --- /dev/null +++ b/hw/tb/wb_cpu_adapter_tb.sv @@ -0,0 +1,54 @@ +`timescale 1ns/1ps +module wb_cpu_adapter_tb; + logic clk=0, rst_n=0; + always #5 clk=~clk; + logic cpu_valid, cpu_write; + logic [31:0] cpu_addr, cpu_wdata; + logic [3:0] cpu_wstrb; + wire cpu_ready, cpu_error; + wire [31:0] cpu_rdata; + wire wb_cyc, wb_stb, wb_we; + wire [31:0] wb_adr, wb_dat_w; + wire [3:0] wb_sel; + logic [31:0] wb_dat_r; + logic wb_ack, wb_err; + + wb_cpu_adapter dut (.*); + + initial begin + cpu_valid=0; cpu_write=0; cpu_addr=0; cpu_wdata=0; cpu_wstrb=0; + wb_dat_r=0; wb_ack=0; wb_err=0; + repeat(2) @(posedge clk); rst_n=1; + + @(negedge clk); cpu_valid=1; cpu_addr=32'h1000_0003; cpu_wdata=32'h1234_5678; cpu_wstrb=0; + @(posedge clk); #1; + if (!wb_cyc || !wb_stb || wb_we || wb_adr!=32'h1000_0000 || wb_sel!=4'hf) + $fatal(1,"CPU read request was not correctly adapted"); + repeat(2) begin + @(posedge clk); #1; + if (cpu_ready || !wb_cyc || wb_adr!=32'h1000_0000) $fatal(1,"read was not held under backpressure"); + end + wb_dat_r=32'hCAFE_BABE; wb_ack=1; #1; + if (!cpu_ready || cpu_error || cpu_rdata!=32'hCAFE_BABE) $fatal(1,"ACK response mapping failed"); + @(posedge clk); #1; wb_ack=0; cpu_valid=0; + if (wb_cyc) $fatal(1,"adapter did not retire ACKed transfer"); + + @(negedge clk); cpu_valid=1; cpu_write=1; cpu_addr=32'h1000_0007; + cpu_wdata=32'hAABB_CCDD; cpu_wstrb=4'b1000; + @(posedge clk); #1; + if (!wb_cyc || !wb_we || wb_adr!=32'h1000_0004 || wb_sel!=4'b1000 || wb_dat_w!=32'hAABB_CCDD) + $fatal(1,"CPU write request was not correctly adapted"); + wb_err=1; #1; + if (!cpu_ready || !cpu_error) $fatal(1,"ERR response mapping failed"); + @(posedge clk); #1; wb_err=0; cpu_valid=0; + if (wb_cyc) $fatal(1,"adapter did not retire ERR response"); + + @(negedge clk); cpu_valid=1; cpu_write=0; cpu_addr=32'h0000_0020; + @(posedge clk); #1; + if (!wb_cyc) $fatal(1,"third transfer did not start"); + @(negedge clk); rst_n=0; #1; + if (wb_cyc || wb_stb) $fatal(1,"reset did not abort adapter transfer"); + $display("Wishbone CPU adapter tests passed"); + $finish; + end +endmodule diff --git a/hw/tb/wb_interconnect_tb.sv b/hw/tb/wb_interconnect_tb.sv new file mode 100644 index 0000000..33a3965 --- /dev/null +++ b/hw/tb/wb_interconnect_tb.sv @@ -0,0 +1,164 @@ +`timescale 1ns/1ps +module wb_tb_slave #( + parameter integer LATENCY = 1 +) ( + input logic clk, rst_n, stall, + input logic cyc, stb, we, + input logic [31:0] adr, dat_w, + input logic [3:0] sel, + output logic [31:0] dat_r, + output logic ack, err, + output integer write_count +); + logic [31:0] mem [0:15]; + logic [1:0] state; + integer delay_count; + logic [31:0] saved_adr, saved_data; + logic [3:0] saved_sel; + logic saved_we; + integer i; + always_ff @(posedge clk or negedge rst_n) begin + if (!rst_n) begin + state <= 0; delay_count <= 0; dat_r <= 0; ack <= 0; err <= 0; write_count <= 0; + saved_adr <= 0; saved_data <= 0; saved_sel <= 0; saved_we <= 0; + for (i=0;i<16;i=i+1) mem[i] <= 0; + end else begin + ack <= 0; err <= 0; + case (state) + 0: if (cyc && stb) begin + saved_adr <= adr; saved_data <= dat_w; saved_sel <= sel; saved_we <= we; + delay_count <= LATENCY + adr[3:2]; + state <= 1; + end + 1: if (!cyc || !stb) state <= 0; + else if (!stall) begin + if (delay_count > 0) delay_count <= delay_count-1; + else begin + if (saved_we) begin + for (i=0;i<4;i=i+1) + if (saved_sel[i]) mem[saved_adr[5:2]][i*8 +: 8] <= saved_data[i*8 +: 8]; + write_count <= write_count+1; + end else dat_r <= mem[saved_adr[5:2]]; + if (saved_adr[11:0] == 12'h040) err <= 1; + else ack <= 1; + state <= 2; + end + end + 2: if (!cyc || !stb) state <= 0; + default: state <= 0; + endcase + end + end +endmodule + +module wb_interconnect_tb; + logic clk=0, rst_n=0; + always #5 clk=~clk; + logic [1:0] cyc, stb, we; + logic [63:0] adr, dat_w; + logic [7:0] sel; + wire [63:0] dat_r; + wire [1:0] ack, err; + wire [2:0] s_cyc, s_stb, s_we; + wire [95:0] s_adr, s_dat_w; + wire [11:0] s_sel; + wire [95:0] s_dat_r; + wire [2:0] s_ack, s_err; + logic stall_all; + integer writes_rom, writes_sram, writes_periph; + logic [31:0] got0, got1; + + wb_interconnect #(.TIMEOUT_CYCLES(12)) dut ( + .clk, .rst_n, + .m0_cyc(cyc[0]), .m0_stb(stb[0]), .m0_we(we[0]), .m0_adr(adr[31:0]), .m0_dat_w(dat_w[31:0]), .m0_sel(sel[3:0]), + .m0_dat_r(dat_r[31:0]), .m0_ack(ack[0]), .m0_err(err[0]), + .m1_cyc(cyc[1]), .m1_stb(stb[1]), .m1_we(we[1]), .m1_adr(adr[63:32]), .m1_dat_w(dat_w[63:32]), .m1_sel(sel[7:4]), + .m1_dat_r(dat_r[63:32]), .m1_ack(ack[1]), .m1_err(err[1]), + .s_cyc, .s_stb, .s_we, .s_adr, .s_dat_w, .s_sel, .s_dat_r, .s_ack, .s_err + ); + + wb_tb_slave #(.LATENCY(1)) rom ( + .clk,.rst_n,.stall(stall_all),.cyc(s_cyc[0]),.stb(s_stb[0]),.we(s_we[0]), + .adr(s_adr[31:0]),.dat_w(s_dat_w[31:0]),.sel(s_sel[3:0]),.dat_r(s_dat_r[31:0]), + .ack(s_ack[0]),.err(s_err[0]),.write_count(writes_rom)); + wb_tb_slave #(.LATENCY(2)) sram ( + .clk,.rst_n,.stall(stall_all),.cyc(s_cyc[1]),.stb(s_stb[1]),.we(s_we[1]), + .adr(s_adr[63:32]),.dat_w(s_dat_w[63:32]),.sel(s_sel[7:4]),.dat_r(s_dat_r[63:32]), + .ack(s_ack[1]),.err(s_err[1]),.write_count(writes_sram)); + wb_tb_slave #(.LATENCY(3)) periph ( + .clk,.rst_n,.stall(stall_all),.cyc(s_cyc[2]),.stb(s_stb[2]),.we(s_we[2]), + .adr(s_adr[95:64]),.dat_w(s_dat_w[95:64]),.sel(s_sel[11:8]),.dat_r(s_dat_r[95:64]), + .ack(s_ack[2]),.err(s_err[2]),.write_count(writes_periph)); + + task automatic start(input integer m, input logic wr, input logic [31:0] a, + input logic [31:0] wd, input logic [3:0] byte_sel); + begin + @(negedge clk); + cyc[m]=1; stb[m]=1; we[m]=wr; + adr[m*32 +: 32]=a; dat_w[m*32 +: 32]=wd; sel[m*4 +: 4]=byte_sel; + end + endtask + + task automatic finish(input integer m, input logic expect_error, output logic [31:0] result); + integer cycles; + begin + cycles=0; result=0; + while (!(ack[m] || err[m]) && cycles<40) begin @(posedge clk); cycles=cycles+1; end + if (cycles>=40) $fatal(1,"master %0d transfer timed out",m); + if ((err[m] === 1'b1) !== expect_error) $fatal(1,"master %0d unexpected ERR=%b",m,err[m]); + if (ack[m] && err[m]) $fatal(1,"ACK and ERR simultaneously asserted"); + result=dat_r[m*32 +: 32]; + @(negedge clk); cyc[m]=0; stb[m]=0; sel[m*4 +: 4]=0; + end + endtask + + initial begin + cyc=0; stb=0; we=0; adr=0; dat_w=0; sel=0; stall_all=0; + repeat(3) @(posedge clk); rst_n=1; + repeat(2) @(posedge clk); + + start(0,1,32'h0000_0008,32'h1122_3344,4'hf); finish(0,0,got0); + start(0,1,32'h1000_0008,32'hAABB_CCDD,4'hf); finish(0,0,got0); + start(0,1,32'h4000_0008,32'h5566_7788,4'hf); finish(0,0,got0); + start(0,1,32'h1000_0008,32'h0000_00EE,4'b0001); finish(0,0,got0); + start(0,0,32'h1000_0008,0,4'hf); finish(0,0,got0); + if (got0!==32'hAABB_CCEE) $fatal(1,"byte-select/readback mismatch: %h",got0); + + start(0,0,32'h0000_0FFC,0,4'hf); finish(0,0,got0); + start(0,0,32'h0000_1000,0,4'hf); finish(0,1,got0); + start(0,0,32'h1000_7FFC,0,4'hf); finish(0,0,got0); + start(0,0,32'h1000_8000,0,4'hf); finish(0,1,got0); + start(0,0,32'h4000_EFFC,0,4'hf); finish(0,0,got0); + start(0,0,32'h4001_0000,0,4'hf); finish(0,1,got0); + start(0,0,32'h1000_0000,0,4'b0); finish(0,1,got0); + + start(0,1,32'h1000_0010,32'h1111_1111,4'hf); + start(1,1,32'h1000_0014,32'h2222_2222,4'hf); + fork + finish(0,0,got0); + finish(1,0,got1); + join + if (writes_sram<4) $fatal(1,"lost or duplicated writes during contention"); + start(0,0,32'h1000_0010,0,4'hf); finish(0,0,got0); + if (got0!==32'h1111_1111) $fatal(1,"first contending transaction lost/reordered"); + start(0,0,32'h1000_0014,0,4'hf); finish(0,0,got0); + if (got0!==32'h2222_2222) $fatal(1,"second contending transaction lost/reordered"); + start(0,0,32'h1000_0040,0,4'hf); finish(0,1,got0); + + stall_all=1; + start(0,0,32'h0000_0000,0,4'hf); + repeat(4) @(posedge clk); + @(negedge clk); rst_n=0; cyc=0; stb=0; stall_all=0; + repeat(2) @(posedge clk); rst_n=1; + repeat(3) @(posedge clk); + if (ack || err) $fatal(1,"stale termination after reset"); + start(0,0,32'h0000_0000,0,4'hf); finish(0,0,got0); + + stall_all=1; + start(1,0,32'h4000_0000,0,4'hf); + finish(1,1,got1); + stall_all=0; + $display("Wishbone interconnect tests passed"); + $finish; + end +endmodule diff --git a/scripts/synth.ys b/scripts/synth.ys index 98b3123..4d0342f 100644 --- a/scripts/synth.ys +++ b/scripts/synth.ys @@ -10,3 +10,24 @@ hierarchy -check -top cpu_core synth -top cpu_core stat write_verilog -noattr build/cpu_core_synth.v + +design -reset +read_verilog -sv hw/rtl/bus/wb_error_slave.sv hw/rtl/bus/wb_address_decoder.sv hw/rtl/bus/wb_arbiter.sv hw/rtl/bus/wb_interconnect.sv hw/rtl/bus/wb_cpu_adapter.sv +hierarchy -check -top wb_interconnect +synth -top wb_interconnect +stat +write_verilog -noattr build/wb_interconnect_synth.v + +design -reset +read_verilog -sv hw/rtl/bus/wb_error_slave.sv hw/rtl/bus/wb_address_decoder.sv hw/rtl/bus/wb_arbiter.sv hw/rtl/bus/wb_interconnect.sv hw/rtl/bus/wb_cpu_adapter.sv +hierarchy -check -top wb_cpu_adapter +synth -top wb_cpu_adapter +stat +write_verilog -noattr build/wb_cpu_adapter_synth.v + +design -reset +read_verilog -sv hw/rtl/peripherals/wb_uart.sv hw/rtl/peripherals/wb_gpio.sv hw/rtl/peripherals/wb_spi.sv hw/rtl/peripherals/wb_i2c.sv hw/rtl/peripherals/wb_timer.sv hw/rtl/peripherals/wb_watchdog.sv hw/rtl/peripherals/interrupt_controller.sv hw/rtl/peripherals/wb_debug_perf.sv hw/rtl/peripherals/wb_power_ctrl.sv hw/rtl/peripherals/peripheral_subsystem.sv +hierarchy -check -top peripheral_subsystem +synth -top peripheral_subsystem +stat +write_verilog -noattr build/peripheral_subsystem_synth.v diff --git a/verification/tb/cpu_tb.sv b/verification/tb/cpu_tb.sv index b882402..e471d4a 100644 --- a/verification/tb/cpu_tb.sv +++ b/verification/tb/cpu_tb.sv @@ -20,7 +20,7 @@ module cpu_tb; .dmem_addr(dmem_addr), .dmem_wdata(dmem_wdata), .dmem_wstrb(dmem_wstrb), .dmem_ready(dmem_ready), .dmem_rdata(dmem_rdata), .dmem_error(dmem_error), .trap_valid(trap_valid), .trap_pc(trap_pc), - .trap_cause(trap_cause), .trap_value(trap_value) + .trap_cause(trap_cause), .trap_value(trap_value), .retire() ); initial begin