diff --git a/Makefile b/Makefile index 986f1a1..1fb8f0b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: Makefile -## +## {{{ ## Project: ZBasic, a generic toplevel impl using the full ZipCPU ## ## Purpose: A master project makefile. It tries to build all targets @@ -12,11 +12,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2015-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2015-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -29,16 +29,18 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} .PHONY: all all: check-install archive datestamp rtl sim sw +AUTOD := auto-data +SIMD := sim # # Could also depend upon load, if desired, but not necessary BENCH := # `find bench -name Makefile` `find bench -name "*.cpp"` `find bench -name "*.h"` @@ -117,18 +119,18 @@ archive: # .PHONY: autodata autodata: check-autofpga - $(SUBMAKE) auto-data - $(call copyif-changed,auto-data/toplevel.v,rtl/toplevel.v) - $(call copyif-changed,auto-data/main.v,rtl/main.v) - $(call copyif-changed,auto-data/iscachable.v,rtl/cpu/iscachable.v) - $(call copyif-changed,auto-data/regdefs.h,sw/host/regdefs.h) - $(call copyif-changed,auto-data/regdefs.cpp,sw/host/regdefs.cpp) - $(call copyif-changed,auto-data/board.h,sw/zlib/board.h) - $(call copyif-changed,auto-data/board.ld,sw/board/board.ld) - $(call copyif-changed,auto-data/bkram.ld,sw/board/bkram.ld) - $(call copyif-changed,auto-data/rtl.make.inc,rtl/make.inc) - $(call copyif-changed,auto-data/testb.h,sim/verilated/testb.h) - $(call copyif-changed,auto-data/main_tb.cpp,sim/verilated/main_tb.cpp) + $(SUBMAKE) $(AUTOD) + $(call copyif-changed,$(AUTOD)/toplevel.v,rtl/toplevel.v) + $(call copyif-changed,$(AUTOD)/main.v,rtl/main.v) + $(call copyif-changed,$(AUTOD)/iscachable.v,rtl/cpu/iscachable.v) + $(call copyif-changed,$(AUTOD)/regdefs.h,sw/host/regdefs.h) + $(call copyif-changed,$(AUTOD)/regdefs.cpp,sw/host/regdefs.cpp) + $(call copyif-changed,$(AUTOD)/board.h,sw/zlib/board.h) + $(call copyif-changed,$(AUTOD)/board.ld,sw/board/board.ld) + $(call copyif-changed,$(AUTOD)/bkram.ld,sw/board/bkram.ld) + $(call copyif-changed,$(AUTOD)/rtl.make.inc,rtl/make.inc) + $(call copyif-changed,$(AUTOD)/testb.h,$(SIMD)/testb.h) + $(call copyif-changed,$(AUTOD)/main_tb.cpp,$(SIMD)/main_tb.cpp) # # @@ -148,7 +150,7 @@ rtl: verilated # .PHONY: sim sim: rtl check-gpp - +@$(SUBMAKE) sim/verilated + +@$(SUBMAKE) $(SIMD) # # @@ -163,7 +165,7 @@ sw: sw-host sw-zlib sw-board # .PHONY: sw-host sw-host: - $(SUBMAKE) sw/host + +$(SUBMAKE) sw/host # # @@ -188,11 +190,11 @@ sw-board: sw-zlib check-zip-gcc # .PHONY: hello hello: sim sw - sim/verilated/main_tb sw/board/hello + sim/main_tb sw/board/hello .PHONY: sdtest sdtest: sim sw - sim/verilated/main_tb sw/board/sdtest + sim/main_tb sw/board/sdtest .PHONY: test test: hello @@ -217,9 +219,9 @@ endef .PHONY: clean clean: - +$(SUBMAKE) auto-data clean - +$(SUBMAKE) sim/verilated clean - +$(SUBMAKE) rtl clean - +$(SUBMAKE) sw/zlib clean - +$(SUBMAKE) sw/board clean - +$(SUBMAKE) sw/host clean + +$(SUBMAKE) auto-data clean + +$(SUBMAKE) sim clean + +$(SUBMAKE) rtl clean + +$(SUBMAKE) sw/zlib clean + +$(SUBMAKE) sw/board clean + +$(SUBMAKE) sw/host clean diff --git a/README.md b/README.md index 4fd36fb..66c6b74 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # ZBasic This project provides a very basic version of a working -[ZipCPU](https://github.com/ZipCPU/zbasic) system. It is designed so that +[ZipCPU](https://github.com/ZipCPU/zipcpu) system. It is designed so that others (you perhaps?) can then build off of it and design with it. ZBasic has three primary goals: @@ -17,7 +17,17 @@ describing how to do so. # Status -The ZBasic system can now be made using [autofpga](https://github.com/ZipCPU/autofpga), all the way from zero to Hello World successfully in Verilator testing. +The ZBasic system can now be made using [autofpga](https://github.com/ZipCPU/autofpga), all the way from zero to Hello World successfully in Verilator testing. Other tests have been added as well: + +- [sw/board/cputest.c](Bare CPU test) +- [sw/board/lockcheck.c](Atomic access check) +- [sw/board/hellostep.c](Stepping through hello world) + +That said, current ZipCPU development now supports a simulation checking +environment that allows the CPU to be checked in multiple environments, with +multiple different configurations. This repository only tends to check a +single configuration at a time. It's still a useful testbed, but it is no +longer the definitive one for the ZipCPU. # License diff --git a/auto-data/Makefile b/auto-data/Makefile index 68d8d00..02478db 100644 --- a/auto-data/Makefile +++ b/auto-data/Makefile @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: auto-data/Makefile -## +## {{{ ## Project: ZBasic, a generic toplevel impl using the full ZipCPU ## ## Purpose: To direct the build of the autofpga automatically generated @@ -13,11 +13,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -30,14 +30,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} .PHONY: all all: data # diff --git a/auto-data/bkram.txt b/auto-data/bkram.txt index 1462236..9362d8f 100644 --- a/auto-data/bkram.txt +++ b/auto-data/bkram.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: bkram.txt -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: To define the interface to a generic block RAM device for the @@ -11,11 +11,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -28,28 +28,32 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=bkram @DEVID=BKRAM @$LGMEMSZ=20 @LGMEMSZ.FORMAT=%d -@$NADDR=(1<<(@$THIS.LGMEMSZ-2)) +@$NADDR=(1<<(@$THIS.LGMEMSZ))/(@$(SLAVE.BUS.WIDTH)/8) @$NBYTES=(1<<(@$THIS.LGMEMSZ)) @NBYTES.FORMAT=0x%08x @ACCESS=@$(DEVID)_ACCESS @SLAVE.TYPE=MEMORY @SLAVE.BUS=wb @MAIN.INSERT= - memdev #(.LGMEMSZ(@$THIS.LGMEMSZ), .EXTRACLOCK(1)) - @$(PREFIX)i(i_clk, i_reset, - @$(SLAVE.PORTLIST)); + memdev #( + .LGMEMSZ(@$THIS.LGMEMSZ), .EXTRACLOCK(1), + .DW(@$(SLAVE.BUS.WIDTH)) + ) @$(PREFIX)i( + i_clk, i_reset, + @$(SLAVE.PORTLIST) + ); @REGS.N=1 @REGS.0= 0 R_@$(DEVID) RAM @REGDEFS.H.DEFNS= @@ -75,14 +79,38 @@ #endif #endif +#ifdef NEW_VERILATOR +#define block_ram rootp->main__DOT__@$(PREFIX)i__DOT__mem.m_storage +#else #define block_ram VVAR(_@$(PREFIX)i__DOT__mem) +#endif + @SIM.LOAD= start = start & (-4); wlen = (wlen+3)&(-4); // Need to byte swap data to get it into the memory - char *bswapd = new char[len+8]; - memcpy(bswapd, &buf[offset], wlen); - byteswapbuf(len>>2, (uint32_t *)bswapd); - memcpy(&m_core->block_ram[start], bswapd, wlen); - delete bswapd; + if (@$(SLAVE.BUS.WIDTH) == 32) { + char *bswapd = new char[len+8]; + memcpy(bswapd, &buf[offset], wlen); + byteswapbuf(len>>2, (uint32_t *)bswapd); + memcpy(&m_core->block_ram[start], bswapd, wlen); + delete[] bswapd; + } else { + for(unsigned jk=0; jkblock_ram[word_addr]; + cp = (char *)wp; + + subword_addr = start+jk; + subword_addr = ~subword_addr; + subword_addr &= @$(SLAVE.BUS.WIDTH)/8-1; + // subword_addr = @$(SLAVE.BUS.WIDTH)/8-subword_addr; + cp[subword_addr] = buf[offset+jk]; + } + } diff --git a/auto-data/busconsole.txt b/auto-data/busconsole.txt index 7ba8fc0..f2d55d1 100644 --- a/auto-data/busconsole.txt +++ b/auto-data/busconsole.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: wbuconsole.txt -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: Provide access to both a debugging bus and a console port for @@ -15,11 +15,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2019, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -32,14 +32,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=wbu @DEVID=DBGBUS @ACCESS=WBUBUS_MASTER @@ -99,24 +99,34 @@ wire zip_dbg_stall, zip_dbg_ack; wire [31:0] zip_dbg_data; `endif -@$ZIP_ADDRESS_BIT=@$BUS_ADDRESS_WIDTH-1 -@$ZIP_ADDRESS=(1<<(@$.ZIP_ADDRESS_BIT+2)) -@ZIP_ADDRESS.FORMAT= 0x%08x -@$ZIP_DBGDATA=4+@$.ZIP_ADDRESS -@ZIP_DBGDATA.FORMAT= 0x%08x @MAIN.INSERT= // The Host USB interface, to be used by the WB-UART bus - rxuartlite #(.TIMER_BITS(@$(DEVID)BITS), - .CLOCKS_PER_BAUD(BUSUART[@$(DEVID)BITS-1:0])) - rcv(@$(CLOCK.WIRE), i_@$(PREFIX)_uart_rx, - @$(PREFIX)_rx_stb, @$(PREFIX)_rx_data); - txuartlite #(.TIMING_BITS(@$(DEVID)BITS[4:0]), - .CLOCKS_PER_BAUD(BUSUART[@$(DEVID)BITS-1:0])) - txv(@$(CLOCK.WIRE), - @$(PREFIX)_tx_stb, - @$(PREFIX)_tx_data, - o_@$(PREFIX)_uart_tx, - @$(PREFIX)_tx_busy); + rxuartlite #( + // {{{ + .TIMER_BITS(@$(DEVID)BITS), + .CLOCKS_PER_BAUD(BUSUART[@$(DEVID)BITS-1:0]) + // }}} + ) rcv( + // {{{ + @$(CLOCK.WIRE), i_@$(PREFIX)_uart_rx, + @$(PREFIX)_rx_stb, @$(PREFIX)_rx_data + // }}} + ); + + txuartlite #( + // {{{ + .TIMING_BITS(@$(DEVID)BITS[4:0]), + .CLOCKS_PER_BAUD(BUSUART[@$(DEVID)BITS-1:0]) + // }}} + ) txv( + // {{{ + @$(CLOCK.WIRE), + @$(PREFIX)_tx_stb, + @$(PREFIX)_tx_data, + o_@$(PREFIX)_uart_tx, + @$(PREFIX)_tx_busy + // }}} + ); `ifdef INCLUDE_ZIPCPU `else @@ -132,10 +142,11 @@ wire [31:0] @$(MASTER.PREFIX)_tmp_addr; // Verilator lint_on UNUSED wbuconsole #(.LGWATCHDOG(@$(DEVID)WATCHDOG)) - genbus(@$(CLOCK.WIRE), @$(PREFIX)_rx_stb, @$(PREFIX)_rx_data, + genbus(@$(CLOCK.WIRE), 1'b0, @$(PREFIX)_rx_stb, @$(PREFIX)_rx_data, @$(MASTER.PREFIX)_cyc, @$(MASTER.PREFIX)_stb, @$(MASTER.PREFIX)_we, @$(MASTER.PREFIX)_tmp_addr, @$(MASTER.PREFIX)_data, @$(MASTER.PREFIX)_stall, @$(MASTER.PREFIX)_ack, - @$(MASTER.PREFIX)_err, @$(MASTER.PREFIX)_idata, + @$(MASTER.PREFIX)_idata, + @$(MASTER.PREFIX)_err, w_bus_int, @$(PREFIX)_tx_stb, @$(PREFIX)_tx_data, @$(PREFIX)_tx_busy, // @@ -146,8 +157,6 @@ assign @$(MASTER.PREFIX)_sel = 4'hf; assign @$(MASTER.PREFIX)_addr = @$(MASTER.PREFIX)_tmp_addr[(@$BUS_ADDRESS_WIDTH-1):0]; @REGDEFS.H.DEFNS= -#define R_ZIPCTRL @$.ZIP_ADDRESS -#define R_ZIPDATA @$.ZIP_DBGDATA #define BAUDRATE @$[%d](BAUDRATE) @RTL.MAKE.GROUP= WBUBUS @RTL.MAKE.SUBD=wbubus @@ -206,14 +215,25 @@ // Console definitions wire w_console_rx_stb, w_console_tx_stb, w_console_busy; wire [6:0] w_console_rx_data, w_console_tx_data; + // Verilator lint_off UNUSED wire [31:0] uart_debug; + // Verilator lint_on UNUSED @MAIN.INSERT= - wbconsole #(.LGFLEN(6)) console(@$(SLAVE.BUS.CLOCK.WIRE), 1'b0, - @$(SLAVE.PORTLIST), - w_console_tx_stb, w_console_tx_data, w_console_busy, - w_console_rx_stb, w_console_rx_data, - uartrx_int, uarttx_int, uartrxf_int, uarttxf_int, - uart_debug); + wbconsole #( + .LGFLEN(6) + ) console( + // {{{ + .i_clk(@$(SLAVE.BUS.CLOCK.WIRE)), .i_reset(1'b0), + @$(SLAVE.ANSIPORTLIST), + .o_uart_stb(w_console_tx_stb), .o_uart_data(w_console_tx_data), + .i_uart_busy(w_console_busy), + .i_uart_stb(w_console_rx_stb), .i_uart_data(w_console_rx_data), + .o_uart_rx_int(uartrx_int), .o_uart_tx_int(uarttx_int), + .o_uart_rxfifo_int(uartrxf_int), + .o_uart_txfifo_int(uarttxf_int), + .o_debug(uart_debug) + // }}} + ); @REGS.NOTE = // CONSOLE registers @REGS.N = 3 ## This is unused @REGS.0 = 0 R_CONSOLE_SETUP USETUP @@ -229,6 +249,7 @@ typedef struct CONSOLE_S { } CONSOLE; #define _@$(PREFIX)_txbusy ((_uart->u_fifo & 0x10000)==0) +#define _@$(PREFIX)_txidle ((_uart->u_tx & 0x100) ==0) @BDEF.IONAME= io_console @BDEF.IOTYPE= CONSOLE @BDEF.OSDEF= _BOARD_HAS_BUSCONSOLE diff --git a/auto-data/buserr.txt b/auto-data/buserr.txt index 129351a..7badb4f 100644 --- a/auto-data/buserr.txt +++ b/auto-data/buserr.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: buserr.txt -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: Provide a readable memory location containing the address of the @@ -11,11 +11,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -28,14 +28,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=buserr @NADDR=1 @SLAVE.TYPE=SINGLE diff --git a/auto-data/clock.txt b/auto-data/clock.txt index 52d313c..688da39 100644 --- a/auto-data/clock.txt +++ b/auto-data/clock.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: clock.txt -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: @@ -10,11 +10,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -27,14 +27,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=clk @CLOCK.NAME=clk @CLOCK.TOP=i_clk diff --git a/auto-data/flash.txt b/auto-data/flash.txt index 4337184..e7a6995 100644 --- a/auto-data/flash.txt +++ b/auto-data/flash.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: flash.txt -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: Describes the flash in our new data format. @@ -10,11 +10,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -27,14 +27,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=flash @DEVID=FLASH @ACCESS=@$(DEVID)_ACCESS @@ -42,7 +42,6 @@ @$NADDR=(1<<(@$LGFLASHSZ-2)) @$NBYTES=(1<<@$LGFLASHSZ) @NBYTES.FORMAT=0x%08x -@ACCESS= FLASH_ACCESS @SLAVE.TYPE=MEMORY @SLAVE.BUS=wb @NDUMMY=6 @@ -144,10 +143,18 @@ output wire [1:0] o_qspi_mod; @MAIN.DEFNS= // Definitions for the @$(PREFIX) debug port + // Verilator lint_off UNUSED wire @$(PREFIX)_dbg_trigger; wire [31:0] @$(PREFIX)_debug; + // Verilator lint_on UNUSED @MAIN.INSERT= - qflexpress #(.LGFLASHSZ(@$LGFLASHSZ), .OPT_CLKDIV(1), + //////////////////////////////////////////////////////////////////////// + // + // Flash controller + // {{{ + qflexpress #( + // {{{ + .LGFLASHSZ(@$LGFLASHSZ), .OPT_CLKDIV(1), .OPT_ENDIANSWAP(0), .NDUMMY(@$(NDUMMY)), .RDDELAY(@$(RDDELAY)), .OPT_STARTUP_FILE(@$(STARTUP_SCRIPT)), @@ -156,14 +163,19 @@ `else .OPT_CFG(1'b0) `endif - ) - @$(PREFIX)i(i_clk, i_reset, - // Primary memory reading inputs - @$(SLAVE.PORTLIST), - // Configuration bus ports - @$(flashcfg.SLAVE.PORTLIST), - o_qspi_sck, o_qspi_cs_n, o_qspi_mod, o_qspi_dat, i_qspi_dat, - flash_dbg_trigger, flash_debug); + // }}} + ) @$(PREFIX)i( + // {{{ + i_clk, i_reset, + // Primary memory reading inputs + @$(SLAVE.PORTLIST), + // Configuration bus ports + @$(flashcfg.SLAVE.PORTLIST), + o_qspi_sck, o_qspi_cs_n, o_qspi_mod, o_qspi_dat, i_qspi_dat, + flash_dbg_trigger, flash_debug + // }}} + ); + // }}} @MAIN.ALT= assign o_qspi_sck = 1'b1; assign o_qspi_cs_n = 1'b1; diff --git a/auto-data/global.txt b/auto-data/global.txt index a325189..cad87a3 100644 --- a/auto-data/global.txt +++ b/auto-data/global.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: global.txt -## +## {{{ ## Project: ZBasic, a generic toplevel impl using the full ZipCPU ## ## Purpose: Capture any global configuration parameters @@ -10,11 +10,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2015-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2015-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -27,14 +27,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @LEGAL=legalgen.txt @PROJECT=ZBasic, a generic toplevel implementation using the full ZipCPU # @XDC.FILE=nexysv.xdc @@ -75,14 +75,6 @@ unsigned addrdecode(const char *v) { for(int i=0; i for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=gpio @DEVID=GPIO @NADDR=1 @@ -46,19 +46,17 @@ @INT.GPIO.WIRE=gpio_int @INT.GPIO.PIC=buspic @TOP.PORTLIST= - // GPIO ports + // @$(DEVID) ports i_@$(PREFIX), o_@$(PREFIX) @TOP.IODECL= - // GPIO wires - localparam NGPI = @$(NUMINPUT), NGPO=@$(NUMOUTPUT); - // GPIO ports + // @$(DEVID) ports input wire [(@$(NUMINPUT)-1):0] i_@$(PREFIX); output wire [(@$(NUMOUTPUT)-1):0] o_@$(PREFIX); @TOP.MAIN= - // GPIO wires + // @$(DEVID) wires i_@$(PREFIX), o_@$(PREFIX) @MAIN.PORTLIST= - // GPIO ports + // @$(DEVID) ports i_@$(PREFIX), o_@$(PREFIX) @MAIN.IODECL= localparam NGPI = @$(NUMINPUT), NGPO=@$(NUMOUTPUT); @@ -68,19 +66,27 @@ @MAIN.DEFNS= wire sd_reset; @MAIN.INSERT= + //////////////////////////////////////////////////////////////////////// // // @$(DEVID) + // {{{ + // This interface should allow us to control up to 16 @$(DEVID) inputs, + // and another 16 @$(DEVID) outputs. The interrupt trips when any of + // the inputs changes. (Sorry, which input isn't (yet) selectable.) // - // This interface should allow us to control up to 16 @$(DEVID) inputs, and - // another 16 @$(DEVID) outputs. The interrupt trips when any of the inputs - // changes. (Sorry, which input isn't (yet) selectable.) - // - localparam INITIAL_@$(DEVID) = @$(NUMOUTPUT)'h0; - wbgpio #(NGPI, NGPO, INITIAL_@$(DEVID)) - @$(PREFIX)i(i_clk, @$(SLAVE.PORTLIST), - i_@$(PREFIX), o_@$(PREFIX), @$(PREFIX)_int); + localparam [NGPO-1:0] INITIAL_@$(DEVID) = @$(NUMOUTPUT)'h0; + + wbgpio #( + .NIN(NGPI), .NOUT(NGPO), .DEFAULT(INITIAL_@$(DEVID)) + ) @$(PREFIX)i( + // {{{ + i_clk, @$(SLAVE.PORTLIST), + i_@$(PREFIX), o_@$(PREFIX), @$(PREFIX)_int + // }}} + ); assign sd_reset = o_@$(PREFIX)[0]; + // }}} @REGS.N=1 @REGS.0= 0 R_@$(DEVID) @$(DEVID) GPI GPO @BDEF.DEFN= diff --git a/auto-data/legalgen.txt b/auto-data/legalgen.txt index fc11766..33f4698 100644 --- a/auto-data/legalgen.txt +++ b/auto-data/legalgen.txt @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // DO NOT EDIT THIS FILE! @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,11 +31,11 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} diff --git a/auto-data/mem_bkram_only.txt b/auto-data/mem_bkram_only.txt index bfcfca0..a970dda 100644 --- a/auto-data/mem_bkram_only.txt +++ b/auto-data/mem_bkram_only.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: mem_flash_bkram.txt -## +## {{{ ## Project: ZBasic, a generic toplevel impl using the full ZipCPU ## ## Purpose: Describes a memory model containing nothing more than flash @@ -13,11 +13,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -30,15 +30,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## -## +## }}} @PREFIX=mem_bkram_only @MASTER.TYPE=SCRIPT @MASTER.BUS=wb diff --git a/auto-data/mem_flash_bkram.txt b/auto-data/mem_flash_bkram.txt index 19b3764..faa91c8 100644 --- a/auto-data/mem_flash_bkram.txt +++ b/auto-data/mem_flash_bkram.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: mem_flash_bkram.txt -## +## {{{ ## Project: ZBasic, a generic toplevel impl using the full ZipCPU ## ## Purpose: Describes a memory model containing nothing more than flash @@ -13,11 +13,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -30,15 +30,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## -## +## }}} @PREFIX=mem_flash_bkram @MASTER.TYPE=SCRIPT @MASTER.BUS=wb diff --git a/auto-data/pic.txt b/auto-data/pic.txt index aab341d..e24593a 100644 --- a/auto-data/pic.txt +++ b/auto-data/pic.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: pic.txt -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: @@ -10,11 +10,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -27,16 +27,17 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=buspic @DEVID=BUSPIC +@ACCESS=BUSPIC_ACCESS @NADDR=1 @BUSP=wb @ACCESS=BUSPIC_ACCESS @@ -53,9 +54,11 @@ // // The BUS Interrupt controller // - icontrol #(15) @$(PREFIX)i(@$(SLAVE.BUS.CLOCK.WIRE), 1'b0, - @$(SLAVE.PORTLIST), - bus_int_vector, w_bus_int); + icontrol #(15) + @$(PREFIX)i( + @$(SLAVE.BUS.CLOCK.WIRE), 1'b0, + @$(SLAVE.PORTLIST), + bus_int_vector, w_bus_int); @MAIN.ALT= @REGS.N=1 @REGS.0= 0 R_PIC PIC diff --git a/auto-data/pwrcount.txt b/auto-data/pwrcount.txt index 81e2bb0..3a68c7e 100644 --- a/auto-data/pwrcount.txt +++ b/auto-data/pwrcount.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: pwrcount.txt -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: @@ -10,11 +10,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -27,14 +27,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=pwrcount @NADDR=1 @ACCESS=PWRCOUNT_ACCESS diff --git a/auto-data/rtcdate.txt b/auto-data/rtcdate.txt index 9e9acb0..d40d225 100644 --- a/auto-data/rtcdate.txt +++ b/auto-data/rtcdate.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: rtcdate.txt -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: @@ -10,11 +10,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -27,14 +27,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=rtcdate @DEVID=RTCDATE @NADDR=1 diff --git a/auto-data/rtclight.txt b/auto-data/rtclight.txt index 1e587e6..9a58f84 100644 --- a/auto-data/rtclight.txt +++ b/auto-data/rtclight.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: rtclight.txt -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: @@ -10,11 +10,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -27,14 +27,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=rtc @DEVID=RTC @NADDR=8 @@ -50,15 +50,27 @@ // This clock step is designed to match @$(CLOCK.FREQUENCY) Hz localparam [31:0] RTC_CLKSTEP = @$[32'h%08x](CLKSTEP); wire @$(PREFIX)_ppd; + // Verilator lint_off UNUSED wire ck_pps; + // Verilator lint_on UNUSED @MAIN.INSERT= - rtclight #(.DEFAULT_SPEED(@$[32'h%x](CLKSTEP)), + rtclight #( + // {{{ + .DEFAULT_SPEED(RTC_CLKSTEP), .OPT_TIMER(1'b1), .OPT_STOPWATCH(1'b1), .OPT_ALARM(1'b0), - .OPT_FIXED_SPEED(1'b1)) - the@$(PREFIX)(i_clk, i_reset, @$(SLAVE.PORTLIST), - @$(PREFIX)_int, ck_pps, @$(PREFIX)_ppd); + .OPT_FIXED_SPEED(1'b1) + // }}} + ) the@$(PREFIX)( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + @$(SLAVE.ANSIPORTLIST), + .o_interrupt(@$(PREFIX)_int), + .o_pps(ck_pps), + .o_ppd(@$(PREFIX)_ppd) + // }}} + ); @MAIN.ALT= assign ck_pps = 1'b0; @REGS.NOTE= // RTC clock registers diff --git a/auto-data/sdspi.txt b/auto-data/sdspi.txt index 8feef11..7205187 100644 --- a/auto-data/sdspi.txt +++ b/auto-data/sdspi.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: sdspi.txt -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: Describes how to connect an SD-SPI peripheral to a wishbone @@ -17,11 +17,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -34,14 +34,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=sdcard @DEVID=SDSPI @NADDR=4 @@ -58,47 +58,56 @@ o_sd_sck, io_sd_cmd, io_sd, i_sd_cd @TOP.IODECL= // SD Card + // {{{ output wire o_sd_sck; inout wire io_sd_cmd; inout wire [3:0] io_sd; input wire i_sd_cd; + // }}} @TOP.DEFNS= + // SD Card definitions + // {{{ wire w_sd_cmd; wire [3:0] w_sd_data; wire i_sd_cmd; wire [3:0] i_sd; + // }}} @TOP.MAIN= // SD Card o_sd_sck, w_sd_cmd, w_sd_data, io_sd_cmd, io_sd, i_sd_cd @TOP.INSERT= + ////////////////////////////////////////////////////////////////////// // - // - // Wires for setting up the SD Card Controller + // SD Card SPI Controller + // {{{ + ////////////////////////////////////////////////////////////////////// // // - // assign io_sd_cmd = w_sd_cmd ? 1'bz:1'b0; - // assign io_sd[0] = w_sd_data[0]? 1'bz:1'b0; + + // Wires for setting up the SD Card Controller + // {{{ + // This is how we'd set up for SDIO + // assign io_sd_cmd = w_sd_cmd ? 1'bz:1'b0; // MOSI + // assign io_sd[0] = w_sd_data[0]? 1'bz:1'b0; // MISO // assign io_sd[1] = w_sd_data[1]? 1'bz:1'b0; // assign io_sd[2] = w_sd_data[2]? 1'bz:1'b0; - // assign io_sd[3] = w_sd_data[3]? 1'bz:1'b0; - - - // IOBUF sd_cmd_buf(.T(w_sd_cmd),.O(i_sd_cmd), .I(1'b0), .IO(io_sd_cmd)); - IOBUF sd_dat0_bf(.T(1'b1),.O(i_sd[0]),.I(1'b1),.IO(io_sd[0])); + // assign io_sd[3] = w_sd_data[3]? 1'bz:1'b0; // CS_n + // }}} + IOBUF sd_cmd_bf(.T(1'b0),.O(i_sd_cmd),.I(w_sd_cmd),.IO(io_sd_cmd));// MISO + IOBUF sd_dat0_bf(.T(1'b1),.O(i_sd[0]),.I(1'b1),.IO(io_sd[0]));// MISO IOBUF sd_dat1_bf(.T(1'b1),.O(i_sd[1]),.I(1'b1),.IO(io_sd[1])); IOBUF sd_dat2_bf(.T(1'b1),.O(i_sd[2]),.I(1'b1),.IO(io_sd[2])); - // IOBUF sd_dat3_bf(.T(w_sd_data[3]),.O(i_sd[3]),.I(1'b0),.IO(io_sd[3])); - - IOBUF sd_cmd_buf(.T(1'b0),.O(i_sd_cmd), .I(w_sd_cmd), .IO(io_sd_cmd)); - IOBUF sd_dat3_bf(.T(1'b0),.O(i_sd[3]),.I(w_sd_data[3]),.IO(io_sd[3])); - + // Implement an open-drain output + IOBUF sd_dat3_bf(.T(w_sd_data[3]),.O(i_sd[3]),.I(1'b0),.IO(io_sd[3])); + // }}} @MAIN.PORTLIST= // The SD-Card wires o_sd_sck, o_sd_cmd, o_sd_data, i_sd_cmd, i_sd_data, i_sd_detect @MAIN.IODECL= // SD-Card declarations + // {{{ output wire o_sd_sck, o_sd_cmd; output wire [3:0] o_sd_data; // verilator lint_off UNUSED @@ -106,35 +115,40 @@ input wire [3:0] i_sd_data; // verilator lint_on UNUSED input wire i_sd_detect; + // }}} @MAIN.DEFNS= - wire[31:0] sdspi_debug; + // SD SPI definitions + // Verilator lint_off UNUSED + wire [31:0] sdspi_debug; + // Verilator lint_on UNUSED + wire w_sd_cs_n, w_sd_mosi, w_sd_miso; @MAIN.INSERT= - // SPI mapping - wire w_sd_cs_n, w_sd_mosi, w_sd_miso; + //////////////////////////////////////////////////////////////////////// + // + // SD Card SPI handling + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // - sdspi @$(PREFIX)i(i_clk, sd_reset, + sdspi + @$(PREFIX)i( + // {{{ + i_clk, sd_reset, @$(SLAVE.PORTLIST), w_sd_cs_n, o_sd_sck, w_sd_mosi, w_sd_miso, i_sd_detect, - @$(PREFIX)_int, 1'b1, sdspi_debug); + @$(PREFIX)_int, 1'b1, sdspi_debug + // }}} + ); assign w_sd_miso = i_sd_data[0]; assign o_sd_data = { w_sd_cs_n, 3'b111 }; assign o_sd_cmd = w_sd_mosi; + // }}} @MAIN.ALT= assign o_sd_sck = 1'b1; assign o_sd_cmd = 1'b1; assign o_sd_data = 4'hf; -@TOP.INSERT= - // - // - // Wires for setting up the SD Card Controller - // - // - assign io_sd_cmd = w_sd_cmd ? 1'bz:1'b0; - assign io_sd[0] = w_sd_data[0]? 1'bz:1'b0; - assign io_sd[1] = w_sd_data[1]? 1'bz:1'b0; - assign io_sd[2] = w_sd_data[2]? 1'bz:1'b0; - assign io_sd[3] = w_sd_data[3]? 1'bz:1'b0; @REGS.N=4 @REGS.NOTE= // SD-SPI addresses @REGS.0= 0 R_@$(DEVID)_CTRL SDCARD @@ -142,6 +156,14 @@ @REGS.2= 2 R_@$(DEVID)_FIFOA SDFIFOA, SDFIF0, SDFIFA @REGS.3= 3 R_@$(DEVID)_FIFOB SDFIFOB, SDFIF1, SDFIFB @BDEF.DEFN= +//////////////////////////////////////////////////////////////////////////////// +// +// SD SPI constants +// {{{ +//////////////////////////////////////////////////////////////////////////////// +// +// + #define @$(DEVID)_SETAUX 0x0000ff #define @$(DEVID)_READAUX 0x0000bf #define @$(DEVID)_CMD 0x000040 @@ -165,6 +187,7 @@ typedef struct @$(DEVID)_S { unsigned sd_ctrl, sd_data, sd_fifo[2]; } @$(DEVID); +// }}} @BDEF.IONAME=_sdcard @BDEF.IOTYPE=@$(DEVID) @BDEF.OSDEF=_BOARD_HAS_@$(DEVID) @@ -187,6 +210,8 @@ typedef struct @$(DEVID)_S { } #endif // @$(ACCESS) @SIM.TICK= + // SD Card simulation + // {{{ #ifdef @$(ACCESS) m_core->i_sd_data = m_sdcard((m_core->o_sd_data&8)?1:0, m_core->o_sd_sck, m_core->o_sd_cmd); @@ -194,10 +219,24 @@ typedef struct @$(DEVID)_S { m_core->i_sd_data |= (m_core->o_sd_data&0x0e); m_core->i_sd_detect = 1; #endif // @$(ACCESS) + // }}} @SIM.DEFINES= +//////////////////////////////////////////////////////////////////////////////// +// +// SD SPI Defines +// {{{ +//////////////////////////////////////////////////////////////////////////////// +// +// #ifndef VVAR -#ifdef NEW_VERILATOR +#ifdef ROOT_VERILATOR + +#include "Vmain___024root.h" +#define VVAR(A) rootp->main__DOT_ ## A + +#elif defined(NEW_VERILATOR) + #define VVAR(A) main__DOT_ ## A #else #define VVAR(A) v__DOT_ ## A @@ -231,61 +270,9 @@ typedef struct @$(DEVID)_S { #define sd_ll_fifo_wr_complete VVAR(_@$(PREFIX)__DOT__ll_fifo_wr_complete) #define sd_use_fifo VVAR(_@$(PREFIX)__DOT__r_use_fifo) #define sd_fifo_wr VVAR(_@$(PREFIX)__DOT__r_fifo_wr) - -@SIM.DEBUG= - /* - printf(" SDSPI[%d,%d(%d),(%d)]", - m_core->sd_cmd_busy, - m_core->sd_clk, - m_core->sd_cmd_state, - m_core->sd_rsp_state); - printf(" LL[%d,%2x->CK=%d/%2x,%s,ST=%2d,TX=%2x,RX=%2x->%d,%2x] ", - m_core->sd_ll_cmd_stb, - m_core->sd_ll_cmd_dat, - m_core->sd_ll_z_counter, - // (m_core->sd_ll_clk_counter==0)?1:0, - m_core->sd_ll_clk_counter, - (m_core->sd_ll_idle)?"IDLE":" ", - m_core->sd_ll_state, - m_core->sd_ll_byte, - m_core->sd_ll_ireg, - m_core->sd_ll_out_stb, - m_core->sd_ll_out_dat - ); - printf(" CRC=%02x/%2d", - m_core->sd_cmd_crc, - m_core->sd_cmd_crc_cnt); - printf(" SPI(%d,%d,%d/%d,%d)->?", - m_core->o_sf_cs_n, - m_core->o_sd_cs_n, - m_core->o_spi_sck, - m_core->@$(VERILATOR_PREFIX)__DOT__sdcard_sck, - m_core->o_spi_mosi); - - printf(" CK=%d,LN=%d", - m_core->sd_clk, - m_core->sd_lgblklen); - - - if (m_core->sd_use_fifo){ - printf(" FIFO"); - if (m_core->sd_fifo_wr) - printf("-WR(%04x,%d,%d,%d)", - m_core->sd_fifo_rd_crc, - m_core->sd_fifo_rd_crc_stb, - m_core->sd_ll_fifo_pkt_state, - m_core->sd_have_data_response_token); - else - printf("-RD(%04x,%d,%d,%d)", - m_core->sd_fifo_wr_crc, - m_core->sd_fifo_wr_crc_stb, - m_core->sd_ll_fifo_wr_state, - m_core->sd_ll_fifo_wr_complete - ); - } - */ +// }}} ## -@RTL.MAKE.SUBD= sdspi @RTL.MAKE.GROUP= SDSPI +@RTL.MAKE.SUBD=sdspi @RTL.MAKE.FILES= sdspi.v llsdspi.v spicmd.v spirxdata.v spitxdata.v ## diff --git a/auto-data/simtrace.txt b/auto-data/simtrace.txt index f651349..688994c 100644 --- a/auto-data/simtrace.txt +++ b/auto-data/simtrace.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: simtrace.c -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: Creates a GPIO output port for the Verilator simulation, @@ -11,11 +11,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2018-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2018-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -28,14 +28,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=simtrace @DEVID=TRACE @NADDR=4 diff --git a/auto-data/version.txt b/auto-data/version.txt index 5264d2b..f6820ac 100644 --- a/auto-data/version.txt +++ b/auto-data/version.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: version.txt -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: @@ -10,11 +10,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -27,14 +27,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} @PREFIX=version @DEVID=VERSION @ACCESS=VERSION_ACCESS diff --git a/auto-data/wbuart.txt b/auto-data/wbuart.txt index 9fc2fa3..2f0c354 100644 --- a/auto-data/wbuart.txt +++ b/auto-data/wbuart.txt @@ -1,7 +1,7 @@ ################################################################################ ## -## Filename: wbuart.txt -## +## Filename: wbuart.txt +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: To describe a UART for autogen to work with. @@ -10,11 +10,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -27,14 +27,15 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## ## ################################################################################ ## -## +## }}} # The prefix is used to create a series of bus data lines for interaction with # the bus. In this case, these will be uart_sel, uart_stall, uart_ack, and # uart_data. @@ -86,12 +87,19 @@ input wire i_@$(PREFIX)_rx, @$CTS; output wire o_@$(PREFIX)_tx, @$RTS; @MAIN.INSERT= - wbuart #(.INITIAL_SETUP(@$.UARTSETUP)) - @$(PREFIX)_uart(i_clk, 1'b0, - @$(SLAVE.PORTLIST), - i_@$(PREFIX)_rx, o_@$(PREFIX)_tx, @$CTS, @$RTS, - @$(PREFIX)rx_int, @$(PREFIX)tx_int, - @$(PREFIX)rxf_int, @$(PREFIX)txf_int); + wbuart #( + // {{{ + .INITIAL_SETUP(@$.UARTSETUP) + // }}} + ) @$(PREFIX)_uart( + // {{{ + i_clk, 1'b0, + @$(SLAVE.PORTLIST), + i_@$(PREFIX)_rx, o_@$(PREFIX)_tx, @$CTS, @$RTS, + @$(PREFIX)rx_int, @$(PREFIX)tx_int, + @$(PREFIX)rxf_int, @$(PREFIX)txf_int + // }}} + ); @MAIN.ALT= assign o_@$(PREFIX)_tx = 1'b1; assign @$RTS = 1'b0; diff --git a/auto-data/zipmaster.txt b/auto-data/zipmaster.txt index d25e94b..4eb701f 100644 --- a/auto-data/zipmaster.txt +++ b/auto-data/zipmaster.txt @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: zipmaster.txt -## +## {{{ ## Project: AutoFPGA, a utility for composing FPGA designs from peripherals ## ## Purpose: To describe what needs to be done to make the ZipCPU a part @@ -13,11 +13,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -30,17 +30,17 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## ## ## Define @SYSPIC so that other peripherals needing to be on the ZipSystem's ## PIC know the name of the PIC we are using. Note that this is a global. -## +## }}} @SYSPIC= syspic @PREFIX=zip @SIM.CLOCK=clk @@ -70,21 +70,29 @@ // Reset wire for the ZipCPU (!i_cpu_resetn) @MAIN.PORTLIST= - @$(CPURESET) + @$(CPURESET), + i_sim_write, i_sim_addr, i_sim_data, + o_prof_stb, o_prof_addr, o_prof_ticks @MAIN.IODECL= input wire @$(CPURESET); + input wire i_sim_write; + input wire [7-1:0] i_sim_addr; + input wire [31:0] i_sim_data; + output wire o_prof_stb; + output wire [@$(MASTER.BUS.AWID)+1:0] o_prof_addr; + output wire [31:0] o_prof_ticks; @MAIN.PARAM= + //////////////////////////////////////////////////////////////////////// // - // - // Variables/definitions needed by the ZipCPU BUS master - // + // Variables/definitions/parameters used by the ZipCPU bus master + // {{{ // // A 32-bit address indicating where the ZipCPU should start running // from localparam RESET_ADDRESS = @$RESET_ADDRESS; // // The number of valid bits on the bus - localparam ZIP_ADDRESS_WIDTH = @$(BUS.AWID); // Zip-CPU address width + localparam ZIP_ADDRESS_WIDTH = @$(MASTER.BUS.AWID); // Zip-CPU address width // // Number of ZipCPU interrupts localparam ZIP_INTS = 16; @@ -99,30 +107,60 @@ wire [31:0] zip_debug; wire zip_trigger; wire [ZIP_INTS-1:0] zip_int_vector; + // }}} @MAIN.INSERT= - // + //////////////////////////////////////////////////////////////////////// // // The ZipCPU/ZipSystem BUS master - // + // {{{ // assign zip_int_vector = { alt_int_vector[14:8], sys_int_vector[14:6] }; - zipsystem #(.RESET_ADDRESS(RESET_ADDRESS), - .ADDRESS_WIDTH(ZIP_ADDRESS_WIDTH), - .LGICACHE(@$(LGICACHE)),.LGDCACHE(@$(LGDCACHE)), - .START_HALTED(ZIP_START_HALTED), - .RESET_DURATION(20), - .EXTERNAL_INTERRUPTS(ZIP_INTS)) - swic(i_clk, (i_reset)||(@$(CPURESET)), + zipsystem #( + // {{{ + .RESET_ADDRESS(RESET_ADDRESS), + .ADDRESS_WIDTH(ZIP_ADDRESS_WIDTH + $clog2(@$(MASTER.BUS.WIDTH)/8)), + .BUS_WIDTH(@$(MASTER.BUS.WIDTH)), + .OPT_LGICACHE(@$(LGICACHE)), + .OPT_LGDCACHE(@$(LGDCACHE)), + .START_HALTED(ZIP_START_HALTED), + .RESET_DURATION(20), + .OPT_PIPELINED(1), + .OPT_PROFILER(1), + .EXTERNAL_INTERRUPTS(ZIP_INTS) + // }}} + ) swic( + // {{{ + .i_clk(i_clk), .i_reset((i_reset)||(@$(CPURESET))), + .i_sim_write(i_sim_write), + .i_sim_addr(i_sim_addr), + .i_sim_data(i_sim_data), // Zippys wishbone interface - @$(MASTER.PORTLIST), - zip_int_vector, zip_cpu_int, + // [o|i]_wb_* + @$(MASTER.ANSIPORTLIST), + .i_ext_int(zip_int_vector), .o_ext_int(zip_cpu_int), // Debug wishbone interface - @$(SLAVE.PORTLIST), - zip_debug); + // [o|i]_db g_* + @$(SLAVE.ANSIPORTLIST), + .o_cpu_debug(zip_debug), + .o_prof_stb(o_prof_stb), + .o_prof_addr(o_prof_addr), + .o_prof_ticks(o_prof_ticks) + // }}} + ); + assign zip_trigger = zip_debug[31]; + // Verilator lint_off UNUSED + wire unused_@$(PREFIX); + assign unused_@$(PREFIX) = &{ 1'b0, sys_int_vector[5:0], + alt_int_vector[7:0] }; + // Verilator lint_on UNUSED + // }}} @BDEF.INCLUDE= +//////////////////////////////////////////////////////////////////////////////// +// +// ZipCPU defines and macros +// {{{ #include -#include #define _HAVE_ZIPSYS #define PIC _zip->z_pic @@ -135,15 +173,26 @@ #define _HAVE_ZIPSYS_PERFORMANCE_COUNTERS #endif // INCLUDE_ACCOUNTING_COUNTERS #endif // INCLUDE_ZIPCPU +// }}} @REGDEFS.H.INSERT= +//////////////////////////////////////////////////////////////////////////////// +// +// ZipCPU register definitions +// {{{ #define CPU_GO 0x0000 -#define CPU_RESET 0x0040 -#define CPU_INT 0x0080 -#define CPU_STEP 0x0100 -#define CPU_STALL 0x0200 -#define CPU_HALT 0x0400 -#define CPU_CLRCACHE 0x0800 +#define CPU_HALT 0x0001 +#define CPU_STALL 0x0002 +#define CPU_STEP 0x0004 +#define CPU_RESET 0x0008 +#define CPU_CLRCACHE 0x0010 +// (Reserved) 0x00e0 +#define CPU_SLEEPING 0x0100 +#define CPU_GIE 0x0200 +#define CPU_INT 0x0400 +#define CPU_BREAK 0x0800 +#define CPU_EXINT 0xfffff000 +// #define CPU_sR0 0x0000 #define CPU_sSP 0x000d #define CPU_sCC 0x000e @@ -154,58 +203,37 @@ #define CPU_uPC 0x001f #define RESET_ADDRESS @$[0x%08x](RESET_ADDRESS) - - +// }}} @SIM.CLOCK=clk @SIM.INCLUDE= #include "zipelf.h" @SIM.DEFINES= +//////////////////////////////////////////////////////////////////////////////// +// +// ZipCPU simulation defines +// {{{ #ifndef VVAR -#ifdef NEW_VERILATOR +#ifdef ROOT_VERILATOR + +#include "Vmain___024root.h" +#define VVAR(A) rootp->main__DOT_ ## A + +#elif defined(NEW_VERILATOR) #define VVAR(A) main__DOT_ ## A #else #define VVAR(A) v__DOT_ ## A #endif #endif -#define CPUVAR(A) VVAR(_swic__DOT__thecpu__DOT_ ## A) +#define OPT_PIPELINED +#define CPUVAR(A) VVAR(_swic__DOT__thecpu__DOT__core__DOT_ ## A) -#define cpu_reset VVAR(_swic__DOT__cmd_reset) #define cpu_break VVAR(_swic__DOT__cpu_break) -#define cpu_cmd_halt VVAR(_swic__DOT__cmd_halt) -#define cpu_ipc CPUVAR(_ipc) -#define cpu_pf_pc CPUVAR(_pf_pc) -#define cpu_upc CPUVAR(_SET_USER_PC__DOT__r_upc) -#define cpu_gie CPUVAR(_SET_GIE__DOT__r_gie) -#define cpu_iflags CPUVAR(_w_iflags) -#define cpu_uflags CPUVAR(_w_uflags) -#define cpu_regs CPUVAR(_regset) -#define cpu_cmd_addr VVAR(_swic__DOT__cmd_addr) -#define cpu_bus_err CPUVAR(_bus_err) -#define cpu_ibus_err CPUVAR(_ibus_err_flag) -#define cpu_ubus_err CPUVAR(_r_ubus_err_flag) -#define cpu_mem_rdaddr CPUVAR(_domem__DOT__rdaddr) -#define cpu_mem_wraddr CPUVAR(_domem__DOT__wraddr) -#define cpu_sim CPUVAR(_alu_sim) -#define cpu_op_valid CPUVAR(_op_valid) -#define cpu_alu_ce CPUVAR(_alu_ce) -#define cpu_new_pc CPUVAR(_new_pc) -#define cpu_sim_immv CPUVAR(_alu_sim_immv) -#define cpu_alu_pc_valid CPUVAR(_alu_pc_valid) -#define cpu_mem_pc_valid CPUVAR(_mem_pc_valid) -#ifdef OPT_PIPELINED -#define cpu_alu_pc CPUVAR(_GEN_ALU_PC__DOT__r_alu_pc) -#else -#define cpu_alu_pc CPUVAR(_op_pc) -#endif -#ifdef OPT_CIS -#define cpu_alu_phase CPUVAR(_GEN_ALU_PHASE__DOT__r_alu_phase) -#endif -#define cpu_wr_ce CPUVAR(_wr_reg_ce) -#define cpu_wr_reg_id CPUVAR(_wr_reg_id) -#define cpu_wr_gpreg CPUVAR(_wr_gpreg_vl) +#define cpu_gie CPUVAR(_SET_GIE__DOT__r_gie) +#define cpu_regs CPUVAR(_regset.m_storage) +// }}} @SIM.DEFNS= int m_cpu_bombed; @SIM.INIT= @@ -216,7 +244,10 @@ m_core->i_cpu_reset = 0; @SIM.METHODS= #ifdef @$(ACCESS) + // ZipCPU Access functions + // {{{ void loadelf(const char *elfname) { + // {{{ ELFSECTION **secpp, *secp; uint32_t entry; @@ -237,13 +268,25 @@ } } free(secpp); } + // }}} + void sim_write(unsigned addr, unsigned value) { + m_core->i_sim_write = 1; + m_core->i_sim_addr = (addr >> 2) & 0x07f; + m_core->i_sim_data = value; + tick(); + m_core->i_sim_write = 0; + m_core->i_sim_addr = 0; + m_core->i_sim_data = 0; + } + bool gie(void) { return (m_core->cpu_gie); } void dump(const uint32_t *regp) { + // {{{ uint32_t uccv, iccv, ipc, upc; fflush(stderr); fflush(stdout); @@ -254,10 +297,15 @@ printf("Supervisor mode\n"); printf("\n"); - iccv = m_core->cpu_iflags; - uccv = m_core->cpu_uflags; - ipc = m_core->cpu_ipc; - upc = m_core->cpu_upc; + iccv = 0; + uccv = 0; + ipc = 0; + upc = 0; + + // iccv = m_core->cpu_iflags; + // uccv = m_core->cpu_uflags; + // ipc = m_core->cpu_ipc; + // upc = m_core->cpu_upc; printf("sR0 : %08x ", regp[0]); printf("sR1 : %08x ", regp[1]); @@ -298,105 +346,15 @@ fflush(stderr); fflush(stdout); } + // }}} - - void execsim(const uint32_t imm) { - uint32_t *regp = m_core->cpu_regs; - int rbase; - rbase = (gie())?16:0; - - fflush(stdout); - if ((imm & 0x03fffff)==0) - return; - // fprintf(stderr, "SIM-INSN(0x%08x)\n", imm); - if ((imm & 0x0fffff)==0x00100) { - // SIM Exit(0) - close(); - exit(0); - } else if ((imm & 0x0ffff0)==0x00310) { - // SIM Exit(User-Reg) - int rcode, rnum; - rnum = (imm&0x0f)+16; - rcode = regp[rnum] & 0x0ff; - if ((m_core->cpu_wr_ce)&&(m_core->cpu_wr_reg_id==rnum)) - rcode = m_core->cpu_wr_gpreg; - close(); - exit(rcode); - } else if ((imm & 0x0ffff0)==0x00300) { - // SIM Exit(Reg) - int rcode, rnum; - rnum = (imm&0x0f)+rbase; - rcode = regp[rnum] & 0x0ff; - if ((m_core->cpu_wr_ce)&&(m_core->cpu_wr_reg_id==rnum)) - rcode = m_core->cpu_wr_gpreg; - close(); - exit(rcode); - } else if ((imm & 0x0fff00)==0x00100) { - // SIM Exit(Imm) - int rcode; - rcode = imm & 0x0ff; - close(); - exit(rcode); - } else if ((imm & 0x0fffff)==0x002ff) { - // Full/unconditional dump - printf("SIM-DUMP\n"); - dump(regp); - } else if ((imm & 0x0ffff0)==0x00200) { - // Dump a register - int rcode, rnum; - rnum = (imm&0x0f)+rbase; - rcode = regp[rnum]; - if ((m_core->cpu_wr_ce)&&(m_core->cpu_wr_reg_id==rnum)) - rcode = m_core->cpu_wr_gpreg; - printf("%8lu @%08x R[%2d] = 0x%08x\n", m_time_ps/1000, - m_core->cpu_ipc, rnum, rcode); - } else if ((imm & 0x0ffff0)==0x00210) { - // Dump a user register - int rcode, rnum; - rnum = (imm&0x0f)+16; - rcode = regp[rnum] & 0x0ff; - if ((m_core->cpu_wr_ce)&&(m_core->cpu_wr_reg_id==rnum)) - rcode = m_core->cpu_wr_gpreg; - printf("%8lu @%08x uR[%2d] = 0x%08x\n", m_time_ps/1000, - m_core->cpu_ipc, rnum, rcode); - } else if ((imm & 0x0ffff0)==0x00230) { - // SOUT[User Reg] - int rcode, rnum; - rnum = (imm&0x0f)+16; - rcode = regp[rnum]; - if ((m_core->cpu_wr_ce)&&(m_core->cpu_wr_reg_id==rnum)) - rcode = m_core->cpu_wr_gpreg; - printf("%c", rcode&0x0ff); - } else if ((imm & 0x0fffe0)==0x00220) { - // SOUT[User Reg] - int rcode, rnum; - rnum = (imm&0x0f)+rbase; - rcode = regp[rnum]; - if ((m_core->cpu_wr_ce)&&(m_core->cpu_wr_reg_id==rnum)) - rcode = m_core->cpu_wr_gpreg; - printf("%c", rcode&0x0ff); - } else if ((imm & 0x0fff00)==0x00400) { - // SOUT[Imm] - printf("%c", imm&0x0ff); - } else { // if ((insn & 0x0f7c00000)==0x77800000) - uint32_t immv = imm & 0x03fffff; - // Simm instruction that we dont recognize - // if (imm) - // printf("SIM 0x%08x\n", immv); - printf("SIM 0x%08x (ipc = %08x, upc = %08x)\n", immv, - m_core->cpu_ipc, - m_core->cpu_upc); - } fflush(stdout); - } + // }}} #endif // @$(ACCESS) @SIM.TICK= #ifdef @$(ACCESS) // ZipCPU Sim instruction support - if ((m_core->cpu_sim) - &&(!m_core->cpu_new_pc)) { - // - execsim(m_core->cpu_sim_immv); - } + // {{{ + // Sim instructions are handled in Verilog if (m_cpu_bombed) { if (m_cpu_bombed++ > 12) @@ -406,24 +364,78 @@ m_cpu_bombed++; dump(m_core->cpu_regs); } + // }}} #endif // @$(ACCESS) ## ## ## @RTL.MAKE.GROUP=ZIPCPU -@RTL.MAKE.FILES= zipcpu.v cpuops.v pfcache.v pipemem.v pfcache.v idecode.v - wbpriarbiter.v zipsystem.v zipcounter.v zipjiffies.v ziptimer.v - wbdmac.v icontrol.v wbwatchdog.v busdelay.v cpudefs.v +@RTL.MAKE.FILES= zipsystem.v zipcore.v zipwb.v cpuops.v pfcache.v pipemem.v + pfcache.v idecode.v wbpriarbiter.v zipsystem.v zipcounter.v + zipjiffies.v ziptimer.v wbdmac.v icontrol.v wbwatchdog.v + busdelay.v @RTL.MAKE.SUBD= cpu ## -## @PREFIX=zip_dbg +################################################################################ +## +## ZipCPU debug port +## {{{ +## (was once @PREFIX=zip_dbg) @SLAVE.TYPE=OTHER @SLAVE.BUS=wbu @SLAVE.ORDER=1000000 -@NADDR=2 -## +@SLAVE.PREFIX=@$(PREFIX)_dbg +@SLAVE.ANSPREFIX=dbg_ +@IANSI=i_ +@OANSI=o_ +@NADDR=128 +@REGS.N=36 +@REGS.NOTE=// ZipCPU control/debug registers +@REGS.0=0 R_ZIPCTRL CPU ZIPCTRL +@REGS.1=32 R_ZIPREGS ZIPREGS +@REGS.2=32 R_ZIPS0 SR0 +@REGS.3=33 R_ZIPS1 SR1 +@REGS.4=34 R_ZIPS2 SR2 +@REGS.5=35 R_ZIPS3 SR3 +@REGS.6=36 R_ZIPS4 SR4 +@REGS.7=37 R_ZIPS5 SR5 +@REGS.8=38 R_ZIPS6 SR6 +@REGS.9=39 R_ZIPS7 SR7 +@REGS.10=40 R_ZIPS8 SR8 +@REGS.11=41 R_ZIPS9 SR9 +@REGS.12=42 R_ZIPS10 SR10 +@REGS.13=43 R_ZIPS11 SR11 +@REGS.14=44 R_ZIPS12 SR12 +@REGS.15=45 R_ZIPSSP SSP SR13 +@REGS.16=46 R_ZIPCC ZIPCC CC SCC SR14 +@REGS.17=47 R_ZIPPC ZIPPC PC SPC SR15 +@REGS.18=48 R_ZIPUSER ZIPUSER +@REGS.19=48 R_ZIPU0 UR0 +@REGS.20=49 R_ZIPU1 UR1 +@REGS.21=50 R_ZIPU2 UR2 +@REGS.22=51 R_ZIPU3 UR3 +@REGS.23=52 R_ZIPU4 UR4 +@REGS.24=53 R_ZIPU5 UR5 +@REGS.25=54 R_ZIPU6 UR6 +@REGS.26=55 R_ZIPU7 UR7 +@REGS.27=56 R_ZIPU8 UR8 +@REGS.28=57 R_ZIPU9 UR9 +@REGS.29=58 R_ZIPU10 SR10 +@REGS.30=59 R_ZIPU11 SR11 +@REGS.31=60 R_ZIPU12 SR12 +@REGS.32=61 R_ZIPUSP USP UR13 +@REGS.33=62 R_ZIPUCC ZIPUCC UCC +@REGS.34=63 R_ZIPUPC ZIPUPC UPC +@REGS.35=64 R_ZIPSYSTEM ZIPSYSTEM ZIPSYS +## +## +## }}} +################################################################################ ## +## Interrupt definitions +## {{{ +################################################################################ @PREFIX=syspic @PIC.BUS= sys_int_vector @PIC.MAX=15 @@ -502,5 +514,5 @@ @INT.MTC.PIC=altpic @INT.MTC.ID=7 ## -## -## +## }}} +################################################################################ diff --git a/doc/20210524-gcc-10-cpu-test.png b/doc/20210524-gcc-10-cpu-test.png new file mode 100644 index 0000000..d587002 Binary files /dev/null and b/doc/20210524-gcc-10-cpu-test.png differ diff --git a/doc/Makefile b/doc/Makefile index 8770d2b..a8d7c61 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: Makefile -## +## {{{ ## Project: ZBasic, a generic toplevel impl using the full ZipCPU ## ## Purpose: To coordinate the build of documentation PDFs from their @@ -20,11 +20,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2015-2016, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2015-2021, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -37,14 +37,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} all: gpl pdf: gpl DSRC := src diff --git a/doc/hellostep.png b/doc/hellostep.png new file mode 100644 index 0000000..a1fd934 Binary files /dev/null and b/doc/hellostep.png differ diff --git a/doc/lockcheck.png b/doc/lockcheck.png new file mode 100644 index 0000000..6fa862b Binary files /dev/null and b/doc/lockcheck.png differ diff --git a/doc/src/gqtekspec.cls b/doc/src/gqtekspec.cls index ac53f2c..193e345 100644 --- a/doc/src/gqtekspec.cls +++ b/doc/src/gqtekspec.cls @@ -1,6 +1,6 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%/ % -% Copyright (C) 2015, Gisselquist Technology, LLC +% Copyright (C) 2015-2021, Gisselquist Technology, LLC % % This template is free software: you can redistribute it and/or modify it % under the terms of the GNU General Public License as published by the diff --git a/mkdatev.pl b/mkdatev.pl index 11b19a2..1ab0151 100755 --- a/mkdatev.pl +++ b/mkdatev.pl @@ -2,7 +2,7 @@ ################################################################################ ## ## Filename: mkdatev.pl -## +## {{{ ## Project: ZBasic, a generic toplevel impl using the full ZipCPU ## ## Purpose: This file creates a file containing a `define DATESTAMP @@ -13,11 +13,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2015-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2015-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -30,14 +30,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} $now = time; ($sc,$mn,$nhr,$ndy,$nmo,$nyr,$nwday,$nyday,$nisdst) = localtime($now); @@ -50,7 +50,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: builddate.v -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This file records the date of the last build. Running "make" @@ -62,11 +62,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -79,14 +79,14 @@ // with this program. (It's in the \$(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} `ifndef DATESTAMP EOM diff --git a/rtl/Makefile b/rtl/Makefile index c78611c..c8fabf1 100644 --- a/rtl/Makefile +++ b/rtl/Makefile @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: rtl/Makefile -## +## {{{ ## Project: ZBasic, a generic toplevel impl using the full ZipCPU ## ## Purpose: To direct the Verilator build of the SoC sources. The result @@ -13,11 +13,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2015-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2015-2021, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -30,15 +30,15 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## -all: test cpudefs.h design.h +## }}} +all: test design.h YYMMDD=`date +%Y%m%d` CXX := g++ FBDIR := . @@ -50,7 +50,7 @@ VERILATOR := verilator else VERILATOR := $(VERILATOR_ROOT)/bin/verilator endif -VFLAGS = -Wall -Wno-TIMESCALEMOD --MMD -O3 --trace -Mdir $(VDIRFB) $(AUTOVDIRS) -cc +VFLAGS = -Wall -Wno-TIMESCALEMOD --threads 8 --MMD -O3 --trace -Mdir $(VDIRFB) $(AUTOVDIRS) -cc -include make.inc @@ -76,15 +76,6 @@ $(VDIRFB)/Vmain.mk: $(VDIRFB)/Vmain.h $(VDIRFB)/Vmain.h: main.v $(VFLIST) $(VERILATOR) $(VFLAGS) main.v -cpudefs.h: cpu/cpudefs.v - @echo "Building cpudefs.h" - @echo "// " > $@ - @echo "// Do not edit this file, it is automatically generated!" >> $@ - @echo "// To generate this file, \"make cpudefs.h\" in the rtl directory." >> $@ - @echo "// " >> $@ - @sed -e '{ s/^`/#/ }' $< | sed -e ' s/cpudefs.v/cpudefs.h/' >> $@ - @echo >> $@ - design.h: main.v @echo "Building design.h" @echo "// " > $@ @@ -104,7 +95,7 @@ design.h: main.v @echo "#endif // DESIGN_H" >> $@ $(VOBJ)/V%__ALL.a: $(VOBJ)/V%.mk - $(SUBMAKE) V$*.mk + +$(SUBMAKE) V$*.mk .PHONY: archive: diff --git a/rtl/addrdecode.v b/rtl/addrdecode.v index ef8c691..e988ea6 100644 --- a/rtl/addrdecode.v +++ b/rtl/addrdecode.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: addrdecode.v -// +// {{{ // Project: WB2AXIPSP: bus bridges and other odds and ends // // Purpose: @@ -10,9 +10,9 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2019-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2019-2022, Gisselquist Technology, LLC +// {{{ // This file is part of the WB2AXIP project. // // The WB2AXIP project contains free software and gateware, licensed under the @@ -32,48 +32,70 @@ // // `default_nettype none -// -module addrdecode(i_clk, i_reset, i_valid, o_stall, i_addr, i_data, - o_valid, i_stall, o_decode, o_addr, o_data); - parameter NS=8; - parameter AW = 32, DW=32+32/8+1+1; - // - // SLAVE_ADDR contains address assignments for each of the various - // slaves we are adjudicating between. - parameter [NS*AW-1:0] SLAVE_ADDR = { - { 3'b111, {(AW-3){1'b0}}}, - { 3'b110, {(AW-3){1'b0}}}, - { 3'b101, {(AW-3){1'b0}}}, - { 3'b100, {(AW-3){1'b0}}}, - { 3'b011, {(AW-3){1'b0}}}, - { 3'b010, {(AW-3){1'b0}}}, - { 4'b0010, {(AW-4){1'b0}}}, - { 4'b0000, {(AW-4){1'b0}}} }; - // - // SLAVE_MASK contains a mask of those address bits in SLAVE_ADDR - // which are relevant. It shall be true that if - // !SLAVE_MASK[k] then !SLAVE_ADDR[k], for any bits of k - parameter [NS*AW-1:0] SLAVE_MASK = (NS <= 1) ? 0 - : { {(NS-2){ 3'b111, {(AW-3){1'b0}}}}, - {(2){ 4'b1111, {(AW-4){1'b0}} }} }; - // - // ACCESS_ALLOWED is a bit-wise mask indicating which slaves may get - // access to the bus. If ACCESS_ALLOWED[slave] is true, then a master - // can connect to the slave via this method. This parameter is - // primarily here to support AXI (or other similar buses) which may have - // separate accesses for both read and write. By using this, a - // read-only slave can be connected, which would also naturally create - // an error on any attempt to write to it. - parameter [NS-1:0] ACCESS_ALLOWED = -1; - // - // If OPT_REGISTERED is set, address decoding will take an extra clock, - // and will register the results of the decoding operation. - parameter [0:0] OPT_REGISTERED = 0; - // - // If OPT_LOWPOWER is set, then whenever the output is not valid, any - // respective data linse will also be forced to zero in an effort - // to minimize power. - parameter [0:0] OPT_LOWPOWER = 0; +// }}} +module addrdecode #( + // {{{ + parameter NS=8, + parameter AW = 32, DW=32+32/8+1+1, + // + // SLAVE_ADDR contains address assignments for each of the + // various slaves we are adjudicating between. + parameter [NS*AW-1:0] SLAVE_ADDR = { + { 3'b111, {(AW-3){1'b0}} }, + { 3'b110, {(AW-3){1'b0}} }, + { 3'b101, {(AW-3){1'b0}} }, + { 3'b100, {(AW-3){1'b0}} }, + { 3'b011, {(AW-3){1'b0}} }, + { 3'b010, {(AW-3){1'b0}} }, + { 4'b0010, {(AW-4){1'b0}} }, + { 4'b0000, {(AW-4){1'b0}} }}, + // + // SLAVE_MASK contains a mask of those address bits in + // SLAVE_ADDR which are relevant. It shall be true that if + // !SLAVE_MASK[k] then !SLAVE_ADDR[k], for any bits of k + parameter [NS*AW-1:0] SLAVE_MASK = (NS <= 1) ? 0 + : { {(NS-2){ 3'b111, {(AW-3){1'b0}} }}, + {(2){ 4'b1111, {(AW-4){1'b0}} }} }, + // + // ACCESS_ALLOWED is a bit-wise mask indicating which slaves + // may get access to the bus. If ACCESS_ALLOWED[slave] is true, + // then a master can connect to the slave via this method. This + // parameter is primarily here to support AXI (or other similar + // buses) which may have separate accesses for both read and + // write. By using this, a read-only slave can be connected, + // which would also naturally create an error on any attempt to + // write to it. + parameter [NS-1:0] ACCESS_ALLOWED = -1, + // + // If OPT_REGISTERED is set, address decoding will take an extra + // clock, and will register the results of the decoding + // operation. + parameter [0:0] OPT_REGISTERED = 0, + // + // If OPT_LOWPOWER is set, then whenever the output is not + // valid, any respective data linse will also be forced to zero + // in an effort to minimize power. + parameter [0:0] OPT_LOWPOWER = 0 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // + input wire i_valid, + output reg o_stall, + input wire [AW-1:0] i_addr, + input wire [DW-1:0] i_data, + // + output reg o_valid, + input wire i_stall, + output reg [NS:0] o_decode, + output reg [AW-1:0] o_addr, + output reg [DW-1:0] o_data + // }}} + ); + + // Local declarations + // {{{ // // OPT_NONESEL controls whether or not the address lines are fully // proscribed, or whether or not a "no-slave identified" slave should @@ -83,63 +105,67 @@ module addrdecode(i_clk, i_reset, i_valid, o_stall, i_addr, i_data, localparam [0:0] OPT_NONESEL = (!ACCESS_ALLOWED[0]) || (SLAVE_MASK[AW-1:0] != 0); // - input wire i_clk, i_reset; - // - input wire i_valid; - output reg o_stall; - input wire [AW-1:0] i_addr; - input wire [DW-1:0] i_data; - // - output reg o_valid; - input wire i_stall; - output reg [NS:0] o_decode; - output reg [AW-1:0] o_addr; - output reg [DW-1:0] o_data; - - reg [NS:0] request; + wire [NS:0] request; reg [NS-1:0] prerequest; - reg none_sel; integer iM; + // }}} + // prerequest + // {{{ always @(*) for(iM=0; iM 1 && |prerequest[NS-1:1])) - request[0] = 1'b0; + r_request[0] = 1'b0; end + assign request[NS-1:0] = r_request; + // }}} end else if (NS == 1) - begin - - always @(*) - request = { 1'b0, i_valid }; - + begin : SINGLE_SLAVE + // {{{ + assign request[0] = i_valid; + // }}} end else begin + // {{{ + reg [NS-1:0] r_request; always @(*) begin for(iM=0; iM 1 && |prerequest[NS-1:1])) - request[0] = 1'b0; + r_request[0] = 1'b0; end + assign request[NS-1:0] = r_request; + // }}} end endgenerate + // }}} + // request[NS] + // {{{ generate if (OPT_NONESEL) begin + reg r_request_NS, r_none_sel; + always @(*) begin // Let's assume nothing's been selected, and then check @@ -148,29 +174,38 @@ module addrdecode(i_clk, i_reset, i_valid, o_stall, i_addr, i_data, // Note that none_sel will be considered an error // condition in the follow-on processing. Therefore // it's important to clear it if no request is pending. - none_sel = i_valid && (prerequest == 0); + r_none_sel = i_valid && (prerequest == 0); // // request[NS] indicates a request for a non-existent // slave. A request that should (eventually) return a // bus error // - request[NS] = none_sel; + r_request_NS = r_none_sel; end + + assign request[NS] = r_request_NS; end else begin - always @(*) - { request[NS], none_sel } = 2'b00; + assign request[NS] = 1'b0; end endgenerate + // }}} + // o_valid, o_addr, o_data, o_decode, o_stall + // {{{ generate if (OPT_REGISTERED) begin + + // o_valid + // {{{ initial o_valid = 0; always @(posedge i_clk) if (i_reset) o_valid <= 0; else if (!o_stall) o_valid <= i_valid; + // }}} - + // o_addr, o_data + // {{{ initial o_addr = 0; initial o_data = 0; always @(posedge i_clk) @@ -188,7 +223,10 @@ module addrdecode(i_clk, i_reset, i_valid, o_stall, i_addr, i_data, o_addr <= 0; o_data <= 0; end + // }}} + // o_decode + // {{{ initial o_decode = 0; always @(posedge i_clk) if (i_reset) @@ -198,9 +236,13 @@ module addrdecode(i_clk, i_reset, i_valid, o_stall, i_addr, i_data, o_decode <= request; else if (OPT_LOWPOWER && !i_stall) o_decode <= 0; + // }}} + // o_stall + // {{{ always @(*) o_stall = (o_valid && i_stall); + // }}} end else begin always @(*) @@ -213,12 +255,30 @@ module addrdecode(i_clk, i_reset, i_valid, o_stall, i_addr, i_data, o_decode = request; end + // Make Verilator happy + // {{{ // verilator lint_off UNUSED wire unused; - assign unused = &{ 1'b0, i_clk, i_reset }; + assign unused = &{ 1'b0, +`ifdef VERILATOR + // Can't declare the clock as unused for formal, + // lest it not be recognized as *the* clock + i_clk, +`endif + i_reset }; // verilator lint_on UNUSED + // }}} end endgenerate - + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL reg f_past_valid; initial f_past_valid = 0; @@ -295,6 +355,8 @@ module addrdecode(i_clk, i_reset, i_valid, o_stall, i_addr, i_data, if (!ACCESS_ALLOWED[iM]) assert(!o_decode[iM]); + // LOWPOWER check + // {{{ generate if (OPT_LOWPOWER && OPT_REGISTERED) begin always @(*) @@ -305,11 +367,12 @@ module addrdecode(i_clk, i_reset, i_valid, o_stall, i_addr, i_data, assert(o_data == 0); end end endgenerate + // }}} // // The output decoded value may only ever have one value high, // never more--i.e. $onehot0 - // + // {{{ `ifdef VERIFIC always @(*) assert($onehot0(request)); @@ -327,15 +390,18 @@ module addrdecode(i_clk, i_reset, i_valid, o_stall, i_addr, i_data, if (request != 0) assert(onehot_request); `endif + // }}} //////////////////////////////////////////////////////////////////////// // // Cover properties + // {{{ + //////////////////////////////////////////////////////////////////////// // - // Make sure all addresses are reachable // - //////////////////////////////////////////////////////////////////////// + // + // Make sure all addresses are reachable // reg [NS:0] f_reached; @@ -376,8 +442,9 @@ module addrdecode(i_clk, i_reset, i_valid, o_stall, i_addr, i_data, cover($changed(o_decode)); end endgenerate - + // }}} `endif // FORMAL +// }}} endmodule `ifndef YOSYS `default_nettype wire diff --git a/rtl/builddate.v b/rtl/builddate.v index 8ee3720..927c4df 100644 --- a/rtl/builddate.v +++ b/rtl/builddate.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: builddate.v -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This file records the date of the last build. Running "make" @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,16 +30,16 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} `ifndef DATESTAMP -`define DATESTAMP 32'h20201028 -`define BUILDTIME 32'h00184458 +`define DATESTAMP 32'h20221027 +`define BUILDTIME 32'h00092234 `endif // diff --git a/rtl/cpu/axidcache.v b/rtl/cpu/axidcache.v new file mode 100644 index 0000000..0e8421f --- /dev/null +++ b/rtl/cpu/axidcache.v @@ -0,0 +1,1490 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: axidcache.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: A data cache built using AXI as the underlying protocol +// +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2020-2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +// +//////////////////////////////////////////////////////////////////////////////// +// +// +`default_nettype none +// }}} +module axidcache #( + // {{{ + // AXI Setup: address width, data width, ID width, and ID + // {{{ + parameter C_AXI_ADDR_WIDTH = 32, + parameter C_AXI_DATA_WIDTH = 64, + parameter C_AXI_ID_WIDTH = 1, + parameter [C_AXI_ID_WIDTH-1:0] AXI_ID = 0, + // }}} + // LGCACHELEN + // {{{ + // The log (base 2) of the cache size in bytes. + parameter LGCACHELEN = 8 + $clog2(C_AXI_DATA_WIDTH/8), + localparam LGCACHEWORDS= LGCACHELEN-$clog2(C_AXI_DATA_WIDTH/8), + // }}} + // LGNLINES + // {{{ + // The log (base 2) of the number of cache lines. A cache line + // is the minimum amount of data read on a miss. Each cache + // line has a valid signal and a tag associated with it. + parameter LGNLINES = (LGCACHEWORDS-3), + // }}} + parameter [0:0] SWAP_WSTRB = 1'b0, + // OPT_SIGN_EXTEND: When returning values to the CPU that are + // {{{ + // less than word sized in length, if OPT_SIGN_EXTEND is true + // these values will be sign extended. + parameter [0:0] OPT_SIGN_EXTEND = 1'b0, + // }}} + parameter NAUX = 5, + // OPT_PIPE: Set to 1 to allow multiple outstanding transactions + // {{{ + // This is primarily used by write requests. Read requests + // will only ever read one cache line at a time. Non-cache + // reads are only ever done (at present) as singletons. + parameter [0:0] OPT_PIPE = 1'b0, + // }}} + // OPT_WRAP: True if using AXI WRAP mode. With AXI WRAP mode, + // {{{ + // a cache read result will return its value before the entire + // cache line read has been completed + parameter [0:0] OPT_WRAP = 1'b0, + // }}} + // OPT_LOWPOWER: If true, sets unused AXI values to all zeros, + // {{{ + // or, in the case of AxSIZE, to 3'd2. This is to keep things + // from toggling if they don't need to. + parameter [0:0] OPT_LOWPOWER = 1'b0, + // }}} + // OPT_LOCK: Set to 1 in order to support exclusive access. + // {{{ + parameter [0:0] OPT_LOCK = 1'b0, + // }}} + // Local parameters, mostly abbreviations + // {{{ + // Verilator lint_off UNUSED + localparam LGPIPE = (OPT_PIPE) ? 4:2, + // Verilator lint_on UNUSED + localparam [0:0] OPT_DUAL_READ_PORT = 1'b1, + localparam [1:0] DC_IDLE = 2'b00, + DC_WRITE = 2'b01, + DC_READS = 2'b10, + DC_READC = 2'b11, + localparam AW = C_AXI_ADDR_WIDTH, + localparam DW = C_AXI_DATA_WIDTH, + localparam IW = C_AXI_ID_WIDTH, + localparam AXILSB = $clog2(C_AXI_DATA_WIDTH/8), + localparam CS = LGCACHELEN - AXILSB, // Cache size, in wrds + localparam LS = CS-LGNLINES, // Cache lines, lg_2 + localparam TW = AW-(CS+AXILSB) // Tag width + // }}} + // }}} + ) ( + // {{{ + input wire S_AXI_ACLK, S_AXI_ARESETN, + input wire i_cpu_reset, i_clear, + // Interface from the CPU + // {{{ + input wire i_pipe_stb, i_lock, + input wire [2:0] i_op, + input wire [AW-1:0] i_addr, + input wire [AW-1:0] i_restart_pc, + input wire [31:0] i_data, + input wire [NAUX-1:0] i_oreg, + // Outputs, going back to the CPU + output reg o_busy, o_rdbusy, + output wire o_pipe_stalled, + output reg o_valid, o_err, + output reg [NAUX-1:0] o_wreg, + output reg [31:0] o_data, + // }}} + // AXI bus interface + // {{{ + // Write address + // {{{ + output wire M_AXI_AWVALID, + input wire M_AXI_AWREADY, + // verilator coverage_off + output wire [IW-1:0] M_AXI_AWID, // = constant + // verilator coverage_on + output wire [AW-1:0] M_AXI_AWADDR, + // verilator coverage_off + output wire [7:0] M_AXI_AWLEN, // = const 0 + // verilator coverage_on + output wire [2:0] M_AXI_AWSIZE, + output wire [1:0] M_AXI_AWBURST, + output wire M_AXI_AWLOCK, + output wire [3:0] M_AXI_AWCACHE, + // verilator coverage_off + output wire [2:0] M_AXI_AWPROT, + output wire [3:0] M_AXI_AWQOS, + // verilator coverage_on + // }}} + // Write data + // {{{ + output wire M_AXI_WVALID, + input wire M_AXI_WREADY, + output wire [DW-1:0] M_AXI_WDATA, + output wire [DW/8-1:0] M_AXI_WSTRB, + output wire M_AXI_WLAST, + // }}} + // Write return + // {{{ + input wire M_AXI_BVALID, + output wire M_AXI_BREADY, + // verilator coverage_off + input wire [IW-1:0] M_AXI_BID, + // verilator coverage_on + input wire [1:0] M_AXI_BRESP, + // }}} + // Read address + // {{{ + output wire M_AXI_ARVALID, + input wire M_AXI_ARREADY, + // verilator coverage_off + output wire [IW-1:0] M_AXI_ARID, + // verilator coverage_on + output wire [AW-1:0] M_AXI_ARADDR, + output wire [7:0] M_AXI_ARLEN, + output wire [2:0] M_AXI_ARSIZE, + output wire [1:0] M_AXI_ARBURST, + output wire M_AXI_ARLOCK, + output wire [3:0] M_AXI_ARCACHE, + // verilator coverage_off + output wire [2:0] M_AXI_ARPROT, + output wire [3:0] M_AXI_ARQOS, + // verilator coverage_on + // }}} + // Read data returned + // {{{ + input wire M_AXI_RVALID, + output wire M_AXI_RREADY, + // verilator coverage_off + input wire [IW-1:0] M_AXI_RID, + // verilator coverage_on + input wire [DW-1:0] M_AXI_RDATA, + input wire M_AXI_RLAST, + input wire [1:0] M_AXI_RRESP + // }}} + // }}} + // }}} + ); + + // Declarations + // {{{ + localparam [1:0] INCR = 2'b01, + WRAP = 2'b10; + localparam [1:0] OKAY = 2'b00, + EXOKAY = 2'b01; + // Verilator lint_off UNUSED + localparam DSZ = 2; + // Verilator lint_on UNUSED + + // The cache itself + // {{{ + reg [(1<<(CS-LS))-1:0] cache_valid; + reg [TW-1:0] cache_tag [0:(1<= (1<= 1) && !M_AXI_BVALID + && (!M_AXI_RVALID || !M_AXI_RLAST)) + flushing <= 1'b1; + end + // }}} + + // Read handling + // {{{ + // Read state machine + // {{{ + initial state = DC_IDLE; + initial set_vflag = 1'b0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + begin + // {{{ + cache_valid <= 0; + // end_of_line <= 1'b0; + // last_line_stb <= 1'b0; + state <= DC_IDLE; + set_vflag <= 1'b0; + // }}} + end else begin + // {{{ + set_vflag <= 1'b0; + if (set_vflag) + cache_valid[r_cline] <= 1'b1; + + case(state) + DC_IDLE: begin + // {{{ + good_cache_read <= 1'b1; + if (i_pipe_stb && i_write && !misaligned) + state <= DC_WRITE; + else if (w_cache_miss) + state <= DC_READC; + else if (i_pipe_stb && i_read && !cachable_request + && !misaligned) + state <= DC_READS; + + if (i_cpu_reset) + state <= DC_IDLE; + end + // }}} + DC_READC: begin // Read cache line + // {{{ + if (M_AXI_RVALID) + begin + good_cache_read + <= good_cache_read && !M_AXI_RRESP[1]; + cache_valid[r_cline] <= 1'b0; + end + if (M_AXI_RVALID && M_AXI_RLAST) + begin + state <= DC_IDLE; + set_vflag <= !i_cpu_reset && !i_clear && !flushing && !M_AXI_RRESP[1] && good_cache_read; + end end + // }}} + DC_READS: begin // Read single value + // {{{ + if (M_AXI_RVALID && last_ack) + state <= DC_IDLE; + end + // }}} + DC_WRITE: begin + // {{{ + if (M_AXI_BVALID && M_AXI_BREADY + && (!OPT_PIPE || (last_ack && !i_pipe_stb))) + state <= DC_IDLE; + end + // }}} + endcase + + if (i_clear || i_cpu_reset) + cache_valid <= 0; + if (OPT_PIPE && M_AXI_BVALID && M_AXI_BRESP[1]) + cache_valid <= 0; + // }}} + end + // }}} + + // axi_axlock + // {{{ + generate if (OPT_LOCK) + begin : GEN_AXLOCK + + reg r_arlock, r_awlock; + reg [2:0] r_read_in_cache; + + initial r_arlock = 1'b0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + r_arlock <= 1'b0; + else if (i_pipe_stb) + begin + r_arlock <= i_lock; + + if (misaligned || i_write) + r_arlock <= 1'b0; + end else if (M_AXI_RVALID) + r_arlock <= 1'b0; + + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN || r_rd_pending || M_AXI_RVALID) + r_read_in_cache <= 3'b00; + else if (i_pipe_stb) + begin + r_read_in_cache <= 3'b00; + r_read_in_cache[0] <= address_is_cachable && i_read && i_lock; + end else if (r_read_in_cache != 0) + begin + r_read_in_cache[2:1] <= { r_read_in_cache[1:0] }; + if (r_read_in_cache[1] && ((w_tag != r_ctag) || !w_v)) + r_read_in_cache <= 0; + // && (w_tag == r_ctag) && w_v + end + + initial r_awlock = 1'b0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + r_awlock <= 1'b0; + else if (i_pipe_stb) + begin + r_awlock <= i_lock; + + if (misaligned || i_read) + r_awlock <= 1'b0; + end else if (M_AXI_BVALID) + r_awlock <= 1'b0; + + assign locked_write_in_progress = r_awlock; + assign locked_read_in_progress = r_arlock; + assign locked_read_in_cache = r_read_in_cache[2]; + assign axi_awlock = r_awlock; + assign axi_arlock = r_arlock; + end else begin + assign axi_awlock = 1'b0; + assign axi_arlock = 1'b0; + assign locked_write_in_progress = 1'b0; + assign locked_read_in_progress = 1'b0; + assign locked_read_in_cache = 1'b0; + end endgenerate + // }}} + + // M_AXI_ARVALID, axi_arvalid + // {{{ + always @(posedge S_AXI_ACLK) + suppress_miss <= { suppress_miss[0] || set_vflag, set_vflag }; + always @(*) + w_cache_miss = r_cache_miss && state == DC_IDLE + && !r_dvalid && !o_err + && wcache_strb == 0 && !suppress_miss[1]; + + initial axi_arvalid = 0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + axi_arvalid <= 0; + else if (!M_AXI_ARVALID || M_AXI_ARREADY) + begin + axi_arvalid <= 1'b0; + if (i_pipe_stb && i_read && !misaligned + && ((OPT_LOCK && i_lock) || !address_is_cachable)) + axi_arvalid <= 1; + if (w_cache_miss) + axi_arvalid <= 1; + if (i_cpu_reset) + axi_arvalid <= 0; + end + + assign M_AXI_ARVALID = axi_arvalid; + // }}} + + // M_AXI_ARADDR, M_AXI_ARSIZE, M_AXI_ARLEN + // {{{ + initial axi_araddr = 0; + initial axi_arsize = 3'd2; + initial axi_arlen = 0; + always @(posedge S_AXI_ACLK) + if (OPT_LOWPOWER && !S_AXI_ARESETN) + begin + // {{{ + axi_araddr <= 0; + axi_arsize <= 3'd2; + axi_arlen <= 0; + // }}} + end else if (!M_AXI_ARVALID || M_AXI_ARREADY) + begin + axi_arlen <= 0; + if (r_cache_miss) + begin + // {{{ + if (OPT_WRAP) + axi_araddr <= { r_ctag, r_caddr[CS-1:0], + {(AXILSB){1'b0}} }; + else + axi_araddr <= { r_ctag, r_cline, {(LS+AXILSB){1'b0}} }; + axi_arlen <= (1 << LS) - 1; + axi_arsize <= AXILSB[2:0]; + // }}} + end else begin + // {{{ + axi_araddr <= i_addr; + axi_arlen <= 0; + casez(i_op[2:1]) + 2'b0?: axi_arsize <= 3'd2; + 2'b10: axi_arsize <= 3'd1; + 2'b11: axi_arsize <= 3'd0; + // default: axi_arsize <= 3'd2; (Messes w/ coverage) + endcase + + if (SWAP_WSTRB) + begin + axi_araddr[AXILSB-1:0] <= ~i_addr[AXILSB-1:0]; + axi_araddr[1:0] <= 0; + axi_arsize <= 3'b010; + end + // }}} + end + + if (OPT_LOWPOWER && (i_cpu_reset || (!w_cache_miss + && (!i_pipe_stb || i_op[0] || misaligned + || (address_is_cachable && !i_lock))))) + begin + // {{{ + axi_araddr <= 0; + axi_arlen <= 0; + axi_arsize <= 3'd2; + // }}} + end + end + + assign axi_tag = axi_araddr[AW-1:AW-TW]; + assign axi_line = axi_araddr[AXILSB+LS +: CS-LS]; + + assign M_AXI_ARADDR = axi_araddr; + assign M_AXI_ARLEN = axi_arlen; + assign M_AXI_ARSIZE = axi_arsize; + // }}} + + // M_AXI_ARBURST + // {{{ + generate if (OPT_WRAP) + begin : GEN_ARBURST + reg r_wrap; + + initial r_wrap = 1'b0; + always @(posedge S_AXI_ACLK) + if (OPT_LOWPOWER && !S_AXI_ARESETN) + begin + // {{{ + r_wrap <= 1'b0; + // }}} + end else if (!M_AXI_ARVALID || M_AXI_ARREADY) + begin + // {{{ + r_wrap <= 1'b0; + if (r_cache_miss) + r_wrap <= 1'b1; + // }}} + end + + assign axi_arburst = r_wrap ? WRAP : INCR; + + end else begin : NO_WRAPBURST + + assign axi_arburst = INCR; + end endgenerate + + // }}} + // }}} + + // Writes always go straight to the bus + // {{{ + + // M_AXI_AWVALID, axi_awvalid + // {{{ + initial axi_awvalid = 0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + axi_awvalid <= 1'b0; + else if (!M_AXI_AWVALID || M_AXI_AWREADY) + begin + axi_awvalid <= i_pipe_stb && i_op[0] && !misaligned; + if (M_AXI_BVALID && M_AXI_BRESP[1]) + axi_awvalid <= 1'b0; + if (i_cpu_reset || flushing) + axi_awvalid <= 1'b0; + end + + assign M_AXI_AWVALID = axi_awvalid; + // }}} + + // M_AXI_AWADDR + // {{{ + initial axi_awaddr = 0; + initial axi_awsize = 3'd2; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN && OPT_LOWPOWER) + begin + axi_awaddr <= 0; + axi_awsize <= 3'd2; + end else if (!M_AXI_AWVALID || M_AXI_AWREADY) + begin + axi_awaddr <= i_addr; + + casez(i_op[2:1]) + 2'b0?: axi_awsize <= 3'd2; + 2'b10: axi_awsize <= 3'd1; + 2'b11: axi_awsize <= 3'd0; + // default: axi_awsize <= 3'd2; (Unused, messes w/ covr) + endcase + + + if (SWAP_WSTRB) + begin + // axi_awaddr[AXILSB-1:0] <= ~i_addr[AXILSB-1:0]; + axi_awaddr[1:0] <= 0; + axi_awsize <= 3'd2; + end + + if (OPT_LOWPOWER && (!i_pipe_stb || !i_op[0] || misaligned + || i_cpu_reset || flushing + || (M_AXI_BVALID && M_AXI_BRESP[1]))) + begin + axi_awaddr <= 0; + axi_awsize <= 3'd2; + end + end + + assign M_AXI_AWADDR = axi_awaddr; + assign M_AXI_AWSIZE = axi_awsize; + // }}} + + // M_AXI_WVALID + // {{{ + initial axi_wvalid = 0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + axi_wvalid <= 0; + else if (!M_AXI_WVALID || M_AXI_WREADY) + begin + axi_wvalid <= i_pipe_stb && i_op[0] && !misaligned; + if (M_AXI_BVALID && M_AXI_BRESP[1]) + axi_wvalid <= 1'b0; + if (i_cpu_reset || flushing) + axi_wvalid <= 1'b0; + end + + assign M_AXI_WVALID = axi_wvalid; + // }}} + + // M_AXI_WDATA, M_AXI_WSTRB + // {{{ + initial axi_wdata = 0; + initial axi_wstrb = 0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN && OPT_LOWPOWER) + begin + axi_wdata <= 0; + axi_wstrb <= 0; + end else if (!M_AXI_WVALID || M_AXI_WREADY) + begin + + // WDATA + // {{{ + if (SWAP_WSTRB) + begin + // {{{ + casez(i_op[2:1]) + // Write a 16b half-word + 2'b10: axi_wdata <= { i_data[15:0], + {(C_AXI_DATA_WIDTH-16){1'b0}} } + >> (8*i_addr[AXILSB-1:0]); + // Write an 8b half-word + 2'b11: axi_wdata <= { i_data[7:0], + {(C_AXI_DATA_WIDTH-8){1'b0}} } + >> (8*i_addr[AXILSB-1:0]); + default: axi_wdata <= { i_data, + {(C_AXI_DATA_WIDTH-32){1'b0}} } + >> (8*i_addr[AXILSB-1:0]); + endcase + // }}} + end else begin + // {{{ + casez(i_op[2:1]) + // Write a 16b half-word + 2'b10: axi_wdata <= { {(C_AXI_DATA_WIDTH-16){1'b0}}, + i_data[15:0] } << (8*i_addr[AXILSB-1:0]); + // Write an 8b half-word + 2'b11: axi_wdata <= { {(C_AXI_DATA_WIDTH-8){1'b0}}, + i_data[7:0] } << (8*i_addr[AXILSB-1:0]); + default: axi_wdata <= { {(C_AXI_DATA_WIDTH-32){1'b0}}, + i_data } << (8*i_addr[AXILSB-1:0]); + endcase + // }}} + end + // }}} + + // WSTRB + // {{{ + if (SWAP_WSTRB) + begin + // {{{ + casez(i_op[2:1]) + // Write a 16b half-word + 2'b10: axi_wstrb <= { 2'b11, + {(C_AXI_DATA_WIDTH/8-2){1'b0}} } + >> (i_addr[AXILSB-1:0]); + // Write an 8b half-word + 2'b11: axi_wstrb <= { 1'b1, + {(C_AXI_DATA_WIDTH/8-1){1'b0}} } + >> (i_addr[AXILSB-1:0]); + default: axi_wstrb <= { 4'b1111, + {(C_AXI_DATA_WIDTH/8-4){1'b0}} } + >> (i_addr[AXILSB-1:0]); + endcase + // }}} + end else begin + // {{{ + casez(i_op[2:1]) + // Write a 16b half-word + 2'b10: axi_wstrb <= { {(C_AXI_DATA_WIDTH/8-4){1'b0}}, + 4'b0011 } << (i_addr[AXILSB-1:0]); + // Write an 8b half-word + 2'b11: axi_wstrb <= { {(C_AXI_DATA_WIDTH/8-4){1'b0}}, + 4'b0001 } << (i_addr[AXILSB-1:0]); + default: axi_wstrb <= { {(C_AXI_DATA_WIDTH/8-4){1'b0}}, + 4'b1111 } << (i_addr[AXILSB-1:0]); + endcase + // }}} + end + // }}} + + // OPT_LOWPOWER: Clear if nothing is being used + // {{{ + if (OPT_LOWPOWER && ((!i_pipe_stb || !i_op[0] || misaligned) + || (M_AXI_BVALID && M_AXI_BRESP[1]) + || (i_cpu_reset || flushing))) + begin + axi_wdata <= 0; + axi_wstrb <= 0; + end + // }}} + end + + genvar gk; + generate if (!SWAP_WSTRB) + begin + assign M_AXI_WDATA = axi_wdata; + assign M_AXI_WSTRB = axi_wstrb; + + assign axi_rdata = M_AXI_RDATA; + end else for(gk=0; gk> (8*i_addr[AXILSB-1:0]); + // Write an 8b half-word + 2'b11: wcache_data <= { i_data[7:0], + {(DW-8){1'b0}} } + >> (8*i_addr[AXILSB-1:0]); + default: wcache_data <= { i_data, + {(DW-32){1'b0}} } + >> (8*i_addr[AXILSB-1:0]); + endcase + end else begin + casez(i_op[2:1]) + // Write a 16b half-word + 2'b10: wcache_data <= { {(DW-16){1'b0}}, + i_data[15:0] } << (8*i_addr[AXILSB-1:0]); + // Write an 8b half-word + 2'b11: wcache_data <= { {(DW-8){1'b0}}, + i_data[7:0] } << (8*i_addr[AXILSB-1:0]); + default: wcache_data <= { {(DW-32){1'b0}}, + i_data } << (8*i_addr[AXILSB-1:0]); + endcase + end + + if (locked_read_in_progress) + wcache_data <= axi_rdata; + // }}} + + // wcache_strb + // {{{ + if (SWAP_WSTRB) + begin + case(i_op[2:1]) + // Write a 16b half-word + 2'b10: wcache_strb<= + { 2'h3, {(C_AXI_DATA_WIDTH/8-2){1'b0}} } + >> (i_addr[AXILSB-1:0]); + // Write an 8b byte + 2'b11: wcache_strb<= + { 1'b1, {(C_AXI_DATA_WIDTH/8-1){1'b0}} } + >> (i_addr[AXILSB-1:0]); + default: wcache_strb<= + { 4'hf, {(C_AXI_DATA_WIDTH/8-4){1'b0}} } + >> (i_addr[AXILSB-1:0]); + endcase + end else begin + case(i_op[2:1]) + // Write a 16b half-word + 2'b10: wcache_strb<= + { {(C_AXI_DATA_WIDTH/8-4){1'b0}}, 4'h3 } + << (i_addr[AXILSB-1:0]); + // Write an 8b byte + 2'b11: wcache_strb<= + { {(C_AXI_DATA_WIDTH/8-4){1'b0}}, 4'h1 } + << (i_addr[AXILSB-1:0]); + default: wcache_strb<= + {{(C_AXI_DATA_WIDTH/8-4){1'b0}}, 4'hf } + << (i_addr[AXILSB-1:0]); + endcase + end + // }}} + + if (locked_read_in_progress) + begin + if (!locked_read_in_cache || !M_AXI_RVALID + || M_AXI_RRESP != EXOKAY) + wcache_strb <= 0; + end else if (!i_pipe_stb || !i_op[0] || misaligned) + wcache_strb <= 0; + // }}} + end + end + // }}} + + // Actually write to the cache + // {{{ + always @(posedge S_AXI_ACLK) + if (state != DC_WRITE || (r_iv && wcache_tag == r_itag)) + begin + for(ik=0; ik= 1) && !M_AXI_BVALID + && (!M_AXI_RVALID || !M_AXI_RLAST)) + begin + r_pipe_stalled <= 1'b1; + r_pipe_code <= 4'h6; + end + // }}} + end + + // All cachable read requests will stall our pipeline + if (!i_cpu_reset && i_pipe_stb && i_read && !misaligned) + begin + r_pipe_stalled <= 1'b1; + r_pipe_code <= 4'h7; + end + if (!i_cpu_reset && i_pipe_stb && i_lock && !misaligned) + begin + r_pipe_stalled <= 1'b1; + r_pipe_code <= 4'h8; + end + + if (flushing && (M_AXI_AWVALID || M_AXI_ARVALID || M_AXI_WVALID)) + begin + r_pipe_stalled <= 1'b1; + r_pipe_code <= 4'h9; + end + end + + always @(*) + if (r_pipe_stalled) + pipe_stalled = 1'b1; + else if (M_AXI_AWVALID && !M_AXI_AWREADY) + pipe_stalled = 1'b1; + else if (M_AXI_WVALID && !M_AXI_WREADY) + pipe_stalled = 1'b1; + else if (full_pipe) + pipe_stalled = 1'b1; + else + pipe_stalled = 1'b0; + + assign w_pipe_stalled = r_pipe_stalled; + assign o_pipe_stalled = pipe_stalled; + + // Verilator lint_off UNUSED + wire unused_pipe; + assign unused_pipe = &{ 1'b0, r_pipe_code }; + // Verilator lint_on UNUSED + end else begin : PIPE_STALL_ON_BUSY + assign w_pipe_stalled = 1'b0; + assign o_pipe_stalled = o_busy; + + // Verilator lint_off UNUSED + wire unused_pipe; + assign unused_pipe = &{ 1'b0, full_pipe }; + // Verilator lint_on UNUSED + end endgenerate + // }}} + + // o_wreg + // {{{ + // generate if (!OPT_PIPE) + always @(posedge S_AXI_ACLK) + if (i_pipe_stb) + begin + o_wreg <= i_oreg; + if (OPT_LOCK && i_lock && i_op[0]) + o_wreg <= { i_oreg[4], 4'hf }; + end + // }}} + + // restart_pc + // {{{ + generate if (OPT_LOCK) + begin : GEN_RESTART_PC + reg [AW-1:0] r_pc; + + always @(posedge S_AXI_ACLK) + if (i_pipe_stb && i_lock && i_op[0]) + r_pc <= i_restart_pc; + + assign restart_pc = r_pc; + + end else begin + assign restart_pc = 0; + + // Verilator lint_off UNUSED + wire unused_restart_pc; + assign unused_restart_pc = &{ 1'b0, i_restart_pc }; + // Verilator lint_on UNUSED + end endgenerate + // }}} + + // req_data + // {{{ + always @(posedge S_AXI_ACLK) + if (i_pipe_stb) + req_data <= { i_op[2:1], rev_addr[AXILSB-1:0] }; + + assign req_lsb = req_data[AXILSB-1:0]; + assign req_op = req_data[AXILSB +: 2]; + // }}} + + // o_err + // {{{ + initial o_err = 1'b0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + o_err <= 1'b0; + else if (i_cpu_reset || flushing) + o_err <= 1'b0; + else begin + o_err <= 1'b0; + if (M_AXI_RVALID && M_AXI_RRESP[1] && o_rdbusy && !r_dvalid) + o_err <= 1'b1; + if (M_AXI_RVALID && locked_read_in_progress + && M_AXI_RRESP != EXOKAY) + o_err <= 1'b1; + if (M_AXI_BVALID && M_AXI_BRESP[1]) + o_err <= 1'b1; + if (i_pipe_stb && misaligned) + o_err <= 1'b1; + end + // }}} + + // Read from the cache + // {{{ + generate if (OPT_DUAL_READ_PORT) + begin + always @(posedge S_AXI_ACLK) + if (!OPT_LOWPOWER || (i_pipe_stb && !i_op[0])) + cached_iword <= cache_mem[i_caddr]; + + always @(posedge S_AXI_ACLK) + if (!OPT_LOWPOWER || o_rdbusy) + cached_rword <= cache_mem[r_caddr]; + end else begin + + always @(posedge S_AXI_ACLK) + cached_rword <= cache_mem[(o_busy) ? r_caddr : i_caddr]; + + always @(*) + cached_iword = cached_rword; + end endgenerate + // }}} + + // o_data, pre_data + // {{{ + always @(*) + if (r_svalid) + pre_data = cached_iword; + else if (state == DC_READS || (OPT_WRAP && state == DC_READC)) + pre_data = axi_rdata; + else + pre_data = cached_rword; + + always @(*) + if (SWAP_WSTRB) + begin + shifted_data = pre_data << (8*req_lsb); + + casez(req_op) + 2'b10: shifted_data[31:0] = { 16'h0, shifted_data[DW-1:DW-16] }; + 2'b11: shifted_data[31:0] = { 24'h0, shifted_data[DW-1:DW- 8] }; + default: shifted_data[31:0] = shifted_data[DW-1:DW-32]; + endcase + end else + shifted_data = pre_data >> (8*req_lsb); + + // o_data + always @(posedge S_AXI_ACLK) + begin + o_data <= shifted_data[31:0]; + if (OPT_SIGN_EXTEND) + begin + // verilator coverage_off + // Unused, would violate ZipCPU ISA + casez(req_op) + 2'b10: o_data[31:16] <= {(16){shifted_data[15]}}; + 2'b11: o_data[31: 8] <= {(24){shifted_data[ 7]}}; + default: begin end + endcase + // verilator coverage_on + end else begin + casez(req_op) + 2'b10: o_data[31:16] <= 0; + 2'b11: o_data[31: 8] <= 0; + default: begin end + endcase + end + + if (locked_write_in_progress) + begin + o_data <= 0; + o_data[AW-1:0] <= restart_pc; + end + end + // }}} + + // o_valid + // {{{ + initial o_valid = 1'b0; + always @(posedge S_AXI_ACLK) + if (i_cpu_reset || flushing) + o_valid <= 1'b0; + else if (state == DC_READS) + begin + o_valid <= M_AXI_RVALID && !M_AXI_RRESP[1]; + if (OPT_LOCK && locked_read_in_progress && M_AXI_RRESP != EXOKAY) + o_valid <= 0; + end else if (locked_write_in_progress && M_AXI_BVALID + && M_AXI_BRESP == OKAY) + o_valid <= 1'b1; + else if (OPT_WRAP && r_rd_pending && state == DC_READC) + o_valid <= M_AXI_RVALID && !M_AXI_RRESP[1]; + else + o_valid <= r_svalid || r_dvalid; + // }}} + + // Make Verilator happy + // {{{ + // verilator coverage_off + // Verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, M_AXI_BID, M_AXI_RID, r_addr, M_AXI_RRESP[0], + M_AXI_BRESP[0], i_lock, shifted_data, + w_pipe_stalled, axi_tag, axi_line, + rev_addr[C_AXI_ADDR_WIDTH-1:AXILSB] }; + // Verilator lint_on UNUSED + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL +// Formal properties for this core are maintained elsewhere +`endif +// }}} +endmodule diff --git a/rtl/cpu/axiicache.v b/rtl/cpu/axiicache.v new file mode 100644 index 0000000..4f4dfa8 --- /dev/null +++ b/rtl/cpu/axiicache.v @@ -0,0 +1,784 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: axiicache.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: An I-Cache, for when the CPU has an AXI interface. +// +// Goal: To return each instruction within a single clock tick. Jumps +// should only stall if they switch cache lines. +// +// This logic is driven by a couple realities: +// 1. It takes a clock to read from a block RAM address, and hence a clock +// to read from the cache. +// 2. It takes another clock to check that the tag matches +// +// Our goal will be to avoid this second check if at all possible. +// Hence, we'll test on the clock of any given request whether +// or not the request matches the last tag value, and on the next +// clock whether it new tag value (if it has changed). Hence, +// for anything found within the cache, there will be a one +// cycle delay on any branch. +// +// +// Address Words are separated into three components: +// [ Tag bits ] [ Cache line number ] [ Cache position w/in the line ] +// +// On any read from the cache, only the second two components are required. +// On any read from memory, the first two components will be fixed across +// the bus, and the third component will be adjusted from zero to its +// maximum value. +// +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +// +//////////////////////////////////////////////////////////////////////////////// +// +// +`default_nettype none +// }}} +module axiicache #( + // {{{ + // LGCACHESZ is the log(based two) of the cache size *in bytes* + parameter LGCACHESZ = 14, + // + // LGLINESZ is the size of one cache line, represented in + // words + parameter LGLINESZ=3, + // + // LGWAYS is the number of cache "ways" + // parameter LGWAYS = 0, + // + parameter C_AXI_ID_WIDTH = 1, + parameter C_AXI_ADDR_WIDTH = 32, + parameter C_AXI_DATA_WIDTH = 32, + parameter [0:0] OPT_WRAP = 1'b0, + parameter [0:0] OPT_LOWPOWER = 1'b0, + // + // SWAP_ENDIANNESS + parameter [0:0] SWAP_ENDIANNESS = 1'b0, + // + parameter INSN_WIDTH = 32, + parameter [C_AXI_ID_WIDTH-1:0] AXI_ID = 0, + localparam ADDRLSB = $clog2(C_AXI_DATA_WIDTH/8), + localparam LGINSN = $clog2(INSN_WIDTH/8), + // localparam INSN_PER_WORD = C_AXI_DATA_WIDTH/INSN_WIDTH, + localparam AW=C_AXI_ADDR_WIDTH, + localparam DW=C_AXI_DATA_WIDTH, + localparam LS=LGLINESZ, // Size of a cache line in words + LSB=LGLINESZ+ADDRLSB + // }}} + ) ( + // {{{ + input wire S_AXI_ACLK, + input wire S_AXI_ARESETN, + // + // The AXI Master (cache) interface + // {{{ + // An instruction cache only needs to support cache reads + output wire M_AXI_ARVALID, + input wire M_AXI_ARREADY, + // verilator coverage_off + output wire [C_AXI_ID_WIDTH-1:0] M_AXI_ARID, + // verilator coverage_on + output wire [C_AXI_ADDR_WIDTH-1:0] M_AXI_ARADDR, + output wire [7:0] M_AXI_ARLEN, + output wire [2:0] M_AXI_ARSIZE, + output wire [1:0] M_AXI_ARBURST, + // verilator coverage_off + output wire M_AXI_ARLOCK, // = 0 + output wire [3:0] M_AXI_ARCACHE, + output wire [2:0] M_AXI_ARPROT, + output wire [3:0] M_AXI_ARQOS, + // verilator coverage_on + // + input wire M_AXI_RVALID, + output wire M_AXI_RREADY, + // verilator coverage_off + input wire [C_AXI_ID_WIDTH-1:0] M_AXI_RID, + // verilator coverage_on + input wire [C_AXI_DATA_WIDTH-1:0] M_AXI_RDATA, + input wire M_AXI_RLAST, + input wire [1:0] M_AXI_RRESP, + // }}} + // CPU interface + // {{{ + input wire i_cpu_reset, + input wire i_new_pc, + input wire i_clear_cache, + input wire i_ready, + input wire [AW-1:0] i_pc, + output wire [INSN_WIDTH-1:0] o_insn, + output reg [AW-1:0] o_pc, + output reg o_valid, + output reg o_illegal + // }}} +`ifdef DEPRECATED + output wire [AW-1:0] illegal_addr, + output wire [AW-LSB-1:0] bus_tag, + output wire [AW-LSB-1:0] o_tag, + output wire [AW-LSB-1:0] i_tag, + output wire [AW-LSB-1:0] lastpc_tag +`endif + // }}} + ); + + // Register/local parameter declarations + // {{{ + localparam [1:0] INCR = 2'b01, WRAP = 2'b10; + + // localparam CACHELEN=(1< 1) || r_wrap; + end + 2'b10: r_count <= r_count + 1; + // 2'b00: + // 2'b11: + default: begin end + endcase + // }}} + + // wrap_valid itself + // {{{ + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + r_valid <= 0; + else if (i_cpu_reset || i_new_pc || i_clear_cache || bus_abort + || (M_AXI_RVALID && M_AXI_RRESP[1]) + || !r_poss) + r_valid <= 0; + else if (!r_valid || !o_valid || i_ready) + begin + // We can be valid if there's one more in the buffer + // than we've read so far. + r_valid <=(r_count > + ((r_valid&&(!o_valid||i_ready)) ? 1:0) + + (o_valid ? 1:0)); + // We can also be valid if another one has just been + // read--as long as it's not due to a bus error. + if (M_AXI_RVALID && r_wrap) + r_valid <= 1'b1; + end + + assign wrap_valid = r_valid; + // }}} + +`ifdef FORMAL + // {{{ + always @(*) + if (S_AXI_ARESETN && M_AXI_ARVALID) + assert(!r_valid && r_count == 0); + + always @(*) + if (S_AXI_ARESETN && (!request_pending || bus_abort)) + assert(!r_poss); + + always @(*) + if (S_AXI_ARESETN && !r_poss) + assert(r_count == 0); + + always @(*) + if (S_AXI_ARESETN && r_poss) + begin + if (!r_wrap) + assert(r_count < write_posn + - last_pc[LSB-1:ADDRLSB]); + else + assert(r_count <= (1< (o_valid ? 1:0))); + // }}} +`endif + // }}} + end else begin + // {{{ + assign wrap_valid = 1'b0; + + // verilator coverage_off + // verilator lint_off UNUSED + wire unused_nowrap; + assign unused_nowrap = &{ 1'b0, bus_abort }; + // verilator lint_on UNUSED + // verilator coverage_on + // }}} + end endgenerate + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Read the instruction from the cache + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // cache_word -- the cache word at the requested address + // {{{ + always @(posedge S_AXI_ACLK) + if (i_new_pc || (!o_valid || i_ready)) + begin + cache_word <= cache[(i_new_pc || o_valid) + ? i_pc[CWB-1:ADDRLSB] : o_pc[CWB-1:ADDRLSB]]; + end + // }}} + + // o_insn + // {{{ + // Generate the outgoing instruction from the given cache_word + // This involves shifting large cache words down to the desired/correct + // word of interest + generate if (C_AXI_DATA_WIDTH == INSN_WIDTH) + begin : NO_LINE_SHIFT + + assign o_insn = cache_word; + + // Make Verilator happy + // {{{ + // Verilator lint_off UNUSED + if (DW > INSN_WIDTH) + begin + wire unused_wide; + assign unused_wide = &{ 1'b0, cache_word[DW-1:INSN_WIDTH] }; + end + // Verilator lint_on UNUSED + // }}} + end else begin : SHIFT_CACHE_LINE + + reg [C_AXI_DATA_WIDTH-1:0] shifted; + + always @(*) + shifted=cache_word >> (INSN_WIDTH * o_pc[ADDRLSB-1:LGINSN]); + + assign o_insn = shifted[INSN_WIDTH-1:0]; + + // Make Verilator happy + // {{{ + // Verilator lint_off UNUSED + if (DW > INSN_WIDTH) + begin + wire unused_wide; + assign unused_wide = &{ 1'b0, shifted[DW-1:INSN_WIDTH] }; + end + // Verilator lint_on UNUSED + // }}} + end endgenerate + // }}} + + // o_pc + // {{{ + // Signal the CPU with the actual address of the value we are returning + // The ffetch properties will insure this value is correct. This CPU + // then uses it for knowing the program counter of the instruction + // contained within each stage of the pipeline + initial o_pc = 0; + always @(posedge S_AXI_ACLK) + if (i_new_pc) + o_pc <= i_pc; + else if (o_valid && i_ready) + begin + o_pc[AW-1:2] <= o_pc[AW-1:2]+1; + o_pc[1:0] <= 0; + end + // }}} + + // o_valid + // {{{ + // Are we returning a valid instruction to the CPU on this cycle? + initial o_valid = 0; + always @(posedge S_AXI_ACLK) + if (i_cpu_reset || i_clear_cache) + o_valid <= 0; + else if (o_valid && (i_ready || i_new_pc)) + begin + // Grab the next instruction--always ready on the same cycle + // if we stay within the same cache line + o_valid <= valid_line; + if (o_illegal) + o_valid <= 0; + end else if (!o_valid && !i_new_pc) + begin + // We're stuck waiting for the cache line to become valid. + // Don't forget to check for the illegal flag. + o_valid <= valid_line; + if (illegal_valid && o_pc[AW-1:LSB] == illegal_tag) + o_valid <= 1; + end + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Handle bus errors here. + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // If a bus read request returns an error, then we'll mark the entire + // line as having a (valid) illegal value. Once the o_illegal value + // is set, it will remain set until either the CPU branches, or the + // cache is cleared. + // + + // illegal_tag, illegal_valid + // {{{ + // Here's the rule: if illegal_valid is true, then we know that address + // at that illegal_tag value will produce a bus error, and we can serve + // a bus error request response on any read request. + initial illegal_tag = 0; + initial illegal_valid = 0; + always @(posedge S_AXI_ACLK) + if ((i_cpu_reset)||(i_clear_cache)) + begin + illegal_tag <= 0; + illegal_valid <= 0; + end else if (M_AXI_RVALID && M_AXI_RRESP[1]) + begin + illegal_tag <= axi_araddr[AW-1:LSB]; + illegal_valid <= 1'b1; + end + // }}} + + // o_illegal + // {{{ + initial o_illegal = 1'b0; + always @(posedge S_AXI_ACLK) + if (i_cpu_reset || i_clear_cache || i_new_pc) + o_illegal <= 1'b0; + else if (o_valid && !o_illegal) + o_illegal <= 1'b0; + else if (illegal_valid && o_pc[AW-1:LSB] == illegal_tag) + o_illegal <= 1'b1; + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Fixed/constant bus values, map registers to AXI wires/ etc. + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // Fixed bus outputs: we read from the bus only, never write. + // Thus the output data is ... irrelevant and don't care. We set it + // to zero just to set it to something. + assign M_AXI_ARVALID= axi_arvalid; + assign M_AXI_ARID = AXI_ID; + assign M_AXI_ARADDR = axi_araddr; + assign M_AXI_ARLEN = (1< for a copy. +// +// License: GPL, v3, as defined and found on www.gnu.org, +// http://www.gnu.org/licenses/gpl.html +// +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module axilfetch #( + // {{{ + parameter C_AXI_ADDR_WIDTH = 32, + parameter C_AXI_DATA_WIDTH = 64, + parameter INSN_WIDTH=32, + parameter FETCH_LIMIT=16, + parameter [0:0] SWAP_ENDIANNESS = 1'b0, + localparam AW=C_AXI_ADDR_WIDTH + // }}} + ) ( + // {{{ + input wire S_AXI_ACLK, + input wire S_AXI_ARESETN, + // + // CPU interaction wires + // {{{ + input wire i_cpu_reset, + input wire i_new_pc, + input wire i_clear_cache, + input wire i_ready, + input wire [AW-1:0] i_pc, // Ignd unls i_new_pc + output wire [INSN_WIDTH-1:0] o_insn, // Insn read from bus + output reg [AW-1:0] o_pc, // Addr of that insn + output reg o_valid, // If valid + output reg o_illegal, // Bus error + // }}} + // AXI-lite bus interface + // {{{ + output reg M_AXI_ARVALID, + input wire M_AXI_ARREADY, + output reg [C_AXI_ADDR_WIDTH-1:0] M_AXI_ARADDR, + output wire [2:0] M_AXI_ARPROT, + // + input wire M_AXI_RVALID, + output wire M_AXI_RREADY, + input wire [C_AXI_DATA_WIDTH-1:0] M_AXI_RDATA, + input wire [1:0] M_AXI_RRESP + // }}} + // }}} + ); + + // Declarations + // {{{ + localparam AXILLSB = $clog2(C_AXI_DATA_WIDTH/8); + localparam INSNS_PER_WORD = C_AXI_DATA_WIDTH / INSN_WIDTH; + localparam INSN_LSB = $clog2(INSN_WIDTH/8); + localparam LGDEPTH = $clog2(FETCH_LIMIT)+4; + localparam LGFIFO = $clog2(FETCH_LIMIT); + localparam W = LGDEPTH; + localparam FILLBITS = $clog2(INSNS_PER_WORD); + // ($clog2(INSNS_PER_WORD) > 0) + // ? $clog2(INSNS_PER_WORD) : 1); + + reg [W:0] new_flushcount, outstanding, + next_outstanding, flushcount; + reg flushing, flush_request, full_bus; + wire [((AXILLSB>INSN_LSB) ? (AXILLSB-INSN_LSB-1):0):0] shift; + wire fifo_reset, fifo_wr, fifo_rd; + wire ign_fifo_full, fifo_empty; + wire [LGFIFO:0] ign_fifo_fill; + wire [C_AXI_DATA_WIDTH:0] fifo_data; + reg pending_new_pc; + reg [C_AXI_ADDR_WIDTH-1:0] pending_pc; + reg [W-1:0] fill; + reg [FILLBITS:0] out_fill; + reg [C_AXI_DATA_WIDTH-1:0] out_data; + reg [C_AXI_DATA_WIDTH-1:0] endian_swapped_rdata; + // }}} + + assign fifo_reset = i_cpu_reset || i_clear_cache || i_new_pc; + assign fifo_wr = M_AXI_RVALID && !flushing; + + + // ARPROT = 3'b100 for an unprivileged, secure instruction access + // (not sure what unprivileged or secure mean--even after reading the + // spec) + assign M_AXI_ARPROT = 3'b100; + + // next_outstanding + // {{{ + always @(*) + begin + next_outstanding = outstanding; + + case({ M_AXI_ARVALID && M_AXI_ARREADY, M_AXI_RVALID }) + 2'b10: next_outstanding = outstanding + 1; + 2'b01: next_outstanding = outstanding - 1; + default: begin end + endcase + end + // }}} + + // outstanding, full_bus + // {{{ + initial outstanding = 0; + initial full_bus = 0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + begin + outstanding <= 0; + full_bus <= 0; + end else begin + outstanding <= next_outstanding; + full_bus <= (next_outstanding + // + (((M_AXI_ARVALID && !M_AXI_ARREADY) ? 1:0) + >= (1< 0); + flush_request <= (M_AXI_ARVALID && !M_AXI_ARREADY); + end else begin + if (M_AXI_RVALID && flushcount > 0) + begin + flushcount <= flushcount - 1; + // Verilator lint_off CMPCONST + flushing <= (flushcount > 1); + // Verilator lint_on CMPCONST + end + + if (M_AXI_ARREADY) + flush_request <= 0; + end + // }}} + + // M_AXI_ARVALID + // {{{ + initial M_AXI_ARVALID = 1'b0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + M_AXI_ARVALID <= 1'b0; + else if (!M_AXI_ARVALID || M_AXI_ARREADY) + begin + M_AXI_ARVALID <= 1; + if (i_new_pc || pending_new_pc) + M_AXI_ARVALID <= 1'b1; + + // + // Throttle the number of requests we make + // Verilator lint_off CMPCONST + // Verilator lint_off WIDTH + // out_fill will only capture 0 or 1 if DATA_WIDTH == 32 + if (fill + (M_AXI_ARVALID ? 1:0) + + ((o_valid &&(!i_ready || out_fill > 1)) ? 1:0) + >= FETCH_LIMIT) + M_AXI_ARVALID <= 1'b0; + // Verilator lint_on WIDTH + // Verilator lint_on CMPCONST + if (i_cpu_reset || i_clear_cache || full_bus) + M_AXI_ARVALID <= 1'b0; + end + // }}} + + assign M_AXI_RREADY = 1'b1; + + // pending_new_pc + // {{{ + initial pending_new_pc = 0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN || i_clear_cache) + pending_new_pc <= 1'b0; + else if (!M_AXI_ARVALID || M_AXI_ARREADY) + pending_new_pc <= 1'b0; + else if (i_new_pc) + pending_new_pc <= 1'b1; + // }}} + + // pending_pc + // {{{ + initial pending_pc = 0; + always @(posedge S_AXI_ACLK) + if (i_new_pc) + pending_pc <= i_pc; + // }}} + + // M_AXI_ARADDR + // {{{ + always @(posedge S_AXI_ACLK) + if (!M_AXI_ARVALID || M_AXI_ARREADY) + begin + if (i_new_pc) + M_AXI_ARADDR <= i_pc; + else if (pending_new_pc) + M_AXI_ARADDR <= pending_pc; + else if (M_AXI_ARVALID) + begin + M_AXI_ARADDR[C_AXI_ADDR_WIDTH-1:AXILLSB] + <= M_AXI_ARADDR[C_AXI_ADDR_WIDTH-1:AXILLSB] +1; + M_AXI_ARADDR[AXILLSB-1:0] <= 0; + end + end + // }}} + + // o_pc + // {{{ + initial o_pc = 0; + always @(posedge S_AXI_ACLK) + if (i_new_pc) + o_pc <= i_pc; + else if (o_valid && i_ready && !o_illegal) + begin + o_pc <= 0; + o_pc[AW-1:INSN_LSB] <= o_pc[AW-1:INSN_LSB] + 1; + end + // }}} + + generate if (AXILLSB > INSN_LSB) + begin : BIG_WORD + // {{{ + assign shift = o_pc[AXILLSB-1:INSN_LSB]; + // }}} + end else begin : NO_SHIFT + // {{{ + assign shift = 0; + // }}} + end endgenerate + + generate if (SWAP_ENDIANNESS) + begin : SWAPPED_ENDIANNESS + // {{{ + genvar gw, gb; // Word count, byte count + + for(gw=0; gw 0) + assert(!o_valid); +`endif + // }}} + end else if (FETCH_LIMIT == 2) + begin : DBLFETCH + // {{{ + // Single word cache + reg cache_valid; + reg [C_AXI_DATA_WIDTH:0] cache_data; + + assign fifo_rd = !o_valid || (i_ready && (out_fill <= 1)); + assign fifo_empty =(!M_AXI_RVALID && !cache_valid) || flushing; + assign fifo_data = cache_valid ? cache_data + : ({ M_AXI_RRESP[1], endian_swapped_rdata }); + + + assign ign_fifo_fill = cache_valid ? 1 : 0; + assign ign_fifo_full = cache_valid; + + initial cache_valid = 1'b0; + always @(posedge S_AXI_ACLK) + if (fifo_reset) + cache_valid <= 1'b0; + else if (M_AXI_RVALID && o_valid && !fifo_rd) + cache_valid <= 1; + else if (fifo_rd) + cache_valid <= 1'b0; + + always @(posedge S_AXI_ACLK) + if (M_AXI_RVALID) + cache_data <= { M_AXI_RRESP[1], endian_swapped_rdata }; + // }}} + end else begin : FIFO_FETCH + // {{{ + // FIFO cache + + // Verilator lint_off CMPCONST + // out_fill will only capture 0 or 1 if DATA_WIDTH == 32 + assign fifo_rd = !o_valid || (i_ready && (out_fill <= 1)); + // Verilator lint_on CMPCONST + + sfifo #( + // {{{ + .BW(1+C_AXI_DATA_WIDTH), .LGFLEN(LGFIFO) + // }}} + ) fcache( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(fifo_reset), + .i_wr(fifo_wr), + .i_data({M_AXI_RRESP[1], endian_swapped_rdata }), + .o_full(ign_fifo_full), .o_fill(ign_fifo_fill), + .i_rd(fifo_rd),.o_data(fifo_data),.o_empty(fifo_empty) + // }}} + ); + // }}} + end endgenerate + + // o_valid + // {{{ + initial o_valid = 1'b0; + always @(posedge S_AXI_ACLK) + if (fifo_reset) + o_valid <= 1'b0; + else if (!o_valid || i_ready) + o_valid <= (fifo_rd && !fifo_empty) + || out_fill > (o_valid ? 1:0); + // }}} + + // out_fill + // {{{ + // == number of instructions in the fifo_data word that have not (yet) + // been accepted by the CPU. + // == 0 when no data is available + // == INSN_PER_WORD on the first instruction of any word + // == 1 on the last instruction of any word + initial out_fill = 0; + always @(posedge S_AXI_ACLK) + if (fifo_reset) + out_fill <= 0; + else if (fifo_rd) + begin + if (fifo_empty) + out_fill <= 0; + else if (o_valid) + out_fill <= INSNS_PER_WORD[FILLBITS:0]; + else + // Verilator lint_off WIDTH + out_fill <= (INSNS_PER_WORD[FILLBITS:0] - shift); + // Verilator lint_on WIDTH + end else if (i_ready && out_fill > 0) + out_fill <= out_fill - 1; + // }}} + + // out_data + // {{{ + always @(posedge S_AXI_ACLK) + if (fifo_rd) + begin + if (o_valid || (INSN_WIDTH == C_AXI_DATA_WIDTH)) + out_data <= fifo_data[C_AXI_DATA_WIDTH-1:0]; + else + out_data <= fifo_data[C_AXI_DATA_WIDTH-1:0]>>(INSN_WIDTH*shift); + end else if (i_ready) + out_data <= out_data >> INSN_WIDTH; + // }}} + + assign o_insn = out_data[INSN_WIDTH-1:0]; + + // o_illegal + // {{{ + initial o_illegal = 1'b0; + always @(posedge S_AXI_ACLK) + if (fifo_reset) + o_illegal <= 1'b0; + else if (!o_illegal && fifo_rd && !fifo_empty) + o_illegal <= fifo_data[C_AXI_DATA_WIDTH]; + // }}} + + // Make verilator happy + // {{{ + // verilator coverage_off + // verilator lint_off UNUSED + wire unused; + assign unused = & { 1'b0, M_AXI_RRESP[0], ign_fifo_full, ign_fifo_fill }; + // verilator lint_on UNUSED + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL + // Declarations + // {{{ + localparam DW=INSN_WIDTH; + localparam F_LGDEPTH=LGDEPTH+2; + reg f_past_valid; + wire [(F_LGDEPTH-1):0] faxil_outstanding; + // Verilator lint_off UNUSED + wire [(F_LGDEPTH-1):0] faxil_awr_outstanding, + faxil_wr_outstanding; + // Verilator lint_on UNUSED + reg [(AW-1):0] f_last_pc; + reg f_last_pc_valid; + // + reg [AW-1:0] fc_pc, f_address; + reg fc_illegal; + reg [INSN_WIDTH-1:0] fc_insn; + //}}} + //////////////////////////////////////////////////////////////////////// + // + // Generic setup + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // Keep track of a flag telling us whether or not $past() + // will return valid results + initial f_past_valid = 1'b0; + always @(posedge S_AXI_ACLK) + f_past_valid <= 1'b1; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Assumptions about our inputs + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // Assume we start from a reset condition + // {{{ + always @(*) + if (!f_past_valid) + assume(!S_AXI_ARESETN); + + always @(*) + if (!S_AXI_ARESETN) + assume(i_cpu_reset); + // }}} + + // + // + // Let's make some assumptions about how long it takes our + // phantom bus and phantom CPU to respond. + // + // These delays need to be long enough to flush out any potential + // errors, yet still short enough that the formal method doesn't + // take forever to solve. + // + localparam F_CPU_DELAY = 4; + reg [$clog2(F_CPU_DELAY):0] f_cpu_delay; + // First, let's assume that any response from the bus comes back + // within F_WB_DELAY clocks + + // Here's our delay assumption: We'll assume that the + // wishbone will always respond within F_WB_DELAY clock ticks + // of the beginning of any cycle. + // + // This includes both dropping the stall line, as well as + // acknowledging any request. While this may not be + // a reasonable assumption for a piped master, it should + // work here for us. + + // Count the number of clocks it takes the CPU to respond to our + // instruction. + always @(posedge S_AXI_ACLK) + // If no instruction is ready, then keep our counter at zero + if (i_cpu_reset|| !o_valid || i_ready) + f_cpu_delay <= 0; + else + // Otherwise, count the clocks the CPU takes to respond + f_cpu_delay <= f_cpu_delay + 1'b1; + +`ifdef PREFETCH + // Only *assume* that we are less than F_CPU_DELAY if we are not + // integrated into the CPU + always @(posedge S_AXI_ACLK) + assume(f_cpu_delay < F_CPU_DELAY); +`endif + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Bus properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + faxil_master #( + // {{{ + .C_AXI_ADDR_WIDTH(C_AXI_ADDR_WIDTH), + .C_AXI_DATA_WIDTH(C_AXI_DATA_WIDTH), + .F_OPT_READ_ONLY(1'b1), + .F_OPT_COVER_BURST(5), + .F_OPT_ASSUME_RESET(1'b1), + .F_LGDEPTH(F_LGDEPTH) + // }}} + ) faxil( + // {{{ + .i_clk(S_AXI_ACLK), .i_axi_reset_n(S_AXI_ARESETN), + // Unused write interface + // {{{ + .i_axi_awvalid(1'b0), .i_axi_awready(1'b1), + .i_axi_awaddr( {(AW){1'b0}}), + .i_axi_awprot( 3'h0), + .i_axi_wvalid(1'b0), .i_axi_wready(1'b1), + .i_axi_wdata({(C_AXI_DATA_WIDTH){1'b0}}), + .i_axi_wstrb({(C_AXI_DATA_WIDTH/8){1'b0}}), + .i_axi_bvalid(1'b0), .i_axi_bready(1'b1), + .i_axi_bresp(2'b00), + // }}} + // Read address channel + // {{{ + .i_axi_arvalid(M_AXI_ARVALID), + .i_axi_arready(M_AXI_ARREADY), + .i_axi_araddr( M_AXI_ARADDR), + .i_axi_arprot( 3'h0), + // }}} + // Read return / data interface + // {{{ + .i_axi_rvalid(M_AXI_RVALID), + .i_axi_rready(M_AXI_RREADY), + .i_axi_rdata(M_AXI_RDATA), + .i_axi_rresp(M_AXI_RRESP), + // }}} + .f_axi_awr_outstanding(faxil_awr_outstanding), + .f_axi_wr_outstanding(faxil_wr_outstanding), + .f_axi_rd_outstanding(faxil_outstanding) + // }}} + ); + + + always @(*) + assert(full_bus == (outstanding >= (1< 0)); + + always @(*) + assert(flushcount <= outstanding + (flush_request ? 1:0)); + + always @(posedge S_AXI_ACLK) + if (flush_request && !i_clear_cache && !i_cpu_reset) + assert(pending_new_pc || i_new_pc); + + always @(*) + if (!M_AXI_ARVALID || !flushing) + assert(!flush_request); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Subword return + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + reg [W:0] f_word_count; + reg [AW-1:0] f_return_addr; + reg [AW-1:0] f_out_addr, f_subout_addr; + reg [AW-1:0] f_req_addr, f_ret_addr; + reg [W:0] f_req_offset, f_ret_offset; + + + always @(*) + begin + f_req_offset = f_word_count; + if (flushing) + f_req_offset = f_req_offset - flushcount; + if (M_AXI_ARVALID && f_req_offset > 0) + f_req_offset = f_req_offset - 1; + + // Verilator lint_off WIDTH + f_req_addr = 0; + f_req_addr[AW-1:AXILLSB] = f_out_addr[AW-1:AXILLSB] + + f_req_offset; + + /////////////// + + f_ret_offset = o_valid ? 1:0; + f_ret_offset = f_ret_offset + ign_fifo_fill; + + f_ret_addr = 0; + f_ret_addr[AW-1:AXILLSB] = f_out_addr[AW-1:AXILLSB] + f_ret_offset; + end + + always @(*) + if (S_AXI_ARESETN) + begin + assert(f_req_offset <= f_word_count); + + assert(f_req_offset == fill + (o_valid ? 1:0)); + + assert(f_ret_offset <= f_req_offset); + end + + always @(*) + if (!i_cpu_reset && !i_new_pc && !i_clear_cache + && !pending_new_pc && M_AXI_ARADDR != o_pc) + assert(M_AXI_ARADDR[AXILLSB-1:0] == 0); + + always @(*) + if (S_AXI_ARESETN) + begin + if (!flushing) + begin + assert(f_word_count <= FETCH_LIMIT); + end else + assert(f_word_count - flushcount <= FETCH_LIMIT); + end + + always @(*) + if (!i_cpu_reset && !i_new_pc && !i_clear_cache && !flush_request) + begin + // if (!o_illegal) + // assert(f_req_addr[AXILLSB-1:0] == M_AXI_ARADDR[AXILLSB-1:0]); + assert(f_req_addr[AW-1:AXILLSB] == M_AXI_ARADDR[AW-1:AXILLSB]); + end + + always @(*) + if (S_AXI_ARESETN && !i_cpu_reset && !i_new_pc && !i_clear_cache + && !flushing && !o_illegal) + assert(f_ret_addr[AW-1:AXILLSB] == f_return_addr[AW-1:AXILLSB]); + + always @(*) + begin + f_word_count = faxil_outstanding; + if (M_AXI_ARVALID) + f_word_count = f_word_count + 1; + f_word_count = f_word_count + ign_fifo_fill; + if (o_valid) + f_word_count = f_word_count + 1; + end + + + generate if (FETCH_LIMIT <= 1) + begin : F_NO_CACHE + + end else if (FETCH_LIMIT == 2) + begin : F_DBLFETCH + + end else begin : F_FIFO_CACHE + + end endgenerate + + always @(*) + begin + assert(faxil_outstanding + (M_AXI_ARVALID ? 1:0)<=(1<0)); + + generate if (AXILLSB != INSN_LSB) + begin : OUTFILL_CHK + always @(*) + if (o_valid && !o_illegal) + assert(out_fill == INSNS_PER_WORD - o_pc[AXILLSB-1:INSN_LSB]); + end else begin + always @(*) + if (S_AXI_ARESETN) + assert(out_fill <= 1); + end endgenerate + + // + // Assertions about our return responses + // {{{ + // While the below assertions (currently) appear to just copy the logic + // above, they still have a purpose--they'll help us guarantee that the + // logic above will never change without also requiring a change here. + // + generate if (SWAP_ENDIANNESS) + begin : CHECK_SWAPPED_ENDIANNESS + // {{{ + genvar gw, gb; // Word count, byte count + + for(gw=0; gw> (bshift * INSN_WIDTH); + // Verilator lint_on WIDTH + // }}} + end endgenerate + + always @(*) + if (M_AXI_RVALID && f_return_addr[AW-1:AXILLSB] == fc_pc[AW-1:AXILLSB]) + begin + assume(M_AXI_RRESP[1] == fc_illegal); + assume(f_bus_word == fc_insn); + end + + // + // + // Step 2: Assert that any value in our cache meets the contract + // + // + generate if (INSN_WIDTH == C_AXI_DATA_WIDTH) + begin : F_OUTDATA + // {{{ + assign f_out_data = out_data; + // }}} + end else begin : F_OUTDATA_SHIFT + // {{{ + reg [$clog2(INSNS_PER_WORD)-1:0] ashift; + + // Verilator lint_off WIDTH + always @(*) + ashift = (fc_pc - f_subout_addr) >> INSN_LSB; + + assign f_out_data = out_data >> (ashift * INSN_WIDTH); + // Verilator lint_on WIDTH + // }}} + end endgenerate + + generate if (FETCH_LIMIT == 1) + begin : F_SINGLE + end else if (FETCH_LIMIT == 2) + begin : F_CACHE_CHECK + // {{{ + reg [AW-1:0] f_cache_addr; + wire [C_AXI_DATA_WIDTH-1:0] f_cache_data; + wire f_cache_illegal; + reg f_cache_check; + + always @(*) + begin + f_cache_addr = 0; + f_cache_addr[AW-1:AXILLSB] = f_out_addr[AW-1:AXILLSB]+1; + + f_cache_check = (fc_pc[AW-1:AXILLSB] + == f_cache_addr[AW-1:AXILLSB]); + if (!DBLFETCH.cache_valid) + f_cache_check = 1'b0; + end + + if (INSN_WIDTH == C_AXI_DATA_WIDTH) + begin : F_CACHEDATA + + assign f_cache_data=DBLFETCH.cache_data[INSN_WIDTH-1:0]; + assign f_cache_illegal= DBLFETCH.cache_data[INSN_WIDTH]; + + end else begin : F_CACHEDATA_SHIFT + + reg [$clog2(INSNS_PER_WORD)-1:0] ashift; + + always @(*) + ashift = (fc_pc - f_cache_addr) >> INSN_LSB; + + assign f_cache_data = DBLFETCH.cache_data >> (ashift * INSN_WIDTH); + assign f_cache_illegal= DBLFETCH.cache_data[C_AXI_DATA_WIDTH]; + end + + always @(*) + if (f_cache_check && !o_illegal) + begin + assert(f_cache_data[INSN_WIDTH-1:0] == fc_insn); + assert(f_cache_illegal == fc_illegal); + end + // }}} + end else begin : F_FIFO_CHECK + // {{{ + reg [AW-1:0] f_cache_addr, f_fifo_addr; + reg [C_AXI_DATA_WIDTH-1:0] f_cache_data, f_cache_subdata, + f_fifo_subdata; + reg f_cache_illegal; + reg f_cache_check, f_cache_valid; + reg [LGFIFO:0] f_cache_distance; + reg f_cache_assume; + + + always @(*) + { f_cache_illegal, f_cache_data } + = { FIFO_FETCH.fcache.f_first_data }; + + always @(*) + f_cache_distance = FIFO_FETCH.fcache.f_distance_to_first; + + always @(*) + f_cache_valid = FIFO_FETCH.fcache.f_first_in_fifo; + + always @(*) + begin + f_cache_addr = 0; + f_cache_addr[AW-1:AXILLSB] = f_out_addr[AW-1:AXILLSB] + + (o_valid ? 1:0) + + f_cache_distance; + + f_cache_check = (fc_pc[AW-1:AXILLSB] + == f_cache_addr[AW-1:AXILLSB]); + if (!f_cache_valid) + f_cache_check = 1'b0; + end + + if (INSN_WIDTH == C_AXI_DATA_WIDTH) + begin : F_CACHEDATA + + always @(*) + f_cache_subdata = f_cache_data; + + always @(*) + f_fifo_subdata = fifo_data[INSN_WIDTH-1:0]; + + end else begin : F_CACHEDATA_SHIFT + + reg [$clog2(INSNS_PER_WORD)-1:0] ashift; + + // Verilator lint_off WIDTH + always @(*) + ashift = (fc_pc - f_cache_addr) >> INSN_LSB; + // Verilator lint_on WIDTH + + always @(*) + f_cache_subdata = f_cache_data >> (ashift * INSN_WIDTH); + always @(*) + f_fifo_subdata = fifo_data >> (ashift * INSN_WIDTH); + end + + always @(*) + begin + f_fifo_addr = 0; + f_fifo_addr[AW-1:AXILLSB] = f_out_addr[AW-1:AXILLSB] + + (o_valid ? 1:0); + end + + always @(*) + if (f_cache_check && !o_illegal) + begin + assert(f_cache_subdata[INSN_WIDTH-1:0] == fc_insn); + assert(f_cache_illegal == fc_illegal); + end + + always @(*) + begin + f_cache_assume = 0; + if (ign_fifo_fill != 0 && (f_fifo_addr[AW-1:AXILLSB] + == fc_pc[AW-1:AXILLSB])) + begin + if (!f_cache_check || o_illegal) + f_cache_assume = 1; + if (f_cache_addr[AW-1:AXILLSB] + != f_fifo_addr[AW-1:AXILLSB]) + f_cache_assume = 1; + end + end + + always @(*) + if (f_cache_assume) + begin + assume(f_fifo_subdata[INSN_WIDTH-1:0] == fc_insn); + assume(fifo_data[C_AXI_DATA_WIDTH] == fc_illegal); + end + // }}} + end endgenerate + + // + // Check the final output word + // This checks all instruction positions within that word + always @(*) + if ((out_fill > 0) + && (fc_pc[AW-1:AXILLSB] == f_out_addr[AW-1:AXILLSB])) + begin + if (fc_illegal) + begin + assert(o_illegal); + end else if (!o_illegal && fc_pc >= f_subout_addr) + assert(f_out_data == fc_insn); + end + + always @(*) + if (pending_new_pc) + assert(i_new_pc || flush_request); + + always @(*) + if (pending_new_pc) + assert(o_pc == pending_pc); + + // Verilator lint_off WIDTH + always @(*) + if (flush_request) + assert(flushcount == faxil_outstanding + 1); + // Verilator lint_on WIDTH + + always @(*) + if (flushing) + assert(!o_illegal); + + always @(*) + if (flush_request && !i_cpu_reset && !i_new_pc && !i_clear_cache) + begin + assert(f_out_addr[AW-1:AXILLSB] == pending_pc[AW-1:AXILLSB]); + assert(o_pc == pending_pc); + end +`endif + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Cover properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + // Verilator lint_off UNDRIVEN + (* anyconst *) reg cvr_always_ready; + // Verilator lint_on UNDRIVEN + reg [3:0] cvr_returns; + + always @(*) + if (cvr_always_ready) + assume(M_AXI_ARREADY); + + initial cvr_returns = 0; + always @(posedge S_AXI_ACLK) + if (i_cpu_reset || i_new_pc || i_clear_cache || o_illegal) + cvr_returns <= 0; + else if (o_valid && i_ready && !cvr_returns[3]) + cvr_returns <= cvr_returns + 1; + + always @(*) + begin + cover(cvr_returns == 4'b0100); + cover(cvr_returns == 4'b0101); + cover(cvr_returns == 4'b0110 && cvr_always_ready); + end + // }}} + + // Make Verilator happy + // {{{ + // Verilator lint_off UNUSED + wire unused_formal; + assign unused_formal = &{ 1'b0, f_ret_addr[AXILLSB-1:0] }; + // Verilator lint_on UNUSED + // }}} +`endif +// }}} +endmodule diff --git a/rtl/cpu/axilops.v b/rtl/cpu/axilops.v new file mode 100644 index 0000000..b723159 --- /dev/null +++ b/rtl/cpu/axilops.v @@ -0,0 +1,1383 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: axilops.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: A memory unit to support a CPU based upon AXI-lite. This is +// a very basic memory unit designed to be low on logic and yet +// to still support a lot of options. +// +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2020-2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +// +`default_nettype none +// }}} +module axilops #( + // {{{ + parameter ADDRESS_WIDTH=30, + parameter C_AXI_ADDR_WIDTH = ADDRESS_WIDTH, + parameter C_AXI_DATA_WIDTH = 32, + // + // SWAP_ENDIANNESS + // {{{ + // The ZipCPU was designed to be a big endian machine. With + // no other adjustments, this design will make the ZipCPU + // *little* endian, for the simple reason that the AXI bus is + // a little endian bus. However, if SWAP_ENDIANNESS is set, + // the bytes within 32-bit words on the AXI bus will be swapped. + // This will return the CPU to being a big endian CPU on a + // little endian bus. It will also break any design that + // assumes the bus is presented to it in its proper order. + // Simple things like counters or interrupt controllers will + // therefore cease to work with this option unless they also + // swap the endianness of the words they are given. + parameter [0:0] SWAP_ENDIANNESS = 1'b0, + // }}} + // SWAP_WSTRB + // {{{ + // SWAP_WSTRB represents a second attempt to fix the endianness + // issue. It is incompatible with the SWAP_ENDIANNESS option + // above. If SWAP_WSTRB is set, then half words and words will + // be placed on the bus in little endian order, but at big + // endian addresses. Words written to the bus will be written + // in little endian order. Halfwords written to the bus at + // address 2 will be written to address 0, halfwords written to + // address 0 will be written to address 2. Bytes written to the + // but at address 3 will be written to address 0, address 2 + // will be written to address 1, address 1 to address 2, and + // address 3 to address 0. + // + // This may just be a half baked attempt to solve this problem, + // since it will fail if you ever trie to access bytes or + // halfwords at other than their intended widths. + parameter [0:0] SWAP_WSTRB = 1'b0, + // }}} + // OPT_SIGN_EXTEND + // {{{ + // Some CPU's want memory accesses to be sign extended upon + // return. The ZipCPU is not one of those CPU's. However, + // since it's fairly easy to do so, we'll implement this logic + // if ever OPT_SIGN_EXTEND is true so you can see how it would + // be done if necessary. + parameter [0:0] OPT_SIGN_EXTEND = 1'b0, + // }}} + // OPT_ALIGNMENT_ERR + // {{{ + // If set, OPT_ALIGNMENT_ERR will generate an alignment error + // on any attempt to write to or read from an unaligned word. + // If not set, unaligned reads (or writes) will be expanded into + // pairs so as to still accomplish the action requested. The + // bus does not guarantee protection, however, that these two + // writes or two reads will proceed uninterrupted. Since + // unaligned writes and unaligned reads are no longer + // guaranteed to be atomic by the AXI bus, it is possible that + // any unaligned operations might yield an incoherent result. + parameter [0:0] OPT_ALIGNMENT_ERR = 1'b1, + // }}} + // OPT_LOWPOWER + // {{{ + // If set, the design will use extra logic to guarantee that any + // unused registers are kept at zero until they are used. This + // will help to guarantee the design (ideally) has fewer + // transitions and therefore uses lower power. + parameter [0:0] OPT_LOWPOWER = 1'b0, + // }}} + localparam AW = C_AXI_ADDR_WIDTH, + localparam DW = C_AXI_DATA_WIDTH, + localparam AXILSB = $clog2(C_AXI_DATA_WIDTH/8) + // }}} + ) ( + // {{{ + input wire S_AXI_ACLK, + input wire S_AXI_ARESETN, + input wire i_cpu_reset, + // + // CPU interface + // {{{ + input wire i_stb, + input wire i_lock, + input wire [2:0] i_op, + input wire [AW-1:0] i_addr, + input wire [31:0] i_data, + input wire [4:0] i_oreg, + output reg o_busy, + output reg o_rdbusy, + output reg o_valid, + output reg o_err, + output reg [4:0] o_wreg, + output reg [31:0] o_result, + // }}} + // AXI-Lite bus interface + // + // Writes + // {{{ + output reg M_AXI_AWVALID, + input wire M_AXI_AWREADY, + output reg [AW-1:0] M_AXI_AWADDR, + // verilator coverage_off + output wire [2:0] M_AXI_AWPROT, + // verilator coverage_on + // + output reg M_AXI_WVALID, + input wire M_AXI_WREADY, + output reg [DW-1:0] M_AXI_WDATA, + output reg [DW/8-1:0] M_AXI_WSTRB, + // + input wire M_AXI_BVALID, + output reg M_AXI_BREADY, + input wire [1:0] M_AXI_BRESP, + // }}} + // Reads + // {{{ + output reg M_AXI_ARVALID, + input wire M_AXI_ARREADY, + output reg [AW-1:0] M_AXI_ARADDR, + // verilator coverage_off + output wire [2:0] M_AXI_ARPROT, + // verilator coverage_on + // + input wire M_AXI_RVALID, + output reg M_AXI_RREADY, + input wire [DW-1:0] M_AXI_RDATA, + input wire [1:0] M_AXI_RRESP + // }}} + // }}} + ); + + // Declarations + // {{{ + wire i_clk = S_AXI_ACLK; + // wire i_reset = !S_AXI_ARESETN; + + reg w_misaligned, w_misalignment_err; + wire misaligned_request, misaligned_aw_request, + misaligned_response_pending, pending_err, misaligned_read; + reg r_flushing; + reg [AXILSB+1:0] r_op; + reg [DW-1:0] next_wdata; + reg [DW/8-1:0] next_wstrb; + reg [DW-1:0] last_result; + // reg [31:0] endian_swapped_wdata; + // reg [31:0] endian_swapped_result; + reg [2*DW/8-1:0] shifted_wstrb_word, + shifted_wstrb_halfword, + shifted_wstrb_byte; + reg [2*DW/8-1:0] swapped_wstrb_word, + swapped_wstrb_halfword, + swapped_wstrb_byte; + reg [DW-1:0] axi_wdata; + reg [DW/8-1:0] axi_wstrb; + reg [AXILSB-1:0] swapaddr; + wire [DW-1:0] endian_swapped_rdata; + reg [31:0] pre_result; + reg [2*DW-1:0] wide_return; + + // }}} + + // xVALID, and xREADY + // {{{ + initial M_AXI_AWVALID = 1'b0; + initial M_AXI_WVALID = 1'b0; + initial M_AXI_ARVALID = 1'b0; + initial M_AXI_BREADY = 1'b0; + initial M_AXI_RREADY = 1'b0; + always @(posedge i_clk) + if (!S_AXI_ARESETN) + begin + // {{{ + M_AXI_AWVALID <= 1'b0; + M_AXI_WVALID <= 1'b0; + M_AXI_ARVALID <= 1'b0; + M_AXI_BREADY <= 1'b0; + M_AXI_RREADY <= 1'b0; + // }}} + end else if (M_AXI_BREADY || M_AXI_RREADY) + begin // Something is outstanding + // {{{ + if (M_AXI_AWREADY) + M_AXI_AWVALID <= M_AXI_AWVALID && misaligned_aw_request; + if (M_AXI_WREADY) + M_AXI_WVALID <= M_AXI_WVALID && misaligned_request; + if (M_AXI_ARREADY) + M_AXI_ARVALID <= M_AXI_ARVALID && misaligned_request; + + if ((M_AXI_BVALID || M_AXI_RVALID) && !misaligned_response_pending) + begin + M_AXI_BREADY <= 1'b0; + M_AXI_RREADY <= 1'b0; + end + // }}} + end else begin // New memory operation + // {{{ + // Initiate a request + M_AXI_AWVALID <= i_op[0]; // Write request + M_AXI_WVALID <= i_op[0]; // Write request + M_AXI_ARVALID <= !i_op[0]; // Read request + + // Set BREADY or RREADY to accept the response. These will + // remain ready until the response is returned. + M_AXI_BREADY <= i_op[0]; + M_AXI_RREADY <= !i_op[0]; + + if (i_cpu_reset || o_err || !i_stb || w_misalignment_err) + begin + M_AXI_AWVALID <= 0; + M_AXI_WVALID <= 0; + M_AXI_ARVALID <= 0; + + M_AXI_BREADY <= 0; + M_AXI_RREADY <= 0; + end + // }}} + end + // }}} + + // r_flushing + // {{{ + initial r_flushing = 1'b0; + always @(posedge i_clk) + if (!S_AXI_ARESETN) + // If everything is reset, then we don't need to worry about + // or wait for any pending returns--they'll be canceled by the + // global reset. + r_flushing <= 1'b0; + else if (M_AXI_BREADY || M_AXI_RREADY) + begin + if (i_cpu_reset) + // If only the CPU is reset, however, we have a problem. + // The bus hasn't been reset, and so it is still active. + // We can't respond to any new requests from the CPU + // until we flush any transactions that are currently + // active. + r_flushing <= 1'b1; + if (M_AXI_BVALID || M_AXI_RVALID) + // A request just came back, therefore we can clear + // r_flushing + r_flushing <= 1'b0; + if (misaligned_response_pending) + // ... unless we're in the middle of a misaligned + // request. In that case, there will be a second + // return that we still need to wait for. This request, + // though, will clear misaligned_response_pending. + r_flushing <= r_flushing || i_cpu_reset; + end else + // If nothing is active, we don't care about the CPU reset. + // Flushing just stays at zero. + r_flushing <= 1'b0; + // }}} + + // M_AXI_AxADDR + // {{{ + initial M_AXI_AWADDR = 0; + always @(posedge i_clk) + if (!S_AXI_ARESETN && OPT_LOWPOWER) + M_AXI_AWADDR <= 0; + else if (!M_AXI_BREADY && !M_AXI_RREADY) + begin // Initial address + // {{{ + M_AXI_AWADDR <= i_addr; + + if (OPT_LOWPOWER && (i_cpu_reset || o_err || !i_stb || w_misalignment_err)) + M_AXI_AWADDR <= 0; + + if (SWAP_ENDIANNESS || SWAP_WSTRB) + begin + // When adjusting endianness, reads (or writes) are + // always full words. This is important since the + // the bytes at issues may (or may not) be in their + // expected locations + if (OPT_ALIGNMENT_ERR) + M_AXI_AWADDR[AXILSB-1:0] <= 0; + else + M_AXI_AWADDR[1:0] <= 0; + end + // }}} + end else if ((M_AXI_AWVALID && M_AXI_AWREADY) + ||(M_AXI_ARVALID && M_AXI_ARREADY)) + begin // Subsequent addresses + // {{{ + M_AXI_AWADDR[C_AXI_ADDR_WIDTH-1:AXILSB] + <= M_AXI_AWADDR[C_AXI_ADDR_WIDTH-1:AXILSB] + 1; + + M_AXI_AWADDR[AXILSB-1:0] <= 0; + + if (OPT_LOWPOWER && ((M_AXI_RREADY && !misaligned_request) + || (M_AXI_BREADY && !misaligned_aw_request))) + M_AXI_AWADDR <= 0; + // }}} + end + + always @(*) + M_AXI_ARADDR = M_AXI_AWADDR; + // }}} + + // AxPROT + // {{{ + localparam [2:0] AXI_UNPRIVILEGED_NONSECURE_DATA_ACCESS = 3'h0; + localparam [2:0] OPT_PROT=AXI_UNPRIVILEGED_NONSECURE_DATA_ACCESS; + + assign M_AXI_AWPROT = OPT_PROT; + assign M_AXI_ARPROT = OPT_PROT; + // }}} + + // shifted_wstrb_* + // {{{ + generate if (SWAP_WSTRB) + begin : BIG_ENDIAN_WSTRB + always @(*) + shifted_wstrb_word = { 4'b1111, {(2*DW/8-4){1'b0}} } + >> i_addr[AXILSB-1:0]; + + always @(*) + shifted_wstrb_halfword = { 2'b11, {(2*DW/8-2){1'b0}} } + >> i_addr[AXILSB-1:0]; + + always @(*) + shifted_wstrb_byte = { 1'b1, {(2*DW/8-1){1'b0}} } + >> i_addr[AXILSB-1:0]; + end else begin : NORMAL_SHIFTED_WSTRB + always @(*) + shifted_wstrb_word = { {(2*DW/8-4){1'b0}}, + 4'b1111} << i_addr[AXILSB-1:0]; + + always @(*) + shifted_wstrb_halfword = { {(2*DW/8-4){1'b0}}, + 4'b0011} << i_addr[AXILSB-1:0]; + + always @(*) + shifted_wstrb_byte = { {(2*DW/8-4){1'b0}}, + 4'b0001} << i_addr[AXILSB-1:0]; + end endgenerate + // }}} + + // Swapping WSTRB bits + // {{{ + generate if (SWAP_ENDIANNESS) + begin : SWAPPING_ENDIANNESS + // {{{ + genvar gw, gb; + + for(gw=0; gw<2*DW/32; gw=gw+1) + begin : FOREACH_32B_WORD + for(gb=0; gb<32/8; gb=gb+1) + begin : FOREACH_BYTE + + always @(*) + begin + swapped_wstrb_word[gw*4+gb] + = shifted_wstrb_word[gw*4+(3-gb)]; + swapped_wstrb_halfword[gw*4+gb] + = shifted_wstrb_halfword[gw*4+(3-gb)]; + swapped_wstrb_byte[gw*4+gb] + = shifted_wstrb_byte[gw*4+(3-gb)]; + end end end + // }}} + end else begin : KEEP_WSTRB + // {{{ + + always @(*) + swapped_wstrb_word = shifted_wstrb_word; + + always @(*) + swapped_wstrb_halfword = shifted_wstrb_halfword; + + always @(*) + swapped_wstrb_byte = shifted_wstrb_byte; + // }}} + end endgenerate + // }}} + + // wdata, wstrb + // {{{ + always @(*) + swapaddr = i_addr[AXILSB-1:0]; + + initial axi_wdata = 0; + initial axi_wstrb = 0; + initial next_wdata = 0; + initial next_wstrb = 0; + always @(posedge i_clk) + if (OPT_LOWPOWER && !S_AXI_ARESETN) + begin + // {{{ + axi_wdata <= 0; + axi_wstrb <= 0; + + next_wdata <= 0; + next_wstrb <= 0; + + r_op <= 0; + // }}} + end else if (i_stb) + begin + // {{{ + if (SWAP_WSTRB) + begin + casez(i_op[2:1]) + 2'b10: { axi_wdata, next_wdata } + <= { i_data[15:0], {(2*C_AXI_DATA_WIDTH-16){1'b0}} } + >> (8*swapaddr); + 2'b11: { axi_wdata, next_wdata } + <= { i_data[7:0], {(2*C_AXI_DATA_WIDTH-8){1'b0}} } + >> (8*swapaddr); + default: { axi_wdata, next_wdata } + <= { i_data, {(2*C_AXI_DATA_WIDTH-32){1'b0}} } + >> (8*swapaddr); + endcase + end else begin + casez(i_op[2:1]) + 2'b10: { next_wdata, axi_wdata } + <= { {(2*C_AXI_DATA_WIDTH-16){1'b0}}, + i_data[15:0] } << (8*swapaddr); + 2'b11: { next_wdata, axi_wdata } + <= { {(2*C_AXI_DATA_WIDTH-8){1'b0}}, + i_data[7:0] } << (8*swapaddr); + default: { next_wdata, axi_wdata } + <= { {(2*C_AXI_DATA_WIDTH-32){1'b0}}, + i_data } << (8*swapaddr); + endcase + end + + // next_wstrb, axi_wstrb + // {{{ + if (SWAP_WSTRB) + begin + casez(i_op[2:1]) + 2'b0?: { axi_wstrb, next_wstrb } <= swapped_wstrb_word; + 2'b10: { axi_wstrb, next_wstrb } <= swapped_wstrb_halfword; + 2'b11: { axi_wstrb, next_wstrb } <= swapped_wstrb_byte; + endcase + end else begin + casez(i_op[2:1]) + 2'b0?: { next_wstrb, axi_wstrb } <= swapped_wstrb_word; + 2'b10: { next_wstrb, axi_wstrb } <= swapped_wstrb_halfword; + 2'b11: { next_wstrb, axi_wstrb } <= swapped_wstrb_byte; + endcase + end + // }}} + + r_op <= { i_op[2:1] , i_addr[AXILSB-1:0] }; + + // On a read set everything to zero but only if OPT_LOWPOWER + // is set + // {{{ + if (OPT_LOWPOWER && !i_op[0]) + { next_wstrb, next_wdata, axi_wstrb, axi_wdata } <= 0; + + if (OPT_ALIGNMENT_ERR) + { next_wstrb, next_wdata } <= 0; + if (OPT_LOWPOWER) + begin + if (w_misalignment_err) + { axi_wdata, axi_wstrb, r_op } <= 0; + if (o_err || i_cpu_reset) + { next_wdata, next_wstrb, + axi_wdata, axi_wstrb, r_op } <= 0; + end + // }}} + // }}} + end else if ((misaligned_request || !OPT_LOWPOWER) && M_AXI_WREADY) + begin + // {{{ + axi_wdata <= next_wdata; + axi_wstrb <= next_wstrb; + if (OPT_LOWPOWER) + { next_wdata, next_wstrb } <= 0; + // }}} + end else if (OPT_LOWPOWER && M_AXI_WREADY) + begin + // {{{ + axi_wdata <= 0; + axi_wstrb <= 0; + // }}} + end + // }}} + + // M_AXI_WDATA, M_AXI_WSTRB + // {{{ + generate if (SWAP_ENDIANNESS) + begin : SWAP_WRITE_DATA_STRB + // {{{ + genvar gw, gb; + + for(gw=0; gw= (1<= (1<> (8*r_op[AXILSB-1:0]); + else + wide_return = { {(DW){1'b0}}, endian_swapped_rdata } + >> (8*r_op[AXILSB-1:0]); + end + + if (OPT_LOWPOWER && (!M_AXI_RVALID || M_AXI_RRESP[1])) + wide_return = 0; + end + + always @(*) + begin + if (SWAP_WSTRB) + begin + pre_result = 0; + casez(r_op[AXILSB +: 2]) + 2'b10: pre_result[15:0] = wide_return[(2*DW)-1:(2*DW)-16]; + 2'b11: pre_result[7:0] = wide_return[(2*DW)-1:(2*DW)-8]; + default: pre_result[31:0] = wide_return[(2*DW-1):(2*DW-32)]; + endcase + + end else + pre_result = wide_return[31:0]; + end + + // }}} + // last_result, o_result + // {{{ + always @(posedge i_clk) + if (OPT_LOWPOWER &&(!M_AXI_RREADY + || !S_AXI_ARESETN || r_flushing || i_cpu_reset)) + { last_result, o_result } <= 0; + else begin + // {{{ + if (OPT_LOWPOWER) + o_result <= 0; + + if (M_AXI_RVALID) + begin + // {{{ + if (OPT_LOWPOWER && (M_AXI_RRESP[1] || !misaligned_response_pending)) + last_result <= 0; + else + last_result <= endian_swapped_rdata; + + o_result <= pre_result[31:0]; + + if (OPT_SIGN_EXTEND) + begin + // {{{ + // verilator coverage_off + // Optionally sign extend the return result. + // ... would violate ZipCPU ISA + casez(r_op[AXILSB +: 2]) + 2'b10: o_result[31:16] <= {(16){pre_result[15]}}; + 2'b11: o_result[31: 8] <= {(24){pre_result[7]}}; + default: begin end + endcase + // verilator coverage_on + // }}} + end else begin + // Fill unused return bits with zeros + casez(r_op[AXILSB +: 2]) + 2'b10: o_result[31:16] <= 0; + 2'b11: o_result[31: 8] <= 0; + default: begin end + endcase + end + + if (OPT_LOWPOWER && M_AXI_RRESP[1]) + o_result <= 0; + // }}} + end + + if (OPT_ALIGNMENT_ERR) + last_result <= 0; + + if (OPT_LOWPOWER && (pending_err || misaligned_response_pending)) + o_result <= 0; + // }}} + end + // }}} + + // Make verilator happy + // {{{ + // verilator coverage_off + // verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, i_lock, M_AXI_RRESP[0], M_AXI_BRESP[0] }; + + generate if (SWAP_WSTRB) + begin + wire wide_unused; + + if (SWAP_WSTRB) + begin + assign wide_unused = &{ 1'b0, + wide_return[2*DW-32-1:0] }; + end else begin + assign wide_unused = &{ 1'b0, wide_return[2*DW-1:32] }; + end + end endgenerate + // verilator lint_on UNUSED + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL + // Local declarations + // {{{ +`define ASSERT assert +`ifdef AXILOPS +`define ASSUME assume +`else +`define ASSUME assert +`endif + localparam F_LGDEPTH = 2; + + reg f_misaligned; + wire [F_LGDEPTH-1:0] faxil_rd_outstanding, faxil_wr_outstanding, + faxil_awr_outstanding; + wire f_pc, f_gie, f_read_cycle; + + reg f_past_valid; + initial f_past_valid = 0; + always @(posedge i_clk) + f_past_valid = 1'b1; + always @(*) + if (!f_past_valid) + `ASSUME(!S_AXI_ARESETN); + // }}} + + //////////////////////////////////////////////////////////////////////// + // + // Bus property checks + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + faxil_master #( + // {{{ + .C_AXI_DATA_WIDTH(C_AXI_DATA_WIDTH), + .C_AXI_ADDR_WIDTH(C_AXI_ADDR_WIDTH), + .F_OPT_ASSUME_RESET(1'b1), + .F_LGDEPTH(F_LGDEPTH) + // }}} + ) faxil( + // {{{ + .i_clk(S_AXI_ACLK), .i_axi_reset_n(S_AXI_ARESETN), + // + .i_axi_awvalid(M_AXI_AWVALID), + .i_axi_awready(M_AXI_AWREADY), + .i_axi_awaddr( M_AXI_AWADDR), + .i_axi_awprot( M_AXI_AWPROT), + // + .i_axi_wvalid(M_AXI_WVALID), + .i_axi_wready(M_AXI_WREADY), + .i_axi_wdata( M_AXI_WDATA), + .i_axi_wstrb( M_AXI_WSTRB), + // + .i_axi_bvalid(M_AXI_BVALID), + .i_axi_bready(M_AXI_BREADY), + .i_axi_bresp( M_AXI_BRESP), + // + .i_axi_arvalid(M_AXI_ARVALID), + .i_axi_arready(M_AXI_ARREADY), + .i_axi_araddr( M_AXI_ARADDR), + .i_axi_arprot( M_AXI_ARPROT), + // + .i_axi_rvalid(M_AXI_RVALID), + .i_axi_rready(M_AXI_RREADY), + .i_axi_rdata( M_AXI_RDATA), + .i_axi_rresp( M_AXI_RRESP), + // + .f_axi_rd_outstanding(faxil_rd_outstanding), + .f_axi_wr_outstanding(faxil_wr_outstanding), + .f_axi_awr_outstanding(faxil_awr_outstanding) + // }}} + ); + + + always @(*) + begin + if (misaligned_request) + `ASSERT(M_AXI_WVALID || M_AXI_ARVALID); + if (misaligned_aw_request) + `ASSERT(M_AXI_AWVALID); + if (!misaligned_response_pending) + begin + `ASSERT(faxil_awr_outstanding <= (M_AXI_AWVALID ? 0:1)); + `ASSERT(faxil_wr_outstanding <= (M_AXI_WVALID ? 0:1)); + `ASSERT(faxil_rd_outstanding <= (M_AXI_ARVALID ? 0:1)); + + `ASSERT(!misaligned_request); + `ASSERT(!misaligned_aw_request); + end else if (M_AXI_RREADY) + `ASSERT(misaligned_read); + + if (!M_AXI_RREADY) + begin + `ASSERT(!M_AXI_ARVALID); + `ASSERT(faxil_rd_outstanding == 0); + // `ASSERT(misaligned_read == 1'b0); + end else begin + if (misaligned_request) + `ASSERT(faxil_rd_outstanding == 0); + `ASSERT(faxil_rd_outstanding <= 1 + (misaligned_read ? 1:0)); + end + + if (!M_AXI_BREADY) + begin + `ASSERT(!M_AXI_AWVALID); + `ASSERT(!M_AXI_WVALID); + `ASSERT(faxil_awr_outstanding == 0); + `ASSERT(faxil_wr_outstanding == 0); + end else begin + if (misaligned_request) + `ASSERT(faxil_wr_outstanding == 0); + if (misaligned_aw_request) + `ASSERT(faxil_awr_outstanding == 0); + `ASSERT(faxil_awr_outstanding <= 2); + `ASSERT(faxil_wr_outstanding <= 2); + + case({misaligned_request, + misaligned_aw_request, + misaligned_response_pending}) + 3'b000: begin + `ASSERT(faxil_awr_outstanding<= (M_AXI_BREADY ? 1:0)); + `ASSERT(faxil_wr_outstanding <= (M_AXI_BREADY ? 1:0)); + end + 3'b001: begin + `ASSERT(faxil_awr_outstanding<= 1 + (M_AXI_AWVALID ? 0:1)); + `ASSERT(faxil_wr_outstanding <= 1 + (M_AXI_WVALID ? 0:1)); + end + 3'b010: `ASSERT(0); + 3'b011: begin + `ASSERT(faxil_wr_outstanding<= 1 + (M_AXI_WVALID ? 0:1)); + `ASSERT(faxil_awr_outstanding == 0); + `ASSERT(M_AXI_AWVALID); + end + 3'b100: `ASSERT(0); + 3'b101: begin + `ASSERT(faxil_awr_outstanding<= 1 + (M_AXI_AWVALID ? 0:1)); + `ASSERT(faxil_wr_outstanding == 0); + `ASSERT(M_AXI_WVALID); + end + 3'b110: `ASSERT(0); + 3'b111: begin + `ASSERT(faxil_awr_outstanding == 0); + `ASSERT(faxil_wr_outstanding == 0); + `ASSERT(M_AXI_AWVALID); + `ASSERT(M_AXI_WVALID); + end + default: begin end + endcase + end + + + // Rule: Only one of the two xREADY's may be valid, never both + `ASSERT(!M_AXI_BREADY || !M_AXI_RREADY); + end + + // f_misaligned + // {{{ + always @(*) + begin + f_misaligned = 0; + case(r_op[AXILSB +: 2]) + 2'b01: f_misaligned = ((r_op[AXILSB-1:0] + 3) >= (1<= (1< 0 && (!OPT_LOWPOWER || (f_read_cycle && !o_err))) + assert(o_wreg == f_last_reg); + + always @(*) + if (o_busy && (!OPT_LOWPOWER || (M_AXI_RREADY && !r_flushing))) + assert(o_wreg[4] == f_gie); + + always @(*) + if (M_AXI_RREADY) + begin + assert(f_read_cycle || r_flushing); + end else if (M_AXI_BREADY) + assert(!f_read_cycle); + + // ???? Not written yet + // + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Cover properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + reg [3:0] cvr_writes, cvr_reads; + + initial cvr_writes = 0; + always @(posedge i_clk) + if (!S_AXI_ARESETN) + cvr_writes <= 0; + else if (M_AXI_BVALID&& !misaligned_response_pending && !cvr_writes[3]) + cvr_writes <= cvr_writes + 1; + + initial cvr_reads = 0; + always @(posedge i_clk) + if (!S_AXI_ARESETN) + cvr_reads <= 0; + else if (M_AXI_RVALID && !misaligned_response_pending && !cvr_reads[3]) + cvr_reads <= cvr_reads + 1; + + // Cover a write response + always @(posedge i_clk) + cover(M_AXI_BVALID && !M_AXI_BRESP[1]); + always @(posedge i_clk) + cover(M_AXI_BVALID && M_AXI_BRESP[1]); + + always @(posedge i_clk) + cover(M_AXI_RVALID && !M_AXI_RRESP[1]); + always @(posedge i_clk) + cover(M_AXI_RVALID && M_AXI_RRESP[1]); + + + always @(posedge i_clk) + cover(cvr_writes > 3); + + always @(posedge i_clk) + cover(cvr_reads > 3); + // }}} +`endif +// }}} +endmodule +// +// +// Usage (from yosys): +// (BFOR) (!ZOI,ALIGN) (ZOI,ALIGN) (!ZOI,!ALIGN) +// Cells 230 226 281 225 +// FDRE 114 116 116 116 +// LUT2 17 23 76 19 +// LUT3 9 23 17 20 +// LUT4 15 4 11 14 +// LUT5 18 18 7 15 +// LUT6 33 18 54 38 +// MUX7 16 12 2 +// MUX8 8 1 1 +// +// diff --git a/rtl/cpu/axilperiphs.v b/rtl/cpu/axilperiphs.v new file mode 100644 index 0000000..7b35923 --- /dev/null +++ b/rtl/cpu/axilperiphs.v @@ -0,0 +1,772 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: axilperiphs +// {{{ +// Project: WB2AXIPSP: bus bridges and other odds and ends +// +// Purpose: +// Registers: +// 0x00 PIC Interrupt controller +// 0x04 Watchdog +// 0x08 (Reserved) +// 0x0c CTRIC Secondary interrupt controller +// 0x10 TMRA +// 0x14 TMRB +// 0x18 TMRC +// 0x1c JIFF +// (No User counters) +// (No DMA) +// +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2020-2022, Gisselquist Technology, LLC +// {{{ +// +// This file is part of the WB2AXIP project. +// +// The WB2AXIP project contains free software and gateware, licensed under the +// Apache License, Version 2.0 (the "License"). You may not use this project, +// or this file, except in compliance with the License. You may obtain a copy +// of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module axilperiphs #( + // {{{ + // + // Size of the AXI-lite bus. These are fixed, since 1) AXI-lite + // is fixed at a width of 32-bits by Xilinx def'n, and 2) since + // we only ever have 4 configuration words. + parameter C_AXI_ADDR_WIDTH = 6, + localparam C_AXI_DATA_WIDTH = 32, + parameter [0:0] OPT_SKIDBUFFER = 1'b1, + parameter [0:0] OPT_LOWPOWER = 0, + parameter EXTERNAL_INTERRUPTS = 1, + parameter [0:0] OPT_COUNTERS = 1, + localparam ADDRLSB = $clog2(C_AXI_DATA_WIDTH)-3 + // }}} + ) ( + // {{{ + input wire S_AXI_ACLK, + input wire S_AXI_ARESETN, + // AXI-lite interface + // {{{ + input wire S_AXI_AWVALID, + output wire S_AXI_AWREADY, + input wire [C_AXI_ADDR_WIDTH-1:0] S_AXI_AWADDR, + input wire [2:0] S_AXI_AWPROT, + // + input wire S_AXI_WVALID, + output wire S_AXI_WREADY, + input wire [C_AXI_DATA_WIDTH-1:0] S_AXI_WDATA, + input wire [C_AXI_DATA_WIDTH/8-1:0] S_AXI_WSTRB, + // + output wire S_AXI_BVALID, + input wire S_AXI_BREADY, + output wire [1:0] S_AXI_BRESP, + // + input wire S_AXI_ARVALID, + output wire S_AXI_ARREADY, + input wire [C_AXI_ADDR_WIDTH-1:0] S_AXI_ARADDR, + input wire [2:0] S_AXI_ARPROT, + // + output wire S_AXI_RVALID, + input wire S_AXI_RREADY, + output wire [C_AXI_DATA_WIDTH-1:0] S_AXI_RDATA, + output wire [1:0] S_AXI_RRESP, + // }}} + input wire i_cpu_reset, + input wire i_cpu_halted, + input wire i_cpu_gie, + input wire i_cpu_pfstall, + input wire i_cpu_opstall, + input wire i_cpu_icount, + input wire [EXTERNAL_INTERRUPTS-1:0] i_ivec, + output wire o_interrupt, + output wire o_watchdog_reset + // }}} + ); + + //////////////////////////////////////////////////////////////////////// + // + // Register/wire signal declarations + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + localparam [3:0] ADR_PIC = 4'h0, + ADR_WATCHDOG = 4'h1, + ADR_APIC = 4'h2; + // No bus watchdog + + localparam [3:0] ADR_TIMERA = 4'h4, + ADR_TIMERB = 4'h5, + ADR_TIMERC = 4'h6, + ADR_JIFFIES= 4'h7; + + localparam [3:0] ADR_MCLOCKS = 4'h8, + ADR_MOPSTALL = 4'h9, + ADR_MPFSTALL = 4'ha, + ADR_MICOUNT = 4'hb; + + localparam [3:0] ADR_UCLOCKS = 4'hc, + ADR_UOPSTALL = 4'hd, + ADR_UPFSTALL = 4'he, + ADR_UICOUNT = 4'hf; + + wire i_reset = !S_AXI_ARESETN; + + wire axil_write_ready; + wire [C_AXI_ADDR_WIDTH-ADDRLSB-1:0] awskd_addr; + // + wire [C_AXI_DATA_WIDTH-1:0] wskd_data; + wire [C_AXI_DATA_WIDTH/8-1:0] wskd_strb; + reg axil_bvalid; + // + wire axil_read_ready; + wire [C_AXI_ADDR_WIDTH-ADDRLSB-1:0] arskd_addr; + reg [C_AXI_DATA_WIDTH-1:0] axil_read_data; + reg axil_read_valid; + + wire pic_stall, pic_ack; + wire [31:0] pic_data; + + wire wdog_stall, wdog_ack; + wire [31:0] wdog_data; + + wire apic_stall, apic_ack, apic_int; + wire [31:0] apic_data; + + wire tmra_stall, tmra_ack, tmra_int; + wire [31:0] tmra_data; + + wire tmrb_stall, tmrb_ack, tmrb_int; + wire [31:0] tmrb_data; + + wire tmrc_stall, tmrc_ack, tmrc_int; + wire [31:0] tmrc_data; + + wire jif_stall, jif_ack, jif_int; + wire [31:0] jif_data; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // AXI-lite signaling + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // + // Write signaling + // + // {{{ + + generate if (OPT_SKIDBUFFER) + begin : SKIDBUFFER_WRITE + + wire awskd_valid, wskd_valid; + + skidbuffer #(.OPT_OUTREG(0), + .OPT_LOWPOWER(OPT_LOWPOWER), + .DW(C_AXI_ADDR_WIDTH-ADDRLSB)) + axilawskid(// + .i_clk(S_AXI_ACLK), .i_reset(i_reset), + .i_valid(S_AXI_AWVALID), .o_ready(S_AXI_AWREADY), + .i_data(S_AXI_AWADDR[C_AXI_ADDR_WIDTH-1:ADDRLSB]), + .o_valid(awskd_valid), .i_ready(axil_write_ready), + .o_data(awskd_addr)); + + skidbuffer #(.OPT_OUTREG(0), + .OPT_LOWPOWER(OPT_LOWPOWER), + .DW(C_AXI_DATA_WIDTH+C_AXI_DATA_WIDTH/8)) + axilwskid(// + .i_clk(S_AXI_ACLK), .i_reset(i_reset), + .i_valid(S_AXI_WVALID), .o_ready(S_AXI_WREADY), + .i_data({ S_AXI_WDATA, S_AXI_WSTRB }), + .o_valid(wskd_valid), .i_ready(axil_write_ready), + .o_data({ wskd_data, wskd_strb })); + + assign axil_write_ready = awskd_valid && wskd_valid + && (!S_AXI_BVALID || S_AXI_BREADY); + + end else begin : SIMPLE_WRITES + + reg axil_awready; + + initial axil_awready = 1'b0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + axil_awready <= 1'b0; + else + axil_awready <= !axil_awready + && (S_AXI_AWVALID && S_AXI_WVALID) + && (!S_AXI_BVALID || S_AXI_BREADY); + + assign S_AXI_AWREADY = axil_awready; + assign S_AXI_WREADY = axil_awready; + + assign awskd_addr = S_AXI_AWADDR[C_AXI_ADDR_WIDTH-1:ADDRLSB]; + assign wskd_data = S_AXI_WDATA; + assign wskd_strb = S_AXI_WSTRB; + + assign axil_write_ready = axil_awready; + + end endgenerate + + initial axil_bvalid = 0; + always @(posedge S_AXI_ACLK) + if (i_reset) + axil_bvalid <= 0; + else if (axil_write_ready) + axil_bvalid <= 1; + else if (S_AXI_BREADY) + axil_bvalid <= 0; + + assign S_AXI_BVALID = axil_bvalid; + assign S_AXI_BRESP = 2'b00; + // }}} + + // + // Read signaling + // + // {{{ + + generate if (OPT_SKIDBUFFER) + begin : SKIDBUFFER_READ + + wire arskd_valid; + + skidbuffer #(.OPT_OUTREG(0), + .OPT_LOWPOWER(OPT_LOWPOWER), + .DW(C_AXI_ADDR_WIDTH-ADDRLSB)) + axilarskid(// + .i_clk(S_AXI_ACLK), .i_reset(i_reset), + .i_valid(S_AXI_ARVALID), .o_ready(S_AXI_ARREADY), + .i_data(S_AXI_ARADDR[C_AXI_ADDR_WIDTH-1:ADDRLSB]), + .o_valid(arskd_valid), .i_ready(axil_read_ready), + .o_data(arskd_addr)); + + assign axil_read_ready = arskd_valid + && (!axil_read_valid || S_AXI_RREADY); + + end else begin : SIMPLE_READS + + reg axil_arready; + + always @(*) + axil_arready = !S_AXI_RVALID; + + assign arskd_addr = S_AXI_ARADDR[C_AXI_ADDR_WIDTH-1:ADDRLSB]; + assign S_AXI_ARREADY = axil_arready; + assign axil_read_ready = (S_AXI_ARVALID && S_AXI_ARREADY); + + end endgenerate + + initial axil_read_valid = 1'b0; + always @(posedge S_AXI_ACLK) + if (i_reset) + axil_read_valid <= 1'b0; + else if (axil_read_ready) + axil_read_valid <= 1'b1; + else if (S_AXI_RREADY) + axil_read_valid <= 1'b0; + + assign S_AXI_RVALID = axil_read_valid; + assign S_AXI_RDATA = axil_read_data; + assign S_AXI_RRESP = 2'b00; + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // AXI-lite register logic + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + //////////////////////////////////////////////////////////////////////// + // + // Interrupt handling + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + reg [30:0] int_vector; + + // Generate the interrupt vector + // {{{ + generate if (EXTERNAL_INTERRUPTS == 0) + begin : NO_EXTERNAL_INTERRUPTS + // {{{ + always @(*) + begin + int_vector = 0; + int_vector[5:0] = { apic_int, tmra_int, tmrb_int, + tmrc_int, jif_int, 1'b0 }; + end + // }}} + end else if (EXTERNAL_INTERRUPTS == 1) + begin : SINGLE_EXTERNAL_INTERRUPT + // {{{ + + always @(*) + begin + int_vector = 0; + int_vector[5:0] = { apic_int, tmra_int, tmrb_int, + tmrc_int, jif_int, i_ivec[0] }; + end + // }}} + end else begin : MANY_EXTERNAL_INTERRUPTS + // {{{ + always @(*) + begin + int_vector = 0; + int_vector[5:0] = { apic_int, tmra_int, tmrb_int, + tmrc_int, jif_int, i_ivec[0] }; + int_vector[EXTERNAL_INTERRUPTS+5-1:6] + = i_ivec[EXTERNAL_INTERRUPTS-1:1]; + end + // }}} + end endgenerate + // }}} + + icontrol #(.IUSED(15)) + pic( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(i_cpu_reset), + .i_wb_cyc(axil_write_ready), + .i_wb_stb(axil_write_ready && awskd_addr == ADR_PIC), + .i_wb_we(1'b1), .i_wb_data(wskd_data), .i_wb_sel(wskd_strb), + .o_wb_stall(pic_stall), .o_wb_ack(pic_ack),.o_wb_data(pic_data), + .i_brd_ints(int_vector[14:0]), .o_interrupt(o_interrupt) + // }}} + ); + + ziptimer #(32,31,0) + watchdog( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(i_cpu_reset), + .i_ce(!i_cpu_halted), .i_wb_cyc(1'b1), + .i_wb_stb(axil_write_ready && awskd_addr == ADR_WATCHDOG), + .i_wb_we(1'b1), .i_wb_data(wskd_data), .i_wb_sel(wskd_strb), + .o_wb_stall(wdog_stall), + .o_wb_ack(wdog_ack), .o_wb_data(wdog_data), + .o_int(o_watchdog_reset) + // }}} + ); + + // APIC + // {{{ + generate if (EXTERNAL_INTERRUPTS > 15) + begin : APIC + + icontrol #(.IUSED(15)) + apic( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(i_cpu_reset), + .i_wb_cyc(axil_write_ready), + .i_wb_stb(axil_write_ready && awskd_addr == ADR_APIC), + .i_wb_we(1'b1), .i_wb_data(wskd_data), + .i_wb_sel(wskd_strb), + .o_wb_stall(apic_stall), .o_wb_ack(apic_ack), + .o_wb_data(apic_data), + .i_brd_ints(int_vector[30:15]), + .o_interrupt(o_interrupt) + // }}} + ); + + end else begin + + assign apic_data = 0; + assign apic_stall = 0; + assign apic_ack = 0; + assign apic_int = 0; + + end endgenerate + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Timers + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + ziptimer + timer_a( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(i_cpu_reset), + .i_ce(!i_cpu_halted), .i_wb_cyc(1'b1), + .i_wb_stb(axil_write_ready && awskd_addr == ADR_TIMERA), + .i_wb_we(1'b1), .i_wb_data(wskd_data), .i_wb_sel(wskd_strb), + .o_wb_stall(tmra_stall), + .o_wb_ack(tmra_ack), .o_wb_data(tmra_data), .o_int(tmra_int) + // }}} + ); + + ziptimer + timer_b( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(i_cpu_reset), + .i_ce(!i_cpu_halted), .i_wb_cyc(1'b1), + .i_wb_stb(axil_write_ready && awskd_addr == ADR_TIMERB), + .i_wb_we(1'b1), .i_wb_data(wskd_data), .i_wb_sel(wskd_strb), + .o_wb_stall(tmrb_stall), + .o_wb_ack(tmrb_ack), .o_wb_data(tmrb_data), .o_int(tmrb_int) + // }}} + ); + + ziptimer + timer_c( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(i_cpu_reset), + .i_ce(!i_cpu_halted), .i_wb_cyc(1'b1), + .i_wb_stb(axil_write_ready && awskd_addr == ADR_TIMERC), + .i_wb_we(1'b1), .i_wb_data(wskd_data), .i_wb_sel(wskd_strb), + .o_wb_stall(tmrc_stall), + .o_wb_ack(tmrc_ack), .o_wb_data(tmrc_data), .o_int(tmrc_int) + // }}} + ); + + zipjiffies + jiffies( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(i_cpu_reset), + .i_ce(!i_cpu_halted), .i_wb_cyc(1'b1), + .i_wb_stb(axil_write_ready && awskd_addr == ADR_JIFFIES), + .i_wb_we(1'b1), .i_wb_data(wskd_data), .i_wb_sel(wskd_strb), + .o_wb_stall(jif_stall), + .o_wb_ack(jif_ack), .o_wb_data(jif_data), .o_int(jif_int) + // }}} + ); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Optional performance counters + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + wire [31:0] mtask, mopstall, mpfstall, micount; + wire [31:0] utask, uopstall, upfstall, uicount; + + generate if (OPT_COUNTERS) + begin : ACCOUNTING_COUNTERS + // {{{ + reg [31:0] r_mtask, r_mopstall, r_mpfstall, r_micount; + reg [31:0] r_utask, r_uopstall, r_upfstall, r_uicount; + + initial { r_mtask, r_mopstall, r_mpfstall, r_micount } = 0; + always @(posedge S_AXI_ACLK) + if (i_cpu_reset) + { r_mtask, r_mopstall, r_mpfstall, r_micount } <= 0; + else begin + if (!i_cpu_halted) + r_mtask <= r_mtask + 1; + if (i_cpu_opstall) + r_mopstall <= mopstall + 1; + if (i_cpu_pfstall) + r_mpfstall <= r_mpfstall + 1; + if (i_cpu_icount) + r_micount <= r_micount + 1; + + if (axil_write_ready) + case(awskd_addr) + ADR_MCLOCKS: begin + // {{{ + if(wskd_strb[0]) r_mtask[ 7: 0]<=wskd_data[ 7: 0]; + if(wskd_strb[1]) r_mtask[15: 8]<=wskd_data[15: 8]; + if(wskd_strb[2]) r_mtask[23:16]<=wskd_data[23:16]; + if(wskd_strb[3]) r_mtask[31:24]<=wskd_data[31:24]; + end + // }}} + ADR_MOPSTALL: begin + // {{{ + if(wskd_strb[0]) r_mopstall[ 7: 0]<=wskd_data[ 7: 0]; + if(wskd_strb[1]) r_mopstall[15: 8]<=wskd_data[15: 8]; + if(wskd_strb[2]) r_mopstall[23:16]<=wskd_data[23:16]; + if(wskd_strb[3]) r_mopstall[31:24]<=wskd_data[31:24]; + end + // }}} + ADR_MPFSTALL: begin + // {{{ + if(wskd_strb[0]) r_mpfstall[ 7: 0]<=wskd_data[ 7: 0]; + if(wskd_strb[1]) r_mpfstall[15: 8]<=wskd_data[15: 8]; + if(wskd_strb[2]) r_mpfstall[23:16]<=wskd_data[23:16]; + if(wskd_strb[3]) r_mpfstall[31:24]<=wskd_data[31:24]; + end + // }}} + ADR_MICOUNT: begin + // {{{ + if(wskd_strb[0]) r_micount[ 7: 0]<=wskd_data[ 7: 0]; + if(wskd_strb[1]) r_micount[15: 8]<=wskd_data[15: 8]; + if(wskd_strb[2]) r_micount[23:16]<=wskd_data[23:16]; + if(wskd_strb[3]) r_micount[31:24]<=wskd_data[31:24]; + end + // }}} + default: begin end + endcase + end + + initial { r_utask, r_uopstall, r_upfstall, r_uicount } = 0; + always @(posedge S_AXI_ACLK) + if (i_cpu_reset) + { r_utask, r_uopstall, r_upfstall, r_uicount } <= 0; + else begin + if (!i_cpu_halted && i_cpu_gie) + r_utask <= r_utask + 1; + if (i_cpu_opstall && i_cpu_gie) + r_uopstall <= r_uopstall + 1; + if (i_cpu_pfstall && i_cpu_gie) + r_upfstall <= r_upfstall + 1; + if (i_cpu_icount && i_cpu_gie) + r_uicount <= r_uicount + 1; + + if (axil_write_ready) + case(awskd_addr) + ADR_UCLOCKS: begin + // {{{ + if(wskd_strb[0]) r_utask[ 7: 0]<=wskd_data[ 7: 0]; + if(wskd_strb[1]) r_utask[15: 8]<=wskd_data[15: 8]; + if(wskd_strb[2]) r_utask[23:16]<=wskd_data[23:16]; + if(wskd_strb[3]) r_utask[31:24]<=wskd_data[31:24]; + end + // }}} + ADR_UOPSTALL: begin + // {{{ + if(wskd_strb[0]) r_uopstall[ 7: 0]<=wskd_data[ 7: 0]; + if(wskd_strb[1]) r_uopstall[15: 8]<=wskd_data[15: 8]; + if(wskd_strb[2]) r_uopstall[23:16]<=wskd_data[23:16]; + if(wskd_strb[3]) r_uopstall[31:24]<=wskd_data[31:24]; + end + // }}} + ADR_UPFSTALL: begin + // {{{ + if(wskd_strb[0]) r_upfstall[ 7: 0]<=wskd_data[ 7: 0]; + if(wskd_strb[1]) r_upfstall[15: 8]<=wskd_data[15: 8]; + if(wskd_strb[2]) r_upfstall[23:16]<=wskd_data[23:16]; + if(wskd_strb[3]) r_upfstall[31:24]<=wskd_data[31:24]; + end + // }}} + ADR_UICOUNT: begin + // {{{ + if(wskd_strb[0]) r_uicount[ 7: 0]<=wskd_data[ 7: 0]; + if(wskd_strb[1]) r_uicount[15: 8]<=wskd_data[15: 8]; + if(wskd_strb[2]) r_uicount[23:16]<=wskd_data[23:16]; + if(wskd_strb[3]) r_uicount[31:24]<=wskd_data[31:24]; + end + // }}} + default: begin end + endcase + end + + assign { mtask, mopstall, mpfstall, micount } = + { r_mtask, r_mopstall, r_mpfstall, r_micount }; + assign { utask, uopstall, upfstall, uicount } = + { r_utask, r_uopstall, r_upfstall, r_uicount }; + + // }}} + end else begin : NO_ACCOUNTING + + assign { mtask, mopstall, mpfstall, micount } = 0; + assign { utask, uopstall, upfstall, uicount } = 0; + + end endgenerate + // }}} + + // axil_read_data + // {{{ + initial axil_read_data = 0; + always @(posedge S_AXI_ACLK) + if (OPT_LOWPOWER && !S_AXI_ARESETN) + axil_read_data <= 0; + else if (!S_AXI_RVALID || S_AXI_RREADY) + begin + axil_read_data <= 0; + case({ (OPT_COUNTERS & arskd_addr[3]), arskd_addr[2:0] }) + ADR_PIC: axil_read_data <= pic_data; + ADR_WATCHDOG: axil_read_data <= wdog_data; + // 3'b10: axil_read_data <= watchdog_data; + ADR_APIC: axil_read_data <= apic_data; + ADR_TIMERA: axil_read_data <= tmra_data; + ADR_TIMERB: axil_read_data <= tmrb_data; + ADR_TIMERC: axil_read_data <= tmrc_data; + ADR_JIFFIES: axil_read_data <= jif_data; + // Supervisor counters + ADR_MCLOCKS: axil_read_data <= mtask; + ADR_MOPSTALL: axil_read_data <= mopstall; + ADR_MPFSTALL: axil_read_data <= mpfstall; + ADR_MICOUNT: axil_read_data <= micount; + // User counters + ADR_UCLOCKS: axil_read_data <= utask; + ADR_UOPSTALL: axil_read_data <= uopstall; + ADR_UPFSTALL: axil_read_data <= upfstall; + ADR_UICOUNT: axil_read_data <= uicount; + default: axil_read_data <= 0; + endcase + + if (OPT_LOWPOWER && !axil_read_ready) + axil_read_data <= 0; + end + // }}} + + // apply_wstrb + // {{{ + function [C_AXI_DATA_WIDTH-1:0] apply_wstrb; + input [C_AXI_DATA_WIDTH-1:0] prior_data; + input [C_AXI_DATA_WIDTH-1:0] new_data; + input [C_AXI_DATA_WIDTH/8-1:0] wstrb; + + integer k; + for(k=0; k for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +// +`default_nettype none +// }}} +module axilpipe #( + // {{{ + parameter ADDRESS_WIDTH=30, + parameter C_AXI_ADDR_WIDTH = ADDRESS_WIDTH, + parameter C_AXI_DATA_WIDTH = 32, + localparam AW = C_AXI_ADDR_WIDTH, + localparam DW = C_AXI_DATA_WIDTH, + // + // AXI locks are a challenge, and require support from the + // CPU. Specifically, we have to be able to unroll and re-do + // the load instruction on any atomic access failure. For that + // reason, we'll ignore the lock request initially. + parameter [0:0] OPT_ALIGNMENT_ERR = 1'b1, + // Verilator lint_off UNUSED + // This *should* be used -- need to rewrite so that it is + parameter [0:0] OPT_LOWPOWER = 1'b0, + // Verilator lint_on UNUSED + parameter [0:0] SWAP_WSTRB = 0, + parameter [0:0] OPT_SIGN_EXTEND = 0 + // }}} + ) ( + // {{{ + input wire S_AXI_ACLK, + input wire S_AXI_ARESETN, + input wire i_cpu_reset, + // + // CPU interface + // {{{ + input wire i_stb, + input wire i_lock, + input wire [2:0] i_op, + input wire [31:0] i_addr, + input wire [31:0] i_data, + input wire [4:0] i_oreg, + output reg o_busy, + output reg o_pipe_stalled, + output reg o_rdbusy, + output reg o_valid, + output reg o_err, + output reg [4:0] o_wreg, + output reg [31:0] o_result, + // }}} + // + // AXI-Lite bus interface + // {{{ + // Writes + // {{{ + output reg M_AXI_AWVALID, + input wire M_AXI_AWREADY, + output reg [AW-1:0] M_AXI_AWADDR, + // verilator coverage_off + output wire [2:0] M_AXI_AWPROT, + // verilator coverage_on + // + output reg M_AXI_WVALID, + input wire M_AXI_WREADY, + output reg [DW-1:0] M_AXI_WDATA, + output reg [DW/8-1:0] M_AXI_WSTRB, + // + input wire M_AXI_BVALID, + output wire M_AXI_BREADY, + input wire [1:0] M_AXI_BRESP, + // }}} + // Reads + // {{{ + output reg M_AXI_ARVALID, + input wire M_AXI_ARREADY, + output reg [AW-1:0] M_AXI_ARADDR, + // verilator coverage_off + output wire [2:0] M_AXI_ARPROT, + // verilator coverage_on + // + input wire M_AXI_RVALID, + output wire M_AXI_RREADY, + input wire [DW-1:0] M_AXI_RDATA, + input wire [1:0] M_AXI_RRESP + // }}} + // }}} + // }}} + ); + + // Declarations + // {{{ + localparam AXILLSB = $clog2(C_AXI_DATA_WIDTH/8); + localparam AXILSB = $clog2(C_AXI_DATA_WIDTH/8); + localparam LGPIPE = 4; + localparam FIFO_WIDTH = AXILLSB+1+2+5 + 1; + + wire i_clk = S_AXI_ACLK; + + reg w_misaligned; + wire misaligned_request, misaligned_aw_request, pending_err, + w_misalignment_err; + reg [C_AXI_DATA_WIDTH-1:0] next_wdata; + reg [C_AXI_DATA_WIDTH/8-1:0] next_wstrb; + + + reg none_outstanding, bus_abort, + read_abort, write_abort; + reg [LGPIPE:0] beats_outstanding; + reg r_flushing, flush_request, + r_pipe_stalled; + reg [LGPIPE:0] flushcount, new_flushcount; + reg [LGPIPE:0] wraddr, rdaddr; + reg [4:0] ar_oreg; + reg [1:0] ar_op; + reg [AXILSB-1:0] adr_lsb; + reg [FIFO_WIDTH-1:0] fifo_data [0:((1< ((M_AXI_RVALID || M_AXI_BVALID) ? 1:0)) + o_busy <= 1; + else + o_busy <= 0; +`ifdef FORMAL + always @(*) + assert(o_busy == (!none_outstanding || M_AXI_ARVALID + || M_AXI_AWVALID || M_AXI_WVALID)); +`endif + // }}} + + // Read busy + // {{{ + // True if the CPU should expect some kind of pending response from a + // read, and so should stall for that purpose. False otherwise. + initial o_rdbusy = 0; + always @(posedge S_AXI_ACLK) + if (i_cpu_reset || r_flushing) + o_rdbusy <= 0; + else if ((i_stb && w_misalignment_err) || bus_abort) + o_rdbusy <= 0; + else if (i_stb && !i_op[0]) + o_rdbusy <= 1; + else if (o_rdbusy && !M_AXI_ARVALID) + o_rdbusy <= (beats_outstanding > (M_AXI_RVALID ? 1:0)); + +`ifdef FORMAL + reg writing; + + always @(posedge S_AXI_ACLK) + if (i_stb && !o_busy) + writing <= i_op[0]; + + always @(*) + begin + if (writing) + assert(!o_rdbusy); + if (r_flushing) + assert(!o_rdbusy); + if (!o_busy) + assert(!o_rdbusy); + end +`endif + // }}} + + // o_pipe_stalled, r_pipe_stalled + // {{{ + // True if the CPU should expect some kind of pending response from a + // read, and so should stall for that purpose. False otherwise. + generate if (OPT_ALIGNMENT_ERR) + begin : FULL_PIPE_STALL + // {{{ + // Here, we stall if the FIFO is ever full. In this case, + // any new beat will count as only one item to the FIFO, and + // so we can run all the way to full. + reg [LGPIPE:0] beats_committed; + + always @(*) + beats_committed = beats_outstanding + + ((i_stb && !w_misalignment_err) ? 1:0) + + ((M_AXI_AWVALID || M_AXI_WVALID + || M_AXI_ARVALID) ? 1:0); + + initial r_pipe_stalled = 0; + always @(posedge S_AXI_ACLK) + if (i_cpu_reset) + r_pipe_stalled <= 0; + else if (M_AXI_RVALID || M_AXI_BVALID) + r_pipe_stalled <= 0; + else // if (!r_pipe_stalled) + r_pipe_stalled <= (beats_committed >= (1<= (1<= (1<= (1< 1); + end + + casez({M_AXI_AWVALID && (M_AXI_AWREADY && !misaligned_aw_request), + (M_AXI_WVALID && M_AXI_WREADY && !misaligned_request), + (M_AXI_ARVALID && M_AXI_ARREADY && !misaligned_request) }) + 3'b001: flush_request <= 0; + 3'b10?: flush_request <= M_AXI_WVALID; + 3'b01?: flush_request <= M_AXI_AWVALID; + 3'b11?: flush_request <= 0; + default: begin end + endcase + // }}} + end +`ifdef FORMAL + always @(*) + begin + assert(r_flushing == (flushcount > 0)); + if (!r_flushing) + begin + assert(!flush_request); + end else + assert(flush_request == (M_AXI_AWVALID || M_AXI_WVALID || M_AXI_ARVALID)); + if (r_flushing && !flush_request) + begin + assert(!misaligned_request); + assert(!misaligned_aw_request); + end + + if (flush_request) + begin + assert(flushcount == beats_outstanding + 1 + + ((misaligned_request + || misaligned_aw_request) ? 1:0)); + // else if (faxil_rd_outstanding > 0 || M_AXI_ARVALID) + end else if (r_flushing) + begin + assert(beats_outstanding == flushcount); + end else + assert(beats_outstanding >= flushcount); + end +`endif + // }}} + + // Bus addressing + // {{{ + initial M_AXI_AWADDR = 0; + always @(posedge i_clk) + if (i_stb) + begin + M_AXI_AWADDR <= i_addr[AW-1:0]; + if (SWAP_WSTRB) + M_AXI_AWADDR[AXILSB-1:0] <= 0; + end else if (!OPT_ALIGNMENT_ERR + && ((M_AXI_AWVALID && M_AXI_AWREADY) // && misaligned_aw_request + || (M_AXI_ARVALID && M_AXI_ARREADY))) // && misaligned_request)) + begin + M_AXI_AWADDR[AW-1:AXILSB] <= M_AXI_AWADDR[AW-1:AXILSB] + 1; + M_AXI_AWADDR[AXILSB-1:0] <= 0; + end + + always @(*) + M_AXI_ARADDR = M_AXI_AWADDR; + // }}} + + // Is this request misaligned? + // {{{ + always @(*) + casez(i_op[2:1]) + // Full word + 2'b0?: w_misaligned = (i_addr[AXILSB-1:0]+3) >= (1<= (1<> (i_addr[AXILSB-1:0] * 8); + 2'b11: wide_wdata + = { i_data[7:0], {(2*C_AXI_DATA_WIDTH-8){1'b0}} } + >> (i_addr[AXILSB-1:0] * 8); + default: wide_wdata + = ({ i_data, {(2*C_AXI_DATA_WIDTH-32){ 1'b0 }} } + >> (i_addr[AXILSB-1:0] * 8)); + endcase + + casez(i_op[2:1]) + 2'b0?: wide_wstrb + = { 4'b1111, {(2*C_AXI_DATA_WIDTH/8-4){1'b0}} } >> i_addr[AXILSB-1:0]; + 2'b10: wide_wstrb + = { 2'b11, {(2*C_AXI_DATA_WIDTH/8-2){1'b0}} } >> i_addr[AXILSB-1:0]; + 2'b11: wide_wstrb + = { 1'b1, {(2*C_AXI_DATA_WIDTH/8-1){1'b0}} } >> i_addr[AXILSB-1:0]; + endcase + // }}} + end else begin : LITTLE_ENDIAN_DATA + // {{{ + casez(i_op[2:1]) + 2'b10: wide_wdata + = { {(2*C_AXI_DATA_WIDTH-16){1'b0}}, i_data[15:0] } << (8*i_addr[AXILSB-1:0]); + 2'b11: wide_wdata + = { {(2*C_AXI_DATA_WIDTH-8){1'b0}}, i_data[7:0] } << (8*i_addr[AXILSB-1:0]); + default: wide_wdata + = { {(2*C_AXI_DATA_WIDTH-32){1'b0}}, i_data } + << (8*i_addr[AXILSB-1:0]); + endcase + + casez(i_op[2:1]) + 2'b0?: wide_wstrb + = { {(2*C_AXI_DATA_WIDTH/8-4){1'b0}}, 4'b1111} << i_addr[AXILSB-1:0]; + 2'b10: wide_wstrb + = { {(2*C_AXI_DATA_WIDTH/8-4){1'b0}}, 4'b0011} << i_addr[AXILSB-1:0]; + 2'b11: wide_wstrb + = { {(2*C_AXI_DATA_WIDTH/8-4){1'b0}}, 4'b0001} << i_addr[AXILSB-1:0]; + endcase + // }}} + end + // }}} + + // WDATA and WSTRB + // {{{ + initial M_AXI_WDATA = 0; + initial M_AXI_WSTRB = 0; + initial next_wdata = 0; + initial next_wstrb = 0; + always @(posedge i_clk) + if (i_stb) + begin + if (SWAP_WSTRB) + begin : BACKWARDS_ORDER_REG + // {{{ + { M_AXI_WDATA, next_wdata } <= wide_wdata; + { M_AXI_WSTRB, next_wstrb } <= wide_wstrb; + // }}} + end else begin + // {{{ + { next_wdata, M_AXI_WDATA } <= wide_wdata; + { next_wstrb, M_AXI_WSTRB } <= wide_wstrb; + // }}} + end + + if (OPT_ALIGNMENT_ERR) + { next_wstrb, next_wdata } <= 0; + + end else if ((OPT_LOWPOWER || !OPT_ALIGNMENT_ERR) && M_AXI_WREADY) + begin + M_AXI_WDATA <= next_wdata; + M_AXI_WSTRB <= next_wstrb; + if (OPT_LOWPOWER) + { next_wdata, next_wstrb } <= 0; + end + // }}} + + generate if (OPT_ALIGNMENT_ERR) + begin + // {{{ + // Generate an error on any misaligned request + assign misaligned_request = 1'b0; + + assign misaligned_aw_request = 1'b0; + assign pending_err = 1'b0; + // }}} + end else begin + // {{{ + reg r_misaligned_request, r_misaligned_aw_request, + r_pending_err; + + // misaligned_request + // {{{ + initial r_misaligned_request = 0; + always @(posedge i_clk) + if (!S_AXI_ARESETN) + r_misaligned_request <= 0; + else if (i_stb && !o_err && !i_cpu_reset && !bus_abort) + r_misaligned_request <= w_misaligned; + else if ((M_AXI_WVALID && M_AXI_WREADY) + || (M_AXI_ARVALID && M_AXI_ARREADY)) + r_misaligned_request <= 1'b0; + + assign misaligned_request = r_misaligned_request; + // }}} + + // misaligned_aw_request + // {{{ + initial r_misaligned_aw_request = 0; + always @(posedge i_clk) + if (!S_AXI_ARESETN) + r_misaligned_aw_request <= 0; + else if (i_stb && !o_err && !i_cpu_reset && !write_abort) + r_misaligned_aw_request <= w_misaligned && i_op[0]; + else if (M_AXI_AWREADY) + r_misaligned_aw_request <= 1'b0; + + assign misaligned_aw_request = r_misaligned_aw_request; + // }}} + + // pending_err + // {{{ + initial r_pending_err = 1'b0; + always @(posedge i_clk) + if (i_cpu_reset || i_stb || o_err || r_flushing) + r_pending_err <= 1'b0; + else if ((M_AXI_BVALID && M_AXI_BRESP[1]) + || (M_AXI_RVALID && M_AXI_RRESP[1])) + r_pending_err <= 1'b1; + + assign pending_err = r_pending_err; + // }}} + +`ifdef FORMAL + always @(*) + if (pending_err) + assert(r_flushing || o_err); +`endif + // }}} + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Read transaction FIFO + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // wraddr + // {{{ + initial wraddr = 0; + always @(posedge S_AXI_ACLK) + if (bus_abort || flush_request) // bus_abort includes i_cpu_reset + wraddr <= 0; + else if ((M_AXI_ARVALID && M_AXI_ARREADY) || (M_AXI_WVALID && M_AXI_WREADY)) + wraddr <= wraddr + 1; + // }}} + + // rdaddr + // {{{ + initial rdaddr = 0; + always @(posedge S_AXI_ACLK) + if (bus_abort || r_flushing) + rdaddr <= 0; + else if (M_AXI_RVALID||M_AXI_BVALID) + rdaddr <= rdaddr + 1; + // }}} + + // ar_oreg, ar_op, adr_lsb + // {{{ + always @(posedge S_AXI_ACLK) + if (i_stb) + { ar_oreg, ar_op, adr_lsb } <= { i_oreg, i_op[2:1], i_addr[AXILSB-1:0] }; + else if ((M_AXI_ARVALID && M_AXI_ARREADY)||(M_AXI_WVALID && M_AXI_WREADY)) + adr_lsb <= 0; + // }}} + + // fifo_data + // {{{ + always @(posedge S_AXI_ACLK) + if ((M_AXI_ARVALID && M_AXI_ARREADY) || (M_AXI_WVALID && M_AXI_WREADY)) + fifo_data[wraddr[LGPIPE-1:0]] <= { M_AXI_ARVALID, ar_oreg,ar_op, + misaligned_request, adr_lsb }; + + always @(*) + fifo_read_data = fifo_data[rdaddr[LGPIPE-1:0]]; + + assign { fifo_read_op, fifo_return_reg, fifo_op, + fifo_misaligned, fifo_lsb } = fifo_read_data; + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Read return generation + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // o_valid + // {{{ + initial o_valid = 1'b0; + always @(posedge i_clk) + if (i_cpu_reset || r_flushing) + o_valid <= 1'b0; + else if (OPT_ALIGNMENT_ERR && i_stb && w_misaligned) + o_valid <= 1'b0; + else + o_valid <= M_AXI_RVALID && !M_AXI_RRESP[1] // && !pending_err + && !fifo_misaligned; + // }}} + + // o_wreg + // {{{ + always @(posedge i_clk) + o_wreg <= fifo_return_reg; + // }}} + + // o_result, misdata + // {{{ + // Need to realign any returning data + // wide_return + // {{{ + always @(*) + begin + if (SWAP_WSTRB) + begin + if (fifo_misaligned && !OPT_ALIGNMENT_ERR) + wide_return = { misdata, M_AXI_RDATA } + << (8*fifo_lsb); + else + wide_return = { M_AXI_RDATA, {(DW){1'b0}} } + << (8*fifo_lsb); + + end else begin + if (fifo_misaligned && !OPT_ALIGNMENT_ERR) + wide_return = { M_AXI_RDATA, misdata } >> (8*fifo_lsb); + else + wide_return = { {(C_AXI_DATA_WIDTH){1'b0}}, M_AXI_RDATA } + >> (8*fifo_lsb); + end + + if (OPT_LOWPOWER && (!M_AXI_RVALID || M_AXI_RRESP[1])) + wide_return = 0; + end + + always @(*) + begin + if (SWAP_WSTRB) + begin + pre_result = 0; + + casez(fifo_op) + 2'b10: pre_result[15:0] = { + wide_return[(2*DW)-1:(2*DW)-16] }; + 2'b11: pre_result[7:0] = { + wide_return[(2*DW)-1:(2*DW)-8] }; + default: pre_result[31:0] = wide_return[(2*DW-1):(2*DW-32)]; + endcase + + end else + pre_result = wide_return[31:0]; + end + // }}} + + // misdata + // {{{ + always @(posedge i_clk) + if (OPT_ALIGNMENT_ERR) + misdata <= 0; + else if (M_AXI_RVALID) + begin + if (fifo_misaligned) + misdata <= M_AXI_RDATA; + else + misdata <= 0; + end + // }}} + + // o_result + // {{{ + always @(posedge i_clk) + if (OPT_LOWPOWER && (!S_AXI_ARESETN || r_flushing || i_cpu_reset)) + begin + o_result <= 0; + end else if (!OPT_LOWPOWER || M_AXI_RVALID) + begin + + o_result <= pre_result[31:0]; + + if (OPT_SIGN_EXTEND) + begin + // {{{ + // Optionally sign extend the return result. + case(fifo_op) + 2'b10: o_result[31:16] <= {(16){pre_result[15]}}; + 2'b11: o_result[31: 8] <= {(24){pre_result[7]}}; + default: begin end + endcase + // }}} + end else if (fifo_op[1]) + begin + // {{{ + if (fifo_op[0]) + o_result[31: 8] <= 0; + else + o_result[31:16] <= 0; + // }}} + end + + end + // }}} + // }}} + + // o_err - report bus errors back to the CPU + // {{{ + initial o_err = 1'b0; + always @(posedge i_clk) + if (i_cpu_reset || r_flushing || o_err) + o_err <= 1'b0; + else if (OPT_ALIGNMENT_ERR && i_stb && w_misaligned) + o_err <= 1'b1; + else if (M_AXI_BVALID || M_AXI_RVALID) + o_err <= (M_AXI_BVALID && M_AXI_BRESP[1]) + || (M_AXI_RVALID && M_AXI_RRESP[1]); + else + o_err <= 1'b0; + // }}} + + // Return xREADY -- always ready + // {{{ + assign M_AXI_RREADY = 1; + assign M_AXI_BREADY = 1; + // }}} + + // AxPROT -- CPU data + // {{{ + assign M_AXI_AWPROT = 3'b000; + assign M_AXI_ARPROT = 3'b000; + + // }}} + // }}} + + // Make verilator happy + // {{{ + // verilator coverage_off + // verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, M_AXI_RRESP[0], M_AXI_BRESP[0], i_lock, + // i_addr[31:C_AXI_ADDR_WIDTH], + (&i_addr), + pending_err, adr_lsb, fifo_read_op, + none_outstanding }; + + generate if (SWAP_WSTRB) + begin + wire wide_unused; + + if (SWAP_WSTRB) + begin + assign wide_unused = &{ 1'b0, + wide_return[2*DW-32-1:0] }; + end else begin + assign wide_unused = &{ 1'b0, wide_return[2*DW-1:32] }; + end + end endgenerate + // verilator lint_on UNUSED + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal property section +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL + // Declarations + // {{{ +`define ASSERT assert +`ifndef BMC_ASSERT +`define BMC_ASSERT assume +`endif +`ifdef AXILPIPE +`define ASSUME assume +`else +`define ASSUME assert +`endif + localparam F_LGDEPTH = LGPIPE+1; + + wire [F_LGDEPTH-1:0] faxil_rd_outstanding, faxil_wr_outstanding, + faxil_awr_outstanding; + reg [LGPIPE:0] f_fifo_fill; + + reg f_clrfifo, f_wrfifo, f_rdfifo; + wire misaligned_response_pending; + reg [1:0] f_fsmfifo; + (* anyconst *) reg [LGPIPE:0] f_first_addr; + reg [LGPIPE:0] f_next_addr, + f_penu_addr, f_last_written, + f_distance_to_first, f_distance_to_next; + reg [FIFO_WIDTH-1:0] f_first_data, f_next_data, f_penu_data; + reg [4:0] f_first_return_reg, f_next_return_reg, f_return_reg, + f_penu_return_reg; + reg f_first_in_fifo, f_next_in_fifo, + f_first_misaligned, f_next_misaligned, + f_this_misaligned, f_penu_misaligned, + f_first_read_cycle, f_next_read_cycle, + f_this_read_cycle, f_penu_read_cycle; + + + wire [LGPIPE:0] cpu_outstanding; + wire cpu_gie, cpu_pc, cpu_read_cycle; + wire [4:0] cpu_last_reg, cpu_addr_reg; + reg [4:0] f_ar_areg; + + (* anyseq *) reg [4:0] f_areg; + reg f_done; + + + reg f_past_valid; + initial f_past_valid = 0; + always @(posedge i_clk) + f_past_valid = 1'b1; + + always @(*) + if (!f_past_valid) + `ASSUME(!S_AXI_ARESETN); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Bus property checking + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + faxil_master #( + // {{{ + .C_AXI_DATA_WIDTH(C_AXI_DATA_WIDTH), + .C_AXI_ADDR_WIDTH(C_AXI_ADDR_WIDTH), + .F_OPT_ASSUME_RESET(1'b1), + .F_LGDEPTH(F_LGDEPTH) + // }}} + ) faxil( + // {{{ + .i_clk(S_AXI_ACLK), .i_axi_reset_n(S_AXI_ARESETN), + // + .i_axi_awvalid(M_AXI_AWVALID), + .i_axi_awready(M_AXI_AWREADY), + .i_axi_awaddr( M_AXI_AWADDR), + .i_axi_awprot( M_AXI_AWPROT), + // + .i_axi_wvalid(M_AXI_WVALID), + .i_axi_wready(M_AXI_WREADY), + .i_axi_wdata( M_AXI_WDATA), + .i_axi_wstrb( M_AXI_WSTRB), + // + .i_axi_bvalid(M_AXI_BVALID), + .i_axi_bready(M_AXI_BREADY), + .i_axi_bresp( M_AXI_BRESP), + // + .i_axi_arvalid(M_AXI_ARVALID), + .i_axi_arready(M_AXI_ARREADY), + .i_axi_araddr( M_AXI_ARADDR), + .i_axi_arprot( M_AXI_ARPROT), + // + .i_axi_rvalid(M_AXI_RVALID), + .i_axi_rready(M_AXI_RREADY), + .i_axi_rdata( M_AXI_RDATA), + .i_axi_rresp( M_AXI_RRESP), + // + .f_axi_rd_outstanding(faxil_rd_outstanding), + .f_axi_wr_outstanding(faxil_wr_outstanding), + .f_axi_awr_outstanding(faxil_awr_outstanding) + // }}} + ); + + + always @(*) + f_fifo_fill = wraddr - rdaddr; + + always @(*) + begin + if (misaligned_request) + `ASSERT(M_AXI_WVALID || M_AXI_ARVALID); + if (misaligned_aw_request) + `ASSERT(M_AXI_AWVALID); + + if (M_AXI_ARVALID || faxil_rd_outstanding > 0) + begin + assert(faxil_wr_outstanding == 0 + && faxil_awr_outstanding == 0); + assert(!M_AXI_AWVALID); + assert(!M_AXI_WVALID); + end + + if (faxil_wr_outstanding > 0 || faxil_awr_outstanding > 0 + || M_AXI_AWVALID || M_AXI_WVALID) + begin + assert(faxil_rd_outstanding == 0); + assert(!M_AXI_ARVALID); + end + + // Rule: Only one of the two VALID's may be valid, never both + `ASSERT(!M_AXI_RVALID || (!M_AXI_AWVALID && !M_AXI_WVALID)); + + assert(beats_outstanding + + ((misaligned_request && !misaligned_aw_request && M_AXI_WVALID) ? 1:0) + + ((M_AXI_WVALID && !M_AXI_AWVALID) ? 1:0) + == faxil_rd_outstanding + faxil_awr_outstanding); + + assert(beats_outstanding + + ((misaligned_aw_request && !misaligned_request) ? 1:0) + + ((M_AXI_AWVALID && !M_AXI_WVALID) ? 1:0) + == faxil_rd_outstanding + faxil_wr_outstanding); + + if (OPT_ALIGNMENT_ERR && !r_flushing + && (faxil_rd_outstanding > 0 || M_AXI_ARVALID)) + begin + assert(f_fifo_fill == faxil_rd_outstanding); + end else if (OPT_ALIGNMENT_ERR && !r_flushing + && (faxil_wr_outstanding > 0 || M_AXI_WVALID)) + assert(f_fifo_fill == faxil_wr_outstanding); + + if (faxil_rd_outstanding == 0 && faxil_wr_outstanding == 0) + assert(f_fifo_fill == 0); + end + + always @(*) + if (!o_busy) + `ASSERT(!r_flushing); + + // Following any i_stb request, assuming we are idle, immediately + // begin a bus transaction + always @(posedge i_clk) + if ((f_past_valid)&&($past(i_stb && !o_err)) + &&(!$past(o_busy))&&($past(!i_cpu_reset))) + begin + `ASSERT(o_busy || (OPT_ALIGNMENT_ERR && o_err)); + end + + always @(*) + if (o_busy && !misaligned_request && OPT_LOWPOWER) + begin + assert(next_wdata == 0); + assert(next_wstrb == 0); + end + + // o_err checking + // {{{ + // If a transaction ends in an error, send o_err on the output port. + always @(posedge i_clk) + if (f_past_valid) + begin + if ($past(i_cpu_reset || r_flushing || o_err)) + begin + `ASSERT(!o_err); + end else if ($past(M_AXI_BVALID && M_AXI_BRESP[1])) + begin + `ASSERT(o_err); + end else if ($past(M_AXI_RVALID && M_AXI_RRESP[1])) + begin + `ASSERT(o_err); + end else if (OPT_ALIGNMENT_ERR && $past(i_stb && w_misaligned)) + begin + `ASSERT(o_err); + end else if (!$past(pending_err)) + `ASSERT(!o_err); + end + // }}} + + always @(posedge i_clk) + if ((f_past_valid)&&($past(!i_cpu_reset))&&($past(i_stb))) + begin + // On a write, assert o_wb_we should be true + assert($past(i_op[0] && !o_err + && (!M_AXI_BVALID || !M_AXI_BRESP[1]) + && (!OPT_ALIGNMENT_ERR || !w_misaligned)) + == (M_AXI_AWVALID && M_AXI_WVALID)); + end + + always @(posedge i_clk) + if ((!f_past_valid)||($past(i_cpu_reset))) + `ASSUME(!i_stb); + + always @(*) + if (!S_AXI_ARESETN) + `ASSUME(i_cpu_reset); + + // misaligned_response_pending + // {{{ + generate if (OPT_ALIGNMENT_ERR) + begin : NO_MISALIGNED_RESPONSES + + assign misaligned_response_pending = 0; + + end else begin : MISALIGNED_RESPONSE_PENDING + reg r_misaligned_response_pending; + + always @(*) + begin + r_misaligned_response_pending = fifo_misaligned; + if (wraddr == rdaddr) + r_misaligned_response_pending = 0; + end + + assign misaligned_response_pending + = r_misaligned_response_pending; + end endgenerate + // }}} + + + always @(*) + if (o_busy) + begin + cover(i_stb); + cover(o_valid); + cover(o_err); + end else begin + cover(o_valid); + cover(o_err); + end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Zero on idle checks + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + generate if (OPT_LOWPOWER) + begin + always @(*) + if (!M_AXI_AWVALID && !M_AXI_ARVALID) + `ASSERT(M_AXI_AWADDR == 0); + + always @(*) + if (!M_AXI_WVALID) + begin + `ASSERT(M_AXI_WDATA == 0); + `ASSERT(M_AXI_WSTRB == 0); + + `ASSERT(next_wdata == 0); + `ASSERT(next_wstrb == 0); + end + + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // FIFO property checking + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(*) + begin + // {{{ + f_next_addr = f_first_addr + 1; + f_last_written = wraddr - 1; + + f_penu_addr = rdaddr + 1; + f_penu_data = fifo_data[f_penu_addr[LGPIPE-1:0]]; + + f_clrfifo = (!S_AXI_ARESETN || bus_abort || flush_request); + f_wrfifo = (!f_clrfifo) && ((M_AXI_ARVALID && M_AXI_ARREADY) + || (M_AXI_WVALID && M_AXI_WREADY)); + f_rdfifo = (!f_clrfifo) && ((M_AXI_RVALID && M_AXI_RREADY) + || (M_AXI_BVALID && M_AXI_BREADY)); + + f_distance_to_first = f_first_addr - rdaddr; + f_distance_to_next = f_next_addr - rdaddr; + + f_first_in_fifo = (f_distance_to_first < f_fifo_fill) && (f_fifo_fill > 0); + f_next_in_fifo = (f_distance_to_next < f_fifo_fill) && (f_fifo_fill > 0); + + f_return_reg = fifo_return_reg; + + f_first_return_reg = f_first_data[AXILLSB+3 +: 5]; + f_next_return_reg = f_next_data[AXILLSB+3 +: 5]; + f_penu_return_reg = f_penu_data[AXILLSB+3 +: 5]; + + f_first_misaligned = f_first_data[AXILLSB]; + f_next_misaligned = f_next_data[AXILLSB]; + f_this_misaligned = fifo_read_data[AXILLSB]; + f_penu_misaligned = f_penu_data[AXILLSB]; + + f_first_read_cycle = f_first_data[FIFO_WIDTH-1]; + f_next_read_cycle = f_next_data[FIFO_WIDTH-1]; + f_this_read_cycle = fifo_read_data[FIFO_WIDTH-1]; + f_penu_read_cycle = f_penu_data[FIFO_WIDTH-1]; + // }}} + end + + always @(*) + if (r_flushing && !f_clrfifo) + assert(rdaddr == wraddr); + + always @(*) + if (!f_clrfifo) + begin + if (f_first_in_fifo) + `ASSERT(f_first_data == fifo_data[f_first_addr]); + if (f_next_in_fifo) + `ASSERT(f_next_data == fifo_data[f_next_addr]); + end + + always @(posedge S_AXI_ACLK) + if (f_wrfifo && wraddr == f_first_addr) + f_first_data <= { M_AXI_ARVALID, ar_oreg, ar_op, misaligned_request, adr_lsb }; + + always @(posedge S_AXI_ACLK) + if (f_wrfifo && wraddr == f_next_addr) + f_next_data <= { M_AXI_ARVALID, ar_oreg, ar_op, misaligned_request, adr_lsb }; + + // f_fsmfifo + // {{{ + initial f_fsmfifo = 2'b00; + always @(posedge S_AXI_ACLK) + if (f_clrfifo) + f_fsmfifo <= 2'b00; + else case(f_fsmfifo) + 2'b00: if (f_wrfifo && wraddr == f_first_addr) + f_fsmfifo <= 2'b01; + 2'b01: if (f_rdfifo && rdaddr == f_first_addr) + f_fsmfifo <= 2'b00; + else if (f_wrfifo && wraddr == f_next_addr) + f_fsmfifo <= 2'b10; + 2'b10: if (f_rdfifo && rdaddr == f_first_addr) + f_fsmfifo <= 2'b11; + 2'b11: if (f_rdfifo) + f_fsmfifo <= 2'b00; + endcase + + always @(*) + if (!f_clrfifo) + case(f_fsmfifo) + 2'b00: begin + // {{{ + `ASSERT(!f_first_in_fifo); + end + // }}} + 2'b01: begin + // {{{ + `ASSERT(f_fifo_fill >= 1); + `ASSERT(f_first_in_fifo); + end + // }}} + 2'b10: begin + // {{{ + `ASSERT(f_fifo_fill >= 2); + `ASSERT(f_first_in_fifo); + `ASSERT(f_next_in_fifo); + end + // }}} + 2'b11: begin + // {{{ + `ASSERT(f_fifo_fill >= 1); + `ASSERT(f_next_in_fifo); + end + // }}} + endcase + // }}} + + // { ar_oreg, ar_op, misaligned_request, M_AXI_ARADDR[AXILLSB-1:0] }; + + // cpu_gie checks + // {{{ + always @(*) + if (M_AXI_ARVALID) + `ASSERT(cpu_gie == ar_oreg[4]); + + always @(*) + if (!f_clrfifo && f_fifo_fill != 0) + begin + if (M_AXI_ARVALID || M_AXI_WVALID || M_AXI_AWVALID) + `ASSERT(cpu_gie == ar_oreg[4]); + + if ((!f_first_in_fifo || rdaddr != f_first_addr) + && (!f_next_in_fifo || rdaddr != f_next_addr) + && (f_fifo_fill > 0)) + `ASSUME(cpu_gie == f_return_reg[4]); + + if (f_first_in_fifo) + `ASSERT(cpu_gie == f_first_return_reg[4]); + if (f_next_in_fifo) + `ASSERT(cpu_gie == f_next_return_reg[4]); + end + // }}} + + // cpu_pc checks + // {{{ + always @(*) + if (M_AXI_ARVALID) + `ASSERT(cpu_pc == ((&ar_oreg[3:1]) && (o_err || !flush_request))); + + always @(*) + if (!f_clrfifo && f_fifo_fill != 0) + begin + if ((M_AXI_ARVALID || (rdaddr != f_last_written))) + begin + `ASSERT(f_this_misaligned || !cpu_read_cycle || !(&f_return_reg[3:1])); + end else if (f_fifo_fill > 0 && !M_AXI_ARVALID) + `ASSERT(f_this_misaligned || !cpu_read_cycle || cpu_pc == (&f_return_reg[3:1])); + + if ((!f_first_in_fifo || rdaddr != f_first_addr) + && (!f_next_in_fifo || rdaddr != f_next_addr) + && cpu_read_cycle) + begin + if (M_AXI_ARVALID) // && (&ar_oreg[3:1])) + begin + `ASSUME(!(&f_return_reg[3:1])); + end else if (rdaddr != f_last_written) + begin + `ASSUME(!(&f_return_reg[3:1])); + end else if (!M_AXI_ARVALID && !o_err) + `ASSUME(cpu_pc == (&f_return_reg[3:1])); // Not last written + end + + if (f_first_in_fifo && cpu_read_cycle) + begin + if (!cpu_pc || M_AXI_ARVALID + || (f_last_written != f_first_addr)) + begin + `ASSERT(f_first_misaligned || !cpu_read_cycle || !(&f_first_return_reg[3:1])); + end else if (!M_AXI_ARVALID && !o_err) + `ASSERT(&f_first_return_reg[3:1]); + end + + if (f_next_in_fifo && cpu_read_cycle) + begin + if (!cpu_pc || M_AXI_ARVALID + || (f_last_written != f_next_addr)) + begin + `ASSERT(f_next_misaligned || !(&f_next_return_reg[3:1])); + end else if (!M_AXI_ARVALID && !o_err) + `ASSERT(&f_next_return_reg[3:1]); + end + + if (M_AXI_ARVALID) + `ASSERT(cpu_pc == (&ar_oreg[3:1])); + end + // }}} + + always @(*) + if (cpu_read_cycle && !r_flushing) + assert(o_rdbusy==((rdaddr != wraddr)||(M_AXI_ARVALID))); + + always @(*) + if (cpu_read_cycle) + assert(!M_AXI_AWVALID && !M_AXI_WVALID && faxil_awr_outstanding == 0); + + // Verifying the alignment flags + // {{{ + always @(*) + if (M_AXI_ARVALID && OPT_ALIGNMENT_ERR) + assert(!misaligned_request); + + always @(*) + if (!f_clrfifo && f_fifo_fill != 0) + begin + if (OPT_ALIGNMENT_ERR) + begin + // {{{ + if (f_first_in_fifo) + `ASSERT(!f_first_misaligned); + if (f_next_in_fifo) + `ASSERT(!f_next_misaligned); + if ((!f_first_in_fifo || rdaddr != f_first_addr) + && (!f_next_in_fifo || rdaddr != f_next_addr)) + `ASSUME(!f_this_misaligned); + // }}} + end else begin + // {{{ + if (f_first_in_fifo && f_first_misaligned) + begin + if (f_next_in_fifo) + begin + `ASSERT(!f_next_misaligned); + end else begin + `ASSERT(!misaligned_request); + `ASSERT(M_AXI_ARVALID || M_AXI_WVALID); + end + end + + if (misaligned_response_pending + && (!f_first_in_fifo || rdaddr != f_first_addr)) + begin + if (f_fifo_fill > 1) + begin + `BMC_ASSERT(!f_penu_misaligned); + end else if (f_fifo_fill == 1) + `BMC_ASSERT(!misaligned_request); + end + // }}} + end + end + // }}} + + // Verifying unaligned registers remain the same + // {{{ + always @(*) + if (!f_clrfifo && f_fifo_fill != 0 && !OPT_ALIGNMENT_ERR) + begin + if (f_first_in_fifo && f_first_misaligned) + begin + if (f_next_in_fifo) + begin + `ASSERT(f_first_return_reg == f_next_return_reg); + end else begin + `ASSERT(M_AXI_ARVALID || M_AXI_WVALID); + `ASSERT(!misaligned_request); + `ASSERT(f_first_return_reg == ar_oreg); + end + end + + if (misaligned_response_pending + && (!f_first_in_fifo || rdaddr != f_first_addr)) + begin + if (f_fifo_fill == 1) + begin + `BMC_ASSERT(M_AXI_ARVALID || M_AXI_WVALID); + `BMC_ASSERT(!misaligned_request); + `BMC_ASSERT(f_first_return_reg == ar_oreg); + end else + `BMC_ASSERT(fifo_return_reg == f_penu_return_reg); + end + end + // }}} + + always @(*) + assert(f_fifo_fill <= (1< 0) + begin + if (f_first_in_fifo) + `ASSERT(cpu_read_cycle == f_first_read_cycle); + if (f_next_in_fifo) + `ASSERT(cpu_read_cycle == f_next_read_cycle); + + if (// f_fifo_fill > 0 && // Redundant + (!f_first_in_fifo || rdaddr != f_first_addr) + && (!f_next_in_fifo || rdaddr != f_next_addr)) + begin + `BMC_ASSERT(cpu_read_cycle == f_this_read_cycle); + end + + if (f_fifo_fill > 1 + && (!f_first_in_fifo || f_penu_addr != f_first_addr) + && (!f_next_in_fifo || f_penu_addr != f_next_addr)) + begin + `BMC_ASSERT(cpu_read_cycle == f_penu_read_cycle); + end + + if (cpu_read_cycle) + begin + `ASSERT(!M_AXI_AWVALID && !M_AXI_WVALID); + `ASSERT(faxil_awr_outstanding == 0); + `ASSERT(faxil_wr_outstanding == 0); + end + end + // }}} + + // Verifying the cpu_last_reg + // {{{ + always @(*) + if (M_AXI_WVALID || M_AXI_ARVALID) + begin + assert(cpu_last_reg == ar_oreg); + end else if (!f_clrfifo && f_fifo_fill > 0) + begin + if (f_first_in_fifo && f_first_addr == f_last_written) + assert(f_first_return_reg == cpu_last_reg); + if (f_next_in_fifo && f_next_addr == f_last_written) + assert(f_next_return_reg == cpu_last_reg); + if (rdaddr == f_last_written + && (rdaddr != f_first_addr) + && (rdaddr != f_next_addr)) + `BMC_ASSERT(fifo_return_reg == cpu_last_reg); + end + // }}} + + // Verifying the cpu_addr_reg + // {{{ + always @(posedge S_AXI_ACLK) + if (i_stb) + f_ar_areg <= f_areg; + + always @(*) + if (o_rdbusy) + `ASSERT(f_ar_areg == cpu_addr_reg); + + always @(*) + if (!f_clrfifo && o_rdbusy && f_fifo_fill > 0) + begin + if (f_first_in_fifo && f_first_addr != f_last_written) + assert(f_first_return_reg != cpu_addr_reg + || f_first_misaligned); + if (f_next_in_fifo && f_next_addr != f_last_written) + assert(f_next_return_reg != cpu_addr_reg + || f_next_misaligned); + + // If the base address register exists in the FIFO, then it + // can't be part of any current requests. + if ((f_first_in_fifo && (f_first_return_reg == cpu_addr_reg) && !f_first_misaligned) + ||(f_next_in_fifo && (f_next_return_reg == cpu_addr_reg) && !f_next_misaligned)) + assert(!M_AXI_WVALID && !M_AXI_ARVALID); + + if ((rdaddr != f_last_written || M_AXI_WVALID || M_AXI_ARVALID) + && !misaligned_response_pending + && (cpu_outstanding > (o_valid ? 1:0)) + && (rdaddr != f_first_addr) + && (rdaddr != f_next_addr)) + `BMC_ASSERT(fifo_return_reg != cpu_addr_reg); + end + + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Contract properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + initial f_done = 1'b0; + always @(posedge i_clk) + if (i_cpu_reset || r_flushing) + f_done <= 1'b0; + else + f_done <= (M_AXI_RVALID && !M_AXI_RRESP[1] + || M_AXI_BVALID && !M_AXI_BRESP[1]) && !pending_err + && !misaligned_response_pending; + + + fmem #( + // {{{ + .F_LGDEPTH(LGPIPE+1), .OPT_MAXDEPTH(1< 0) + `ASSERT(r_flushing); + + always @(*) + if (!o_err && cpu_outstanding > 0) + begin + if (faxil_rd_outstanding > 0 || M_AXI_ARVALID || o_rdbusy) + begin + assert(cpu_read_cycle); + end else if (faxil_awr_outstanding > 0 || faxil_wr_outstanding > 0 + || M_AXI_AWVALID || M_AXI_WVALID) + assert(!cpu_read_cycle); + end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Cover properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + reg [LGPIPE:0] cvr_writes, cvr_reads, cvr_valids; + reg cvr_idle; + + always @(*) + begin + cvr_idle = 1; + if (i_cpu_reset || o_err || f_done) + cvr_idle = 1'b0; + if (M_AXI_AWVALID || M_AXI_WVALID || M_AXI_ARVALID) + cvr_idle = 1'b0; + if (faxil_awr_outstanding > 0) + cvr_idle = 1'b0; + if (faxil_wr_outstanding > 0) + cvr_idle = 1'b0; + if (faxil_rd_outstanding > 0) + cvr_idle = 1'b0; + end + + initial cvr_writes = 0; + always @(posedge i_clk) + if (i_cpu_reset || o_err) + cvr_writes <= 0; + else if (M_AXI_BVALID&& !misaligned_response_pending && !(&cvr_writes)) + cvr_writes <= cvr_writes + 1; + + initial cvr_reads = 0; + always @(posedge i_clk) + if (i_cpu_reset || o_err) + cvr_reads <= 0; + else if (M_AXI_RVALID && !misaligned_response_pending && !(&cvr_reads)) + cvr_reads <= cvr_reads + 1; + + initial cvr_valids = 0; + always @(posedge i_clk) + if (i_cpu_reset || o_err) + cvr_valids <= 0; + else if (o_valid) + cvr_valids <= cvr_valids + 1; + + // Cover a write response + always @(posedge i_clk) + cover(M_AXI_BVALID && !M_AXI_BRESP[1]); + always @(posedge i_clk) + cover(M_AXI_BVALID && M_AXI_BRESP[1]); + + always @(posedge i_clk) + cover(M_AXI_RVALID && !M_AXI_RRESP[1]); + always @(posedge i_clk) + cover(M_AXI_RVALID && M_AXI_RRESP[1]); + + + always @(posedge i_clk) + if (cvr_idle) + begin + cover(cvr_writes > 3); + cover(cvr_reads > 3); + cover(cvr_valids > 3); + + cover(cvr_writes > (1< (1< (1< (1< (1< (1< 3); + cover(cvr_unaligned_reads > 3); + + cover(cvr_unaligned_writes > (1< (1<= (f_done ? 1:0) + + faxil_rd_outstanding[F_LGDEPTH-1:1]); + else if (!r_flushing) + `BMC_ASSERT(cpu_outstanding >= (f_done ? 1:0) + + faxil_wr_outstanding[F_LGDEPTH-1:1]); + + if (!r_flushing && !o_err) + assert(f_fifo_fill == beats_outstanding + + ((misaligned_aw_request && !misaligned_request) ? 1:0) + + (M_AXI_AWVALID && !M_AXI_WVALID)); + + if (!r_flushing && !o_err) + `BMC_ASSERT((cpu_outstanding == 0) == (!M_AXI_AWVALID + && !M_AXI_WVALID && !M_AXI_ARVALID + && (beats_outstanding + (f_done ? 1:0) == 0))); + else if (o_err) + `BMC_ASSERT(cpu_outstanding > 0); + + + if (!r_flushing && !o_err && cpu_outstanding == (f_done ? 1:0)) + `BMC_ASSERT(beats_outstanding == 0 + && !M_AXI_AWVALID && !M_AXI_WVALID + && !M_AXI_ARVALID); + end + + // }}} +`endif +// }}} +endmodule diff --git a/rtl/cpu/axiops.v b/rtl/cpu/axiops.v new file mode 100644 index 0000000..0509a9e --- /dev/null +++ b/rtl/cpu/axiops.v @@ -0,0 +1,1023 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: axiops.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: A memory unit to support a CPU based upon AXI4. This is about +// the simplest AXI4 interface I can design for this purpose. +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2020-2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +// +`default_nettype none +// }}} +module axiops #( + // {{{ + parameter C_AXI_ADDR_WIDTH = 32, + parameter C_AXI_DATA_WIDTH = 32, + parameter C_AXI_ID_WIDTH = 1, + parameter [((C_AXI_ID_WIDTH > 0)? C_AXI_ID_WIDTH:1)-1:0] + AXI_ID = 0, + // + // SWAP_ENDIANNESS + // {{{ + // The ZipCPU was designed to be a big endian machine. With + // no other adjustments, this design will make the ZipCPU + // *little* endian, for the simple reason that the AXI bus is + // a little endian bus. However, if SWAP_ENDIANNESS is set, + // the bytes within 32-bit words on the AXI bus will be swapped. + // This will return the CPU to being a big endian CPU on a + // little endian bus. It will also break any design that + // assumes the bus is presented to it in its proper order. + // Simple things like counters or interrupt controllers will + // therefore cease to work with this option unless they also + // swap the endianness of the words they are given. + parameter [0:0] SWAP_ENDIANNESS = 1'b0, + // }}} + // SWAP_WSTRB + // {{{ + // SWAP_WSTRB represents a second attempt to fix the endianness + // issue. It is incompatible with the SWAP_ENDIANNESS option + // above. If SWAP_WSTRB is set, then half words and words will + // be placed on the bus in little endian order, but at big + // endian addresses. Words written to the bus will be written + // in little endian order. Halfwords written to the bus at + // address 2 will be written to address 0, halfwords written to + // address 0 will be written to address 2. Bytes written to the + // but at address 3 will be written to address 0, address 2 + // will be written to address 1, address 1 to address 2, and + // address 3 to address 0. + // + // This may just be a half baked attempt to solve this problem, + // since it will fail if you ever trie to access bytes or + // halfwords at other than their intended widths. + parameter [0:0] SWAP_WSTRB = 1'b0, + // }}} + // OPT_SIGN_EXTEND + // {{{ + // Some CPU's want memory accesses to be sign extended upon + // return. The ZipCPU is not one of those CPU's. However, + // since it's fairly easy to do so, we'll implement this logic + // if ever OPT_SIGN_EXTEND is true so you can see how it would + // be done if necessary. + parameter [0:0] OPT_SIGN_EXTEND = 1'b0, + // }}} + parameter [0:0] OPT_LOCK=1'b1, + // OPT_ALIGNMENT_ERR + // {{{ + // If set, OPT_ALIGNMENT_ERR will generate an alignment error + // on any attempt to write to or read from an unaligned word. + // If not set, unaligned reads (or writes) will be expanded into + // pairs so as to still accomplish the action requested. The + // bus does not guarantee protection, however, that these two + // writes or two reads will proceed uninterrupted. Since + // unaligned writes and unaligned reads are no longer + // guaranteed to be atomic by the AXI bus, it is possible that + // any unaligned operations might yield an incoherent result. + parameter [0:0] OPT_ALIGNMENT_ERR = 1'b0, + // }}} + // OPT_LOWPOWER + // {{{ + // If set, the design will use extra logic to guarantee that any + // unused registers are kept at zero until they are used. This + // will help to guarantee the design (ideally) has fewer + // transitions and therefore uses lower power. + parameter [0:0] OPT_LOWPOWER = 1'b1, + // }}} + parameter [3:0] OPT_QOS = 0, + localparam IW = (C_AXI_ID_WIDTH > 0) ? C_AXI_ID_WIDTH:1, + localparam AW = C_AXI_ADDR_WIDTH, + localparam DW = C_AXI_DATA_WIDTH, + localparam AXILSB = $clog2(C_AXI_DATA_WIDTH/8) + // }}} + ) ( + // {{{ + input wire S_AXI_ACLK, + input wire S_AXI_ARESETN, + input wire i_cpu_reset, + // + // CPU interface + // {{{ + input wire i_stb, + input wire i_lock, + input wire [2:0] i_op, + input wire [AW-1:0] i_addr, + input wire [AW-1:0] i_restart_pc, + input wire [31:0] i_data, + input wire [4:0] i_oreg, + output reg o_busy, + output reg o_rdbusy, + output reg o_valid, + output reg o_err, + output reg [4:0] o_wreg, + output reg [31:0] o_result, + // }}} + // AXI4 bus interface + // + // Writes + // {{{ + output reg M_AXI_AWVALID, + input wire M_AXI_AWREADY, + // verilator coverage_off + output wire [IW-1:0] M_AXI_AWID, + // verilator coverage_on + output reg [AW-1:0] M_AXI_AWADDR, + output wire [7:0] M_AXI_AWLEN, + output wire [2:0] M_AXI_AWSIZE, + output wire [1:0] M_AXI_AWBURST, + output wire M_AXI_AWLOCK, + output wire [3:0] M_AXI_AWCACHE, + // verilator coverage_off + output wire [2:0] M_AXI_AWPROT, + output wire [3:0] M_AXI_AWQOS, + // verilator coverage_on + // + output reg M_AXI_WVALID, + input wire M_AXI_WREADY, + output reg [DW-1:0] M_AXI_WDATA, + output reg [DW/8-1:0] M_AXI_WSTRB, + output wire M_AXI_WLAST, + // + input wire M_AXI_BVALID, + output reg M_AXI_BREADY, + // verilator coverage_off + input wire [IW-1:0] M_AXI_BID, + // verilator coverage_on + input wire [1:0] M_AXI_BRESP, + // }}} + // Reads + // {{{ + output reg M_AXI_ARVALID, + input wire M_AXI_ARREADY, + // verilator coverage_off + output wire [IW-1:0] M_AXI_ARID, + // verilator coverage_on + output reg [AW-1:0] M_AXI_ARADDR, + output wire [7:0] M_AXI_ARLEN, + output wire [2:0] M_AXI_ARSIZE, + output wire [1:0] M_AXI_ARBURST, + output wire M_AXI_ARLOCK, + output wire [3:0] M_AXI_ARCACHE, + // verilator coverage_off + output wire [2:0] M_AXI_ARPROT, + output wire [3:0] M_AXI_ARQOS, + // verilator coverage_on + // + input wire M_AXI_RVALID, + output reg M_AXI_RREADY, + // verilator coverage_off + input wire [IW-1:0] M_AXI_RID, + // verilator coverage_on + input wire [DW-1:0] M_AXI_RDATA, + input wire M_AXI_RLAST, + input wire [1:0] M_AXI_RRESP + // }}} + // }}} + ); + + // Declarations + // {{{ + localparam CPU_DATA_WIDTH = 32; + // Verilator lint_off WIDTH + localparam [2:0] DSZ = $clog2(CPU_DATA_WIDTH/8); + // Verilator lint_on WIDTH + localparam [1:0] AXI_INCR = 2'b01, + OKAY = 2'b00, + EXOKAY = 2'b01; + + wire i_clk = S_AXI_ACLK; + // wire i_reset = !S_AXI_ARESETN; + + reg w_misaligned, w_misalignment_err; + wire misaligned_request, misaligned_aw_request, + misaligned_response_pending, pending_err, misaligned_read; + reg r_flushing; + reg [AW-1:0] r_pc; + reg [AXILSB+1:0] r_op; + reg [DW-1:0] next_wdata; + reg [DW/8-1:0] next_wstrb; + reg [DW-1:0] last_result; + // reg [31:0] endian_swapped_wdata; + // reg [31:0] endian_swapped_result; + reg [2*DW/8-1:0] shifted_wstrb_word, + shifted_wstrb_halfword, + shifted_wstrb_byte; + reg [2*DW/8-1:0] swapped_wstrb_word, + swapped_wstrb_halfword, + swapped_wstrb_byte; + reg [DW-1:0] axi_wdata; + reg [DW/8-1:0] axi_wstrb; + reg axlock; + reg [AXILSB-1:0] swapaddr; + wire [DW-1:0] endian_swapped_rdata; + reg [2*DW-1:0] wide_return; + reg [31:0] pre_result; + + // }}} + + // xVALID, and xREADY + // {{{ + initial M_AXI_AWVALID = 1'b0; + initial M_AXI_WVALID = 1'b0; + initial M_AXI_ARVALID = 1'b0; + initial M_AXI_BREADY = 1'b0; + initial M_AXI_RREADY = 1'b0; + always @(posedge i_clk) + if (!S_AXI_ARESETN) + begin + // {{{ + M_AXI_AWVALID <= 1'b0; + M_AXI_WVALID <= 1'b0; + M_AXI_ARVALID <= 1'b0; + M_AXI_BREADY <= 1'b0; + M_AXI_RREADY <= 1'b0; + // }}} + end else if (M_AXI_BREADY || M_AXI_RREADY) + begin // Something is outstanding + // {{{ + if (M_AXI_AWREADY) + M_AXI_AWVALID <= M_AXI_AWVALID && misaligned_aw_request; + if (M_AXI_WREADY) + M_AXI_WVALID <= M_AXI_WVALID && misaligned_request; + if (M_AXI_ARREADY) + M_AXI_ARVALID <= M_AXI_ARVALID && misaligned_request; + + if ((M_AXI_BVALID || M_AXI_RVALID) && !misaligned_response_pending) + begin + M_AXI_BREADY <= 1'b0; + M_AXI_RREADY <= 1'b0; + end + // }}} + end else begin // New memory operation + // {{{ + // Initiate a request + M_AXI_AWVALID <= i_op[0]; // Write request + M_AXI_WVALID <= i_op[0]; // Write request + M_AXI_ARVALID <= !i_op[0]; // Read request + + // Set BREADY or RREADY to accept the response. These will + // remain ready until the response is returned. + M_AXI_BREADY <= i_op[0]; + M_AXI_RREADY <= !i_op[0]; + + if (i_cpu_reset || o_err || !i_stb || w_misalignment_err) + begin + M_AXI_AWVALID <= 0; + M_AXI_WVALID <= 0; + M_AXI_ARVALID <= 0; + + M_AXI_BREADY <= 0; + M_AXI_RREADY <= 0; + end + // }}} + end + // }}} + + // axlock + // {{{ + initial axlock = 1'b0; + always @(posedge i_clk) + if (!OPT_LOCK || (!S_AXI_ARESETN && OPT_LOWPOWER)) + begin + // {{{ + axlock <= 1'b0; + // }}} + end else if (M_AXI_BREADY || M_AXI_RREADY) + begin // Something is outstanding + // {{{ + if (OPT_LOWPOWER && (M_AXI_BVALID || M_AXI_RVALID)) + axlock <= axlock && i_lock && M_AXI_RVALID; + // }}} + end else begin // New memory operation + // {{{ + // Initiate a request + if (!OPT_LOWPOWER) + axlock <= i_lock; + else begin + if (i_stb) + axlock <= i_lock; + + if (i_cpu_reset || o_err || w_misaligned) + axlock <= 1'b0; + end + // }}} + end + + assign M_AXI_AWLOCK = axlock; + assign M_AXI_ARLOCK = axlock; + // }}} + + // r_flushing + // {{{ + initial r_flushing = 1'b0; + always @(posedge i_clk) + if (!S_AXI_ARESETN) + // If everything is reset, then we don't need to worry about + // or wait for any pending returns--they'll be canceled by the + // global reset. + r_flushing <= 1'b0; + else if (M_AXI_BREADY || M_AXI_RREADY) + begin + if (i_cpu_reset) + // If only the CPU is reset, however, we have a problem. + // The bus hasn't been reset, and so it is still active. + // We can't respond to any new requests from the CPU + // until we flush any transactions that are currently + // active. + r_flushing <= 1'b1; + if (M_AXI_BVALID || M_AXI_RVALID) + // A request just came back, therefore we can clear + // r_flushing + r_flushing <= 1'b0; + if (misaligned_response_pending) + // ... unless we're in the middle of a misaligned + // request. In that case, there will be a second + // return that we still need to wait for. This request, + // though, will clear misaligned_response_pending. + r_flushing <= r_flushing || i_cpu_reset; + end else + // If nothing is active, we don't care about the CPU reset. + // Flushing just stays at zero. + r_flushing <= 1'b0; + // }}} + + // M_AXI_AxADDR + // {{{ + initial M_AXI_AWADDR = 0; + always @(posedge i_clk) + if (!S_AXI_ARESETN && OPT_LOWPOWER) + M_AXI_AWADDR <= 0; + else if (!M_AXI_BREADY && !M_AXI_RREADY) + begin // Initial address + // {{{ + M_AXI_AWADDR <= i_addr; + + if (OPT_LOWPOWER && (i_cpu_reset || o_err || !i_stb || w_misalignment_err)) + M_AXI_AWADDR <= 0; + + if (SWAP_ENDIANNESS || SWAP_WSTRB) + begin + // When adjusting endianness, reads (or writes) are + // always full words. This is important since the + // the bytes at issues may (or may not) be in their + // expected locations + if (OPT_ALIGNMENT_ERR) + M_AXI_AWADDR[AXILSB-1:0] <= 0; + else + M_AXI_AWADDR[1:0] <= 0; + end + // }}} + end else if ((M_AXI_AWVALID && M_AXI_AWREADY) + ||(M_AXI_ARVALID && M_AXI_ARREADY)) + begin // Subsequent addresses + // {{{ + M_AXI_AWADDR[C_AXI_ADDR_WIDTH-1:AXILSB] + <= M_AXI_AWADDR[C_AXI_ADDR_WIDTH-1:AXILSB] + 1; + + M_AXI_AWADDR[AXILSB-1:0] <= 0; + + if (OPT_LOWPOWER && ((M_AXI_RREADY && !misaligned_request) + || (M_AXI_BREADY && !misaligned_aw_request))) + M_AXI_AWADDR <= 0; + // }}} + end + + always @(*) + M_AXI_ARADDR = M_AXI_AWADDR; + // }}} + + // M_AXI_AxSIZE + // {{{ + reg [2:0] axsize; + + initial axsize = DSZ; + always @(posedge i_clk) + if (!S_AXI_ARESETN) + axsize <= DSZ; + else if (!M_AXI_BREADY && !M_AXI_RREADY && (!OPT_LOWPOWER || i_stb)) + begin + casez(i_op[2:1]) + 2'b0?: begin + axsize <= 3'b010; // Word + if ((|i_addr[1:0]) && !w_misaligned) + axsize <= AXILSB[2:0]; + end + 2'b10: begin + axsize <= 3'b001; // Half-word + if (i_addr[0] && !w_misaligned) + axsize <= AXILSB[2:0]; + end + 2'b11: axsize <= 3'b000; // Byte + endcase + + if (SWAP_WSTRB) + axsize <= (OPT_ALIGNMENT_ERR) ? DSZ : AXILSB[2:0]; + end + + assign M_AXI_AWSIZE = axsize; + assign M_AXI_ARSIZE = axsize; + // }}} + + // AxOTHER + // {{{ + localparam [3:0] AXI_NON_CACHABLE_BUFFERABLE = 4'h3; + localparam [3:0] OPT_CACHE = AXI_NON_CACHABLE_BUFFERABLE; + localparam [2:0] AXI_UNPRIVILEGED_NONSECURE_DATA_ACCESS = 3'h0; + localparam [2:0] OPT_PROT=AXI_UNPRIVILEGED_NONSECURE_DATA_ACCESS; + + assign M_AXI_AWID = AXI_ID; + assign M_AXI_AWLEN = 0; + assign M_AXI_AWBURST = AXI_INCR; + assign M_AXI_AWCACHE = M_AXI_AWLOCK ? 0: OPT_CACHE; + assign M_AXI_AWPROT = OPT_PROT; + assign M_AXI_AWQOS = OPT_QOS; + assign M_AXI_WLAST = 1; + + assign M_AXI_ARID = AXI_ID; + assign M_AXI_ARLEN = 0; + assign M_AXI_ARBURST = AXI_INCR; + assign M_AXI_ARCACHE = M_AXI_ARLOCK ? 0: OPT_CACHE; + assign M_AXI_ARPROT = OPT_PROT; + assign M_AXI_ARQOS = OPT_QOS; + // }}} + + // shifted_wstrb_* + // {{{ + generate if (SWAP_WSTRB) + begin : BIG_ENDIAN_WSTRB + always @(*) + shifted_wstrb_word = { 4'b1111, {(2*DW/8-4){1'b0}} } + >> i_addr[AXILSB-1:0]; + + always @(*) + shifted_wstrb_halfword = { 2'b11, {(2*DW/8-2){1'b0}} } + >> i_addr[AXILSB-1:0]; + + always @(*) + shifted_wstrb_byte = { 1'b1, {(2*DW/8-1){1'b0}} } + >> i_addr[AXILSB-1:0]; + end else begin : NORMAL_SHIFTED_WSTRB + always @(*) + shifted_wstrb_word = { {(2*DW/8-4){1'b0}}, + 4'b1111} << i_addr[AXILSB-1:0]; + + always @(*) + shifted_wstrb_halfword = { {(2*DW/8-4){1'b0}}, + 4'b0011} << i_addr[AXILSB-1:0]; + + always @(*) + shifted_wstrb_byte = { {(2*DW/8-4){1'b0}}, + 4'b0001} << i_addr[AXILSB-1:0]; + end endgenerate + // }}} + + // Swapping WSTRB bits + // {{{ + generate if (SWAP_ENDIANNESS) + begin : SWAPPING_ENDIANNESS + // {{{ + genvar gw, gb; + + for(gw=0; gw<2*DW/32; gw=gw+1) + begin : FOREACH_32B_WORD + for(gb=0; gb<32/8; gb=gb+1) + begin : FOREACH_BYTE + + always @(*) + begin + swapped_wstrb_word[gw*4+gb] + = shifted_wstrb_word[gw*4+(3-gb)]; + swapped_wstrb_halfword[gw*4+gb] + = shifted_wstrb_halfword[gw*4+(3-gb)]; + swapped_wstrb_byte[gw*4+gb] + = shifted_wstrb_byte[gw*4+(3-gb)]; + end end end + // }}} + end else begin : KEEP_WSTRB + // {{{ + + always @(*) + swapped_wstrb_word = shifted_wstrb_word; + + always @(*) + swapped_wstrb_halfword = shifted_wstrb_halfword; + + always @(*) + swapped_wstrb_byte = shifted_wstrb_byte; + // }}} + end endgenerate + // }}} + + // wdata, wstrb + // {{{ + always @(*) + swapaddr = i_addr[AXILSB-1:0]; + + initial axi_wdata = 0; + initial axi_wstrb = 0; + initial next_wdata = 0; + initial next_wstrb = 0; + always @(posedge i_clk) + if (OPT_LOWPOWER && !S_AXI_ARESETN) + begin + // {{{ + axi_wdata <= 0; + axi_wstrb <= 0; + + next_wdata <= 0; + next_wstrb <= 0; + + r_op <= 0; + // }}} + end else if (i_stb) + begin + // {{{ + if (SWAP_WSTRB) + begin + casez(i_op[2:1]) + 2'b10: { axi_wdata, next_wdata } + <= { i_data[15:0], {(2*C_AXI_DATA_WIDTH-16){1'b0}} } + >> (8*swapaddr); + 2'b11: { axi_wdata, next_wdata } + <= { i_data[7:0], {(2*C_AXI_DATA_WIDTH-8){1'b0}} } + >> (8*swapaddr); + default: { axi_wdata, next_wdata } + <= { i_data, {(2*C_AXI_DATA_WIDTH-32){1'b0}} } + >> (8*swapaddr); + endcase + end else begin + casez(i_op[2:1]) + 2'b10: { next_wdata, axi_wdata } + <= { {(2*C_AXI_DATA_WIDTH-16){1'b0}}, + i_data[15:0] } << (8*swapaddr); + 2'b11: { next_wdata, axi_wdata } + <= { {(2*C_AXI_DATA_WIDTH-8){1'b0}}, + i_data[7:0] } << (8*swapaddr); + default: { next_wdata, axi_wdata } + <= { {(2*C_AXI_DATA_WIDTH-32){1'b0}}, + i_data } << (8*swapaddr); + endcase + end + + // next_wstrb, axi_wstrb + // {{{ + if (SWAP_WSTRB) + begin + casez(i_op[2:1]) + 2'b0?: { axi_wstrb, next_wstrb } <= swapped_wstrb_word; + 2'b10: { axi_wstrb, next_wstrb } <= swapped_wstrb_halfword; + 2'b11: { axi_wstrb, next_wstrb } <= swapped_wstrb_byte; + endcase + end else begin + casez(i_op[2:1]) + 2'b0?: { next_wstrb, axi_wstrb } <= swapped_wstrb_word; + 2'b10: { next_wstrb, axi_wstrb } <= swapped_wstrb_halfword; + 2'b11: { next_wstrb, axi_wstrb } <= swapped_wstrb_byte; + endcase + end + // }}} + + r_op <= { i_op[2:1] , i_addr[AXILSB-1:0] }; + + // On a read set everything to zero but only if OPT_LOWPOWER + // is set + // {{{ + if (OPT_LOWPOWER && !i_op[0]) + { next_wstrb, next_wdata, axi_wstrb, axi_wdata } <= 0; + + if (w_misalignment_err) + { next_wstrb, next_wdata } <= 0; + if (OPT_LOWPOWER) + begin + if (w_misalignment_err) + { axi_wdata, axi_wstrb, r_op } <= 0; + if (o_err || i_cpu_reset) + { next_wdata, next_wstrb, + axi_wdata, axi_wstrb, r_op } <= 0; + end + // }}} + // }}} + end else if ((misaligned_request || !OPT_LOWPOWER) && M_AXI_WREADY) + begin + // {{{ + axi_wdata <= next_wdata; + axi_wstrb <= next_wstrb; + if (OPT_LOWPOWER) + { next_wdata, next_wstrb } <= 0; + // }}} + end else if (OPT_LOWPOWER && M_AXI_WREADY) + begin + // {{{ + axi_wdata <= 0; + axi_wstrb <= 0; + // }}} + end + // }}} + + // M_AXI_WDATA, M_AXI_WSTRB + // {{{ + generate if (SWAP_ENDIANNESS) + begin : SWAP_WRITE_DATA_STRB + // {{{ + genvar gw, gb; + + for(gw=0; gw= (1<= (1<> (8*r_op[AXILSB-1:0]); + else + wide_return = { {(DW){1'b0}}, endian_swapped_rdata } + >> (8*r_op[AXILSB-1:0]); + end + + if (OPT_LOWPOWER && (!M_AXI_RVALID || M_AXI_RRESP[1])) + wide_return = 0; + end + + always @(*) + begin + if (SWAP_WSTRB) + begin + pre_result = 0; + + casez(r_op[AXILSB +: 2]) + 2'b10: pre_result[15:0] = { + wide_return[(2*DW)-1:(2*DW)-16] }; + 2'b11: pre_result[7:0] = { + wide_return[(2*DW)-1:(2*DW)-8] }; + default: pre_result[31:0] = wide_return[(2*DW-1):(2*DW-32)]; + endcase + + end else + pre_result = wide_return[31:0]; + end + + // }}} + // last_result, o_result + // {{{ + always @(posedge i_clk) + if (OPT_LOWPOWER &&((!M_AXI_RREADY && (!OPT_LOCK || !M_AXI_BREADY)) + || !S_AXI_ARESETN || r_flushing || i_cpu_reset)) + { last_result, o_result } <= 0; + else begin + // {{{ + if (OPT_LOWPOWER) + o_result <= 0; + + if (OPT_LOCK && M_AXI_BVALID && (!OPT_LOWPOWER + || (axlock && M_AXI_BRESP == OKAY))) + begin + o_result <= 0; + o_result[AW-1:0] <= r_pc; + end + + if (M_AXI_RVALID) + begin + // {{{ + if (OPT_LOWPOWER && (M_AXI_RRESP[1] || !misaligned_response_pending)) + last_result <= 0; + else + last_result <= endian_swapped_rdata; + + o_result <= pre_result[31:0]; + + if (OPT_SIGN_EXTEND) + begin + // {{{ + // verilator coverage_off + // Optionally sign extend the return result. + // Not covered, since this violates ZipCPU ISA + casez(r_op[AXILSB +: 2]) + 2'b10: o_result[31:16] <= {(16){pre_result[15]}}; + 2'b11: o_result[31: 8] <= {(24){pre_result[7]}}; + default: begin end + endcase + // verilator coverage_off + // }}} + end else begin + // Fill unused return bits with zeros + casez(r_op[AXILSB +: 2]) + 2'b10: o_result[31:16] <= 0; + 2'b11: o_result[31: 8] <= 0; + default: begin end + endcase + end + + if (OPT_LOWPOWER && (M_AXI_RRESP[1] + || (axlock && !M_AXI_RRESP[0]))) + o_result <= 0; + // }}} + end + + if (OPT_ALIGNMENT_ERR) + last_result <= 0; + + if (OPT_LOWPOWER && (pending_err || misaligned_response_pending)) + o_result <= 0; + // }}} + end + // }}} + + // Make verilator happy + // {{{ + // verilator coverage_off + // verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, M_AXI_BID, M_AXI_RID, M_AXI_RLAST }; + + generate if (SWAP_WSTRB) + begin + wire wide_unused; + + if (SWAP_WSTRB) + begin + assign wide_unused = &{ 1'b0, + wide_return[2*DW-32-1:0] }; + end else begin + assign wide_unused = &{ 1'b0, wide_return[2*DW-1:32] }; + end + end endgenerate + // verilator lint_on UNUSED + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL +// Formal properties for this core are maintained elsewhere +`endif +// }}} +endmodule diff --git a/rtl/cpu/axipipe.v b/rtl/cpu/axipipe.v new file mode 100644 index 0000000..829df63 --- /dev/null +++ b/rtl/cpu/axipipe.v @@ -0,0 +1,1076 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: axipipe.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: A memory unit to support a CPU based upon AXI-lite. Unlike the +// axilops core, this one will permit multiple requests to be +// outstanding at any given time. +// +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2020-2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +// +`default_nettype none +// }}} +module axipipe #( + // {{{ + parameter C_AXI_ADDR_WIDTH = 30, + parameter C_AXI_DATA_WIDTH = 32, + parameter C_AXI_ID_WIDTH = 1, + parameter [((C_AXI_ID_WIDTH>0)? C_AXI_ID_WIDTH:1)-1:0] + AXI_ID = 0, + localparam AW = C_AXI_ADDR_WIDTH, + localparam DW = C_AXI_DATA_WIDTH, + localparam IW =(C_AXI_ID_WIDTH > 0) ? C_AXI_ID_WIDTH : 1, + // + // parameter [0:0] SWAP_ENDIANNESS = 1'b0, + parameter [0:0] SWAP_WSTRB = 1'b0, + parameter [0:0] OPT_SIGN_EXTEND = 1'b0, + // AXI locks are a challenge, and require support from the + // CPU. Specifically, we have to be able to unroll and re-do + // the load instruction on any atomic access failure. For that + // reason, we'll ignore the lock request initially. + parameter [0:0] OPT_LOCK=1'b1, + parameter [0:0] OPT_ALIGNMENT_ERR = 1'b0, + parameter [0:0] OPT_LOWPOWER = 1'b1, + parameter [3:0] OPT_QOS = 0 + // }}} + ) ( + // {{{ + input wire S_AXI_ACLK, + input wire S_AXI_ARESETN, + input wire i_cpu_reset, + // + // CPU interface + // {{{ + input wire i_stb, + input wire i_lock, + input wire [2:0] i_op, + input wire [AW-1:0] i_addr, + input wire [AW-1:0] i_restart_pc, + input wire [31:0] i_data, + input wire [4:0] i_oreg, + output reg o_busy, + output reg o_pipe_stalled, + output reg o_rdbusy, + output reg o_valid, + output reg o_err, + output reg [4:0] o_wreg, + output reg [31:0] o_result, + // }}} + // + // AXI4 bus interface + // {{{ + // Writes + // {{{ + output reg M_AXI_AWVALID, + input wire M_AXI_AWREADY, + // verilator coverage_off + output wire [IW-1:0] M_AXI_AWID, + // verilator coverage_on + output reg [AW-1:0] M_AXI_AWADDR, + // verilator coverage_off + output wire [7:0] M_AXI_AWLEN, // == 0 + // verilator coverage_on + output wire [2:0] M_AXI_AWSIZE, + output wire [1:0] M_AXI_AWBURST, + output wire M_AXI_AWLOCK, + output wire [3:0] M_AXI_AWCACHE, + // verilator coverage_off + output wire [2:0] M_AXI_AWPROT, + output wire [3:0] M_AXI_AWQOS, + // verilator coverage_on + // + output reg M_AXI_WVALID, + input wire M_AXI_WREADY, + output reg [DW-1:0] M_AXI_WDATA, + output reg [DW/8-1:0] M_AXI_WSTRB, + output wire M_AXI_WLAST, + // + input wire M_AXI_BVALID, + // verilator coverage_off + input wire [IW-1:0] M_AXI_BID, + // verilator coverage_on + output wire M_AXI_BREADY, + input wire [1:0] M_AXI_BRESP, + // }}} + // Reads + // {{{ + output reg M_AXI_ARVALID, + input wire M_AXI_ARREADY, + // verilator coverage_off + output wire [IW-1:0] M_AXI_ARID, + // verilator coverage_on + output reg [AW-1:0] M_AXI_ARADDR, + // verilator coverage_off + output wire [7:0] M_AXI_ARLEN, // == 0 + // verilator coverage_on + output wire [2:0] M_AXI_ARSIZE, + output wire [1:0] M_AXI_ARBURST, + output wire M_AXI_ARLOCK, + output wire [3:0] M_AXI_ARCACHE, + // verilator coverage_off + output wire [2:0] M_AXI_ARPROT, + output wire [3:0] M_AXI_ARQOS, + // verilator coverage_on + // + input wire M_AXI_RVALID, + output wire M_AXI_RREADY, + // verilator coverage_off + input wire [IW-1:0] M_AXI_RID, + // verilator coverage_on + input wire [DW-1:0] M_AXI_RDATA, + input wire M_AXI_RLAST, + input wire [1:0] M_AXI_RRESP + // }}} + // }}} + // }}} + ); + + // Declarations + // {{{ + localparam AXILSB = $clog2(C_AXI_DATA_WIDTH/8); + localparam [2:0] DSZ = AXILSB[2:0]; + localparam LGPIPE = 4; + localparam FIFO_WIDTH = AXILSB+1+2+4 + 1; + localparam [1:0] AXI_INCR = 2'b01, + OKAY = 2'b00, + EXOKAY = 2'b01; + // SLVERR + // DECERR + + wire i_clk = S_AXI_ACLK; + + reg w_misaligned; + wire misaligned_request, misaligned_aw_request, pending_err; + reg w_misalignment_err; + reg [C_AXI_DATA_WIDTH-1:0] next_wdata; + reg [C_AXI_DATA_WIDTH/8-1:0] next_wstrb; + + reg [AW-1:0] r_pc; + reg r_lock; + reg [2:0] axi_size; + + reg none_outstanding, bus_abort, + read_abort, write_abort; + reg [LGPIPE:0] beats_outstanding; + reg r_flushing, flush_request, + r_pipe_stalled; + reg [LGPIPE:0] flushcount, new_flushcount; + reg [LGPIPE:0] wraddr, rdaddr; + reg [3:0] ar_oreg; + reg [1:0] ar_op; + reg [AXILSB-1:0] adr_lsb; + reg [FIFO_WIDTH-1:0] fifo_data [0:((1< ((M_AXI_RVALID || M_AXI_BVALID) ? 1:0)) + o_busy <= 1; + else + o_busy <= 0; + // }}} + + // Read busy + // {{{ + // True if the CPU should expect some kind of pending response from a + // read, and so should stall for that purpose. False otherwise. + initial o_rdbusy = 0; + always @(posedge S_AXI_ACLK) + if (i_cpu_reset || r_flushing) + o_rdbusy <= 0; + else if ((i_stb && w_misalignment_err) || bus_abort) + o_rdbusy <= 0; + else if (i_stb && (!i_op[0] || (OPT_LOCK && i_lock))) + o_rdbusy <= 1; + else if (OPT_LOCK) + begin + if (M_AXI_AWVALID || M_AXI_ARVALID || M_AXI_WVALID) + o_rdbusy <= o_rdbusy; + else if (o_rdbusy) + o_rdbusy <= (beats_outstanding > ((M_AXI_RVALID||M_AXI_BVALID) ? 1:0)); + end else if (o_rdbusy && !M_AXI_ARVALID) + o_rdbusy <= (beats_outstanding > (M_AXI_RVALID ? 1:0)); + // }}} + + // o_pipe_stalled, r_pipe_stalled + // {{{ + // True if the CPU should expect some kind of pending response from a + // read, and so should stall for that purpose. False otherwise. + generate if (OPT_ALIGNMENT_ERR) + begin : FULL_PIPE_STALL + // {{{ + // Here, we stall if the FIFO is ever full. In this case, + // any new beat will count as only one item to the FIFO, and + // so we can run all the way to full. + reg [LGPIPE:0] beats_committed; + + always @(*) + beats_committed = beats_outstanding + + ((i_stb && !w_misalignment_err) ? 1:0) + + ((M_AXI_AWVALID || M_AXI_WVALID + || M_AXI_ARVALID) ? 1:0); + + initial r_pipe_stalled = 0; + always @(posedge S_AXI_ACLK) + if (i_cpu_reset) + r_pipe_stalled <= 0; + else if (M_AXI_RVALID || M_AXI_BVALID) + r_pipe_stalled <= 0; + else if (i_stb && i_lock && !w_misalignment_err) + r_pipe_stalled <= 1; + else if (OPT_LOCK && r_lock) + r_pipe_stalled <= (beats_committed > 0); + else + r_pipe_stalled <= (beats_committed >= (1< ((M_AXI_RVALID || M_AXI_BVALID) ? 1:0))) + r_pipe_stalled <= 1; + if (i_stb && (w_misaligned && !w_misalignment_err) && !o_pipe_stalled) + r_pipe_stalled <= 1'b1; + if (misaligned_request && (M_AXI_WVALID && !M_AXI_WREADY)) + r_pipe_stalled <= 1'b1; + if (misaligned_request && (M_AXI_ARVALID && !M_AXI_ARREADY)) + r_pipe_stalled <= 1'b1; + if (misaligned_aw_request && (M_AXI_AWVALID && !M_AXI_AWREADY)) + r_pipe_stalled <= 1'b1; + if (beats_committed >= (1< 1); + end + + casez({M_AXI_AWVALID && (M_AXI_AWREADY && !misaligned_aw_request), + (M_AXI_WVALID && M_AXI_WREADY && !misaligned_request), + (M_AXI_ARVALID && M_AXI_ARREADY && !misaligned_request) }) + 3'b001: flush_request <= 0; + 3'b10?: flush_request <= M_AXI_WVALID; + 3'b01?: flush_request <= M_AXI_AWVALID; + 3'b11?: flush_request <= 0; + default: begin end + endcase + // }}} + end + // }}} + + // Bus addressing + // {{{ + initial M_AXI_AWADDR = 0; + always @(posedge i_clk) + if (i_stb) + begin + M_AXI_AWADDR <= i_addr[AW-1:0]; + if (SWAP_WSTRB) + M_AXI_AWADDR[AXILSB-1:0] <= 0; + end else if (!OPT_ALIGNMENT_ERR + && ((M_AXI_AWVALID && M_AXI_AWREADY) // && misaligned_aw_request + || (M_AXI_ARVALID && M_AXI_ARREADY))) // && misaligned_request)) + begin + M_AXI_AWADDR[AW-1:AXILSB] <= M_AXI_AWADDR[AW-1:AXILSB] + 1; + M_AXI_AWADDR[AXILSB-1:0] <= 0; + end + + always @(*) + M_AXI_ARADDR = M_AXI_AWADDR; + // }}} + + // Is this request misaligned? + // {{{ + always @(*) + casez(i_op[2:1]) + // Full word + 2'b0?: w_misaligned = (i_addr[AXILSB-1:0]+3) >= (1<= (1<> (i_addr[AXILSB-1:0] * 8); + 2'b11: wide_wdata + = { i_data[7:0], {(2*C_AXI_DATA_WIDTH-8){1'b0}} } + >> (i_addr[AXILSB-1:0] * 8); + default: wide_wdata + = ({ i_data, {(2*C_AXI_DATA_WIDTH-32){ 1'b0 }} } + >> (i_addr[AXILSB-1:0] * 8)); + endcase + + casez(i_op[2:1]) + 2'b0?: wide_wstrb + = { 4'b1111, {(2*C_AXI_DATA_WIDTH/8-4){1'b0}} } >> i_addr[AXILSB-1:0]; + 2'b10: wide_wstrb + = { 2'b11, {(2*C_AXI_DATA_WIDTH/8-2){1'b0}} } >> i_addr[AXILSB-1:0]; + 2'b11: wide_wstrb + = { 1'b1, {(2*C_AXI_DATA_WIDTH/8-1){1'b0}} } >> i_addr[AXILSB-1:0]; + endcase + // }}} + end else begin : LITTLE_ENDIAN_DATA + // {{{ + casez(i_op[2:1]) + 2'b10: wide_wdata + = { {(2*C_AXI_DATA_WIDTH-16){1'b0}}, i_data[15:0] } + << (8*i_addr[AXILSB-1:0]); + 2'b11: wide_wdata + = { {(2*C_AXI_DATA_WIDTH-8){1'b0}}, i_data[7:0] } + << (8*i_addr[AXILSB-1:0]); + default: wide_wdata + = { {(2*C_AXI_DATA_WIDTH-32){1'b0}}, i_data } + << (8*i_addr[AXILSB-1:0]); + endcase + + casez(i_op[2:1]) + 2'b0?: wide_wstrb + = { {(2*C_AXI_DATA_WIDTH/8-4){1'b0}}, 4'b1111} << i_addr[AXILSB-1:0]; + 2'b10: wide_wstrb + = { {(2*C_AXI_DATA_WIDTH/8-4){1'b0}}, 4'b0011} << i_addr[AXILSB-1:0]; + 2'b11: wide_wstrb + = { {(2*C_AXI_DATA_WIDTH/8-4){1'b0}}, 4'b0001} << i_addr[AXILSB-1:0]; + endcase + // }}} + end + // }}} + + // WDATA and WSTRB + // {{{ + initial M_AXI_WDATA = 0; + initial M_AXI_WSTRB = 0; + initial next_wdata = 0; + initial next_wstrb = 0; + always @(posedge i_clk) + if (i_stb) + begin + if (SWAP_WSTRB) + begin : BACKWARDS_ORDER_REG + // {{{ + { M_AXI_WDATA, next_wdata } <= wide_wdata; + { M_AXI_WSTRB, next_wstrb } <= wide_wstrb; + // }}} + end else begin + // {{{ + { next_wdata, M_AXI_WDATA } <= wide_wdata; + { next_wstrb, M_AXI_WSTRB } <= wide_wstrb; + // }}} + end + + if (OPT_ALIGNMENT_ERR) + { next_wstrb, next_wdata } <= 0; + + end else if ((OPT_LOWPOWER || !OPT_ALIGNMENT_ERR) && M_AXI_WREADY) + begin + M_AXI_WDATA <= next_wdata; + M_AXI_WSTRB <= next_wstrb; + if (OPT_LOWPOWER) + { next_wdata, next_wstrb } <= 0; + end + // }}} + + generate if (OPT_ALIGNMENT_ERR) + begin + // {{{ + // Generate an error on any misaligned request + assign misaligned_request = 1'b0; + + assign misaligned_aw_request = 1'b0; + assign pending_err = 1'b0; + // }}} + end else begin + // {{{ + reg r_misaligned_request, r_misaligned_aw_request, + r_pending_err; + + // misaligned_request + // {{{ + initial r_misaligned_request = 0; + always @(posedge i_clk) + if (!S_AXI_ARESETN) + r_misaligned_request <= 0; + else if (i_stb && !o_err && !i_cpu_reset && !bus_abort) + r_misaligned_request <= w_misaligned && !w_misalignment_err; + else if ((M_AXI_WVALID && M_AXI_WREADY) + || (M_AXI_ARVALID && M_AXI_ARREADY)) + r_misaligned_request <= 1'b0; + + assign misaligned_request = r_misaligned_request; + // }}} + + // misaligned_aw_request + // {{{ + initial r_misaligned_aw_request = 0; + always @(posedge i_clk) + if (!S_AXI_ARESETN) + r_misaligned_aw_request <= 0; + else if (i_stb && !o_err && !i_cpu_reset && !write_abort) + r_misaligned_aw_request <= w_misaligned && i_op[0] && !w_misalignment_err; + else if (M_AXI_AWREADY) + r_misaligned_aw_request <= 1'b0; + + assign misaligned_aw_request = r_misaligned_aw_request; + // }}} + + // pending_err + // {{{ + initial r_pending_err = 1'b0; + always @(posedge i_clk) + if (i_cpu_reset || i_stb || o_err || r_flushing) + r_pending_err <= 1'b0; + else if ((M_AXI_BVALID && M_AXI_BRESP[1]) + || (M_AXI_RVALID && M_AXI_RRESP[1])) + r_pending_err <= 1'b1; + + assign pending_err = r_pending_err; + // }}} + // }}} + end endgenerate + + // AxOTHER + // {{{ + localparam [3:0] AXI_NON_CACHABLE_BUFFERABLE = 4'h3; + localparam [3:0] OPT_CACHE = AXI_NON_CACHABLE_BUFFERABLE; + localparam [2:0] AXI_UNPRIVILEGED_NONSECURE_DATA_ACCESS = 3'h0; + localparam [2:0] OPT_PROT=AXI_UNPRIVILEGED_NONSECURE_DATA_ACCESS; + + assign M_AXI_AWID = AXI_ID; + assign M_AXI_AWLEN = 0; + assign M_AXI_AWBURST = AXI_INCR; + assign M_AXI_AWCACHE = M_AXI_AWLOCK ? 0:OPT_CACHE; + assign M_AXI_AWPROT = OPT_PROT; + assign M_AXI_AWQOS = OPT_QOS; + assign M_AXI_WLAST = 1; + + assign M_AXI_ARID = AXI_ID; + assign M_AXI_ARLEN = 0; + assign M_AXI_ARBURST = AXI_INCR; + assign M_AXI_ARCACHE = M_AXI_ARLOCK ? 0:OPT_CACHE; + assign M_AXI_ARPROT = OPT_PROT; + assign M_AXI_ARQOS = OPT_QOS; + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Read transaction FIFO + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // wraddr + // {{{ + initial wraddr = 0; + always @(posedge S_AXI_ACLK) + if (bus_abort || flush_request) // bus_abort includes i_cpu_reset + wraddr <= 0; + else if ((M_AXI_ARVALID && M_AXI_ARREADY) || (M_AXI_WVALID && M_AXI_WREADY)) + wraddr <= wraddr + 1; + // }}} + + // rdaddr + // {{{ + initial rdaddr = 0; + always @(posedge S_AXI_ACLK) + if (bus_abort || r_flushing) + rdaddr <= 0; + else if (M_AXI_RVALID||M_AXI_BVALID) + rdaddr <= rdaddr + 1; + // }}} + + // ar_oreg, ar_op, adr_lsb + // {{{ + always @(posedge S_AXI_ACLK) + if (i_stb) + { fifo_gie, ar_oreg, ar_op, adr_lsb } <= { i_oreg, i_op[2:1], i_addr[AXILSB-1:0] }; + else if ((M_AXI_ARVALID && M_AXI_ARREADY)||(M_AXI_WVALID && M_AXI_WREADY)) + adr_lsb <= 0; + // }}} + + // fifo_data + // {{{ + always @(posedge S_AXI_ACLK) + if ((M_AXI_ARVALID && M_AXI_ARREADY) || (M_AXI_WVALID && M_AXI_WREADY)) + fifo_data[wraddr[LGPIPE-1:0]] <= { M_AXI_ARVALID, ar_oreg,ar_op, + misaligned_request, adr_lsb }; + + always @(*) + fifo_read_data = fifo_data[rdaddr[LGPIPE-1:0]]; + + assign { fifo_read_op, fifo_return_reg, fifo_op, + fifo_misaligned, fifo_lsb } = fifo_read_data; + // }}} + + // r_pc + // {{{ + always @(posedge S_AXI_ACLK) + if (!OPT_LOCK) + r_pc <= 0; + else if (i_stb && i_lock && !i_op[0]) + r_pc <= i_restart_pc; + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Read return generation + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // o_valid + // {{{ + initial o_valid = 1'b0; + always @(posedge i_clk) + if (i_cpu_reset || r_flushing) + o_valid <= 1'b0; + else if (OPT_LOCK && r_lock) + o_valid <= (M_AXI_RVALID && M_AXI_RRESP == EXOKAY) + ||(M_AXI_BVALID && M_AXI_BRESP == OKAY); + else if (OPT_ALIGNMENT_ERR && i_stb && w_misaligned) + o_valid <= 1'b0; + else + o_valid <= M_AXI_RVALID && !M_AXI_RRESP[1] // && !pending_err + && !fifo_misaligned; + // }}} + + // o_wreg + // {{{ + always @(posedge i_clk) + begin + o_wreg <= { fifo_gie, fifo_return_reg }; + if (OPT_LOCK && r_lock && M_AXI_BVALID) + o_wreg[3:0] <= 4'hf; + end + // }}} + + // o_result, misdata + // {{{ + // Need to realign any returning data + // wide_return + // {{{ + always @(*) + begin + if (SWAP_WSTRB) + begin + if (fifo_misaligned && !OPT_ALIGNMENT_ERR) + wide_return = { misdata, M_AXI_RDATA } + << (8*fifo_lsb); + else + wide_return = { M_AXI_RDATA, {(DW){1'b0}} } + << (8*fifo_lsb); + + end else begin + if (fifo_misaligned && !OPT_ALIGNMENT_ERR) + wide_return = { M_AXI_RDATA, misdata } >> (8*fifo_lsb); + else + wide_return = { {(C_AXI_DATA_WIDTH){1'b0}}, M_AXI_RDATA } + >> (8*fifo_lsb); + end + + if (OPT_LOWPOWER && (!M_AXI_RVALID || M_AXI_RRESP[1])) + wide_return = 0; + end + + always @(*) + begin + if (SWAP_WSTRB) + begin + + pre_result = 32'h0; + casez(fifo_op) + 2'b10: pre_result[15:0] = { + wide_return[(2*DW)-1:(2*DW)-16] }; + 2'b11: pre_result[7:0] = { + wide_return[(2*DW)-1:(2*DW)-8] }; + default: pre_result[31:0] = wide_return[(2*DW-1):(2*DW-32)]; + endcase + + end else + pre_result = wide_return[31:0]; + end + // }}} + + // misdata + // {{{ + always @(posedge i_clk) + if (OPT_ALIGNMENT_ERR) + misdata <= 0; + else if (M_AXI_RVALID) + begin + if (fifo_misaligned) + misdata <= M_AXI_RDATA; + else + misdata <= 0; + end + // }}} + + // o_result + // {{{ + always @(posedge i_clk) + if (OPT_LOWPOWER && (!S_AXI_ARESETN || r_flushing || i_cpu_reset)) + begin + o_result <= 0; + end else if (OPT_LOCK && M_AXI_BVALID && (!OPT_LOWPOWER || (r_lock && M_AXI_BRESP == OKAY))) + begin + o_result <= 0; + o_result[AW-1:0] <= r_pc; + end else if (!OPT_LOWPOWER || M_AXI_RVALID) + begin + + o_result <= pre_result[31:0]; + + if (OPT_SIGN_EXTEND) + begin + // {{{ + // verilator coverage_off + // Optionally sign extend the return result. + // This would be contrary to the ZipCPU ISA + case(fifo_op) + 2'b10: o_result[31:16] <= {(16){pre_result[15]}}; + 2'b11: o_result[31: 8] <= {(24){pre_result[7]}}; + default: begin end + endcase + // verilator coverage_on + // }}} + end else if (fifo_op[1]) + begin + // {{{ + if (fifo_op[0]) + o_result[31: 8] <= 0; + else + o_result[31:16] <= 0; + // }}} + end + + end + // }}} + // }}} + + // o_err - report bus errors back to the CPU + // {{{ + initial o_err = 1'b0; + always @(posedge i_clk) + if (i_cpu_reset || r_flushing || o_err) + o_err <= 1'b0; + else if (i_stb && w_misalignment_err) + o_err <= 1'b1; + else if (OPT_LOCK && r_lock) + o_err <= (M_AXI_BVALID && M_AXI_BRESP[1]) + ||(M_AXI_RVALID && M_AXI_RRESP != EXOKAY); + else if (M_AXI_BVALID || M_AXI_RVALID) + o_err <= (M_AXI_BVALID && M_AXI_BRESP[1]) + || (M_AXI_RVALID && M_AXI_RRESP[1]); + else + o_err <= 1'b0; + // }}} + + // Return xREADY -- always ready + // {{{ + assign M_AXI_RREADY = 1; + assign M_AXI_BREADY = 1; + // }}} + + // }}} + + // Make verilator happy + // {{{ + // verilator coverage_off + // verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, M_AXI_RRESP[0], M_AXI_BRESP[0], + M_AXI_BID, M_AXI_RID, M_AXI_RLAST, + // i_addr[31:C_AXI_ADDR_WIDTH], + (&i_addr), + // wide_return[2*C_AXI_DATA_WIDTH-1:32], + pending_err, fifo_read_op, + none_outstanding }; + + generate if (SWAP_WSTRB) + begin + wire wide_unused; + + if (SWAP_WSTRB) + begin + assign wide_unused = &{ 1'b0, + wide_return[2*DW-32-1:0] }; + end else begin + assign wide_unused = &{ 1'b0, wide_return[2*DW-1:32] }; + end + end endgenerate + // verilator lint_on UNUSED + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal property section +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL +// Formal properties for this core are maintained elsewhere +`endif +// }}} +endmodule diff --git a/rtl/cpu/busdelay.v b/rtl/cpu/busdelay.v index 4077271..ffee754 100644 --- a/rtl/cpu/busdelay.v +++ b/rtl/cpu/busdelay.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: busdelay.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: Delay any access to the wishbone bus by a single clock. @@ -36,11 +36,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -53,200 +53,226 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module busdelay(i_clk, i_reset, - // The input bus - i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, i_wb_sel, - o_wb_stall, o_wb_ack, o_wb_data, o_wb_err, - // The delayed bus - o_dly_cyc, o_dly_stb, o_dly_we, o_dly_addr,o_dly_data,o_dly_sel, - i_dly_stall, i_dly_ack, i_dly_data, i_dly_err); - parameter AW=32, DW=32; - localparam F_LGDEPTH=4; - parameter [0:0] DELAY_STALL = 1; - input wire i_clk, i_reset; - // Input/master bus - input wire i_wb_cyc, i_wb_stb, i_wb_we; - input wire [(AW-1):0] i_wb_addr; - input wire [(DW-1):0] i_wb_data; - input wire [(DW/8-1):0] i_wb_sel; - output wire o_wb_stall; - output reg o_wb_ack; - output reg [(DW-1):0] o_wb_data; - output reg o_wb_err; - // Delayed bus - output reg o_dly_cyc, o_dly_stb, o_dly_we; - output reg [(AW-1):0] o_dly_addr; - output reg [(DW-1):0] o_dly_data; - output reg [(DW/8-1):0] o_dly_sel; - input wire i_dly_stall; - input wire i_dly_ack; - input wire [(DW-1):0] i_dly_data; - input wire i_dly_err; +// }}} +module busdelay #( + // {{{ + parameter AW=32, DW=32, + parameter [0:0] DELAY_STALL = 1, + parameter [0:0] OPT_LOWPOWER = 0 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Input/master bus + // {{{ + input wire i_wb_cyc, i_wb_stb, i_wb_we, + input wire [(AW-1):0] i_wb_addr, + input wire [(DW-1):0] i_wb_data, + input wire [(DW/8-1):0] i_wb_sel, + output wire o_wb_stall, + output reg o_wb_ack, + output reg [(DW-1):0] o_wb_data, + output reg o_wb_err, + // }}} + // Delayed bus + // {{{ + output reg o_dly_cyc, o_dly_stb, o_dly_we, + output reg [(AW-1):0] o_dly_addr, + output reg [(DW-1):0] o_dly_data, + output reg [(DW/8-1):0] o_dly_sel, + input wire i_dly_stall, + input wire i_dly_ack, + input wire [(DW-1):0] i_dly_data, + input wire i_dly_err + // }}} + // }}} + ); - generate - if (DELAY_STALL != 0) - begin + generate if (DELAY_STALL) + begin : SKIDBUFFER + // {{{ reg r_stb, r_we; reg [(AW-1):0] r_addr; reg [(DW-1):0] r_data; reg [(DW/8-1):0] r_sel; + // o_dly_cyc + // {{{ initial o_dly_cyc = 1'b0; + always @(posedge i_clk) + if (i_reset || !i_wb_cyc) + o_dly_cyc <= 1'b0; + else + o_dly_cyc <= (!o_wb_err)&&((!i_dly_err)||(!o_dly_cyc)); + // }}} + + // o_dly_stb + // {{{ initial o_dly_stb = 1'b0; + always @(posedge i_clk) + if (i_reset || !i_wb_cyc || o_wb_err || (o_dly_cyc && i_dly_err)) + o_dly_stb <= 1'b0; + else if (!o_dly_stb || !i_dly_stall) + o_dly_stb <= i_wb_stb || r_stb; + // }}} + + // r_stb + // {{{ + initial r_stb = 1'b0; + always @(posedge i_clk) + if (i_reset || !i_wb_cyc || o_wb_err || i_dly_err + || !i_dly_stall || !o_dly_stb) + r_stb <= 1'b0; + else if (i_wb_stb && !o_wb_stall) // && (o_dly_stb&&i_dly_stall) + r_stb <= 1'b1; + // }}} + + // r_* + // {{{ + initial { r_we, r_addr, r_data, r_sel } = 0; + always @(posedge i_clk) + if (OPT_LOWPOWER && (i_reset || !i_wb_cyc || i_dly_err + || !o_dly_stb || !i_dly_stall)) + { r_we, r_addr, r_data, r_sel } <= 0; + else if (i_wb_stb && !o_wb_stall) // && (o_dly_stb&&i_dly_stall) + { r_we, r_addr, r_data, r_sel } + <= { i_wb_we, i_wb_addr, i_wb_data, i_wb_sel }; + // }}} + initial o_dly_we = 1'b0; initial o_dly_addr = 0; initial o_dly_data = 0; initial o_dly_sel = 0; - initial r_stb = 1'b0; - initial r_we = 1'b0; - initial r_addr = 0; - initial r_data = 0; - initial r_sel = 0; - initial o_wb_ack = 1'b0; - initial o_wb_err = 1'b0; always @(posedge i_clk) + if (OPT_LOWPOWER && (i_reset || (!i_wb_cyc || o_wb_err || (o_dly_cyc && i_dly_err)))) + { o_dly_we, o_dly_addr, o_dly_data, o_dly_sel } <= 0; + else if (!o_dly_stb || !i_dly_stall) begin - o_dly_cyc <= (i_wb_cyc)&&(!i_reset)&&(!o_wb_err) - &&((!i_dly_err)||(!o_dly_cyc)); - - if ((!i_dly_stall)||(!o_dly_stb)) - begin - r_we <= i_wb_we; - r_addr <= i_wb_addr; - r_data <= i_wb_data; - r_sel <= i_wb_sel; - - if (r_stb) - begin - o_dly_we <= r_we; - o_dly_addr <= r_addr; - o_dly_data <= r_data; - o_dly_sel <= r_sel; - o_dly_stb <= 1'b1; - end else begin - o_dly_we <= i_wb_we; - o_dly_addr <= i_wb_addr; - o_dly_data <= i_wb_data; - o_dly_sel <= i_wb_sel; - o_dly_stb <= i_wb_stb; - end - - r_stb <= 1'b0; - end else if ((!r_stb)&&(!o_wb_stall)) - begin - r_we <= i_wb_we; - r_addr <= i_wb_addr; - r_data <= i_wb_data; - r_sel <= i_wb_sel; - r_stb <= i_wb_stb; - end - - if ((!i_wb_cyc)||((i_dly_err)&&(o_dly_cyc))||(o_wb_err)) - begin - o_dly_stb <= 1'b0; - r_stb <= 1'b0; - end - - if ((i_reset)||(!i_wb_cyc)||(o_wb_err)) - o_wb_ack <= 1'b0; + if (r_stb) + { o_dly_we, o_dly_addr, o_dly_data, o_dly_sel } <= { r_we, r_addr, r_data, r_sel }; + else if (!OPT_LOWPOWER || i_wb_stb) + { o_dly_we, o_dly_addr, o_dly_data, o_dly_sel } <= { i_wb_we, i_wb_addr, i_wb_data, i_wb_sel }; else - o_wb_ack <= (i_dly_ack)&&(o_dly_cyc)&&(!i_dly_err); - o_wb_data <= i_dly_data; + { o_dly_addr, o_dly_data, o_dly_sel } <= 0; - if (!i_wb_cyc) - o_wb_err <= 1'b0; - else - o_wb_err <= (i_dly_err)&&(o_dly_cyc); - - if (i_reset) - begin - r_stb <= 0; - r_we <= 0; - o_dly_stb <= 0; - o_wb_err <= 0; - o_wb_ack <= 0; - end end assign o_wb_stall = r_stb; -`ifdef FORMAL - assign f_wpending = { r_stb, r_we, r_addr, r_data, r_sel }; -`endif - end else begin + // o_wb_ack + // {{{ + initial o_wb_ack = 0; + always @(posedge i_clk) + if (i_reset || !i_wb_cyc || o_wb_err || !o_dly_cyc) + o_wb_ack <= 1'b0; + else + o_wb_ack <= (i_dly_ack); + // }}} + // o_wb_data + // {{{ + initial o_wb_data = 0; + always @(posedge i_clk) + if (OPT_LOWPOWER && (i_reset || !i_wb_cyc || !o_dly_cyc + || o_wb_err || !i_dly_ack)) + o_wb_data <= 0; + else + o_wb_data <= i_dly_data; + // }}} + + // o_wb_err + // {{{ + initial o_wb_err = 1'b0; + always @(posedge i_clk) + if (i_reset || !i_wb_cyc || !o_dly_cyc) + o_wb_err <= 1'b0; + else + o_wb_err <= i_dly_err; + // }}} + // }}} + end else begin + // {{{ initial o_dly_cyc = 1'b0; initial o_dly_stb = 1'b0; initial o_dly_we = 1'b0; initial o_dly_addr = 0; initial o_dly_data = 0; initial o_dly_sel = 0; - initial o_wb_ack = 0; - initial o_wb_err = 0; always @(posedge i_clk) - if (i_reset) - o_dly_cyc <= 1'b0; - else if ((i_dly_err)&&(o_dly_cyc)) - o_dly_cyc <= 1'b0; - else if ((o_wb_err)&&(i_wb_cyc)) - o_dly_cyc <= 1'b0; - else - o_dly_cyc <= i_wb_cyc; + if (i_reset) + o_dly_cyc <= 1'b0; + else if ((i_dly_err)&&(o_dly_cyc)) + o_dly_cyc <= 1'b0; + else if ((o_wb_err)&&(i_wb_cyc)) + o_dly_cyc <= 1'b0; + else + o_dly_cyc <= i_wb_cyc; // Add the i_wb_cyc criteria here, so we can simplify the // o_wb_stall criteria below, which would otherwise *and* // these two. always @(posedge i_clk) - if (i_reset) - o_dly_stb <= 1'b0; - else if ((i_dly_err)&&(o_dly_cyc)) - o_dly_stb <= 1'b0; - else if ((o_wb_err)&&(i_wb_cyc)) - o_dly_stb <= 1'b0; - else if (!i_wb_cyc) - o_dly_stb <= 1'b0; - else if (!o_wb_stall) - o_dly_stb <= (i_wb_stb); + if (i_reset) + o_dly_stb <= 1'b0; + else if ((i_dly_err)&&(o_dly_cyc)) + o_dly_stb <= 1'b0; + else if ((o_wb_err)&&(i_wb_cyc)) + o_dly_stb <= 1'b0; + else if (!i_wb_cyc) + o_dly_stb <= 1'b0; + else if (!o_wb_stall) + o_dly_stb <= (i_wb_stb); always @(posedge i_clk) - if (!o_wb_stall) - o_dly_we <= i_wb_we; - always @(posedge i_clk) - if (!o_wb_stall) - o_dly_addr<= i_wb_addr; - always @(posedge i_clk) - if (!o_wb_stall) - o_dly_data <= i_wb_data; + if (!o_wb_stall) + o_dly_we <= i_wb_we; + + initial o_dly_addr = 0; always @(posedge i_clk) - if (!o_wb_stall) - o_dly_sel <= i_wb_sel; + if (OPT_LOWPOWER && (i_reset || !i_wb_cyc || o_wb_err + || (o_dly_cyc && i_dly_err))) + { o_dly_addr, o_dly_data, o_dly_sel } <= 0; + else if (!o_dly_stb || !i_dly_stall) + begin + { o_dly_addr, o_dly_data, o_dly_sel } + <= { i_wb_addr, i_wb_data, i_wb_sel }; + if (OPT_LOWPOWER && !i_wb_stb) + { o_dly_addr, o_dly_data, o_dly_sel } <= 0; + end + + initial o_wb_ack = 0; always @(posedge i_clk) - if (i_reset) - o_wb_ack <= 1'b0; - else - o_wb_ack <= ((i_dly_ack)&&(!i_dly_err) - &&(o_dly_cyc)&&(i_wb_cyc)) - &&(!o_wb_err); + if (i_reset) + o_wb_ack <= 1'b0; + else + o_wb_ack <= ((i_dly_ack)&&(!i_dly_err) + &&(o_dly_cyc)&&(i_wb_cyc)) + &&(!o_wb_err); + + initial o_wb_err = 0; always @(posedge i_clk) - if (i_reset) - o_wb_err <= 1'b0; - else if (!o_dly_cyc) - o_wb_err <= 1'b0; - else - o_wb_err <= (o_wb_err)||(i_dly_err)&&(i_wb_cyc); + if (i_reset) + o_wb_err <= 1'b0; + else if (!o_dly_cyc) + o_wb_err <= 1'b0; + else + o_wb_err <= (o_wb_err)||(i_dly_err)&&(i_wb_cyc); + + initial o_wb_data = 0; always @(posedge i_clk) + if (OPT_LOWPOWER && (i_reset || !i_wb_cyc || !o_dly_cyc || o_wb_err || !i_dly_ack)) + o_wb_data <= 0; + else o_wb_data <= i_dly_data; // Our only non-delayed line, yet still really delayed. Perhaps @@ -255,15 +281,19 @@ module busdelay(i_clk, i_reset, // assign o_wb_stall=((i_wb_cyc)&&(i_dly_stall)&&(o_dly_stb));//&&o_cyc assign o_wb_stall = (i_dly_stall)&&(o_dly_stb); -`ifdef FORMAL - // f_wpending isn't used if DELAY_STALL is zero, but we'll give - // it a seemingly useful value anyway--if for no other reason - // than to be sure we set it to the right number of bits - assign f_wpending = { i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, i_wb_sel }; -`endif + // }}} end endgenerate - +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // Formal properties for this module are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/cpudefs.v b/rtl/cpu/cpudefs.v deleted file mode 100644 index e9b0f0a..0000000 --- a/rtl/cpu/cpudefs.v +++ /dev/null @@ -1,276 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// -// Filename: cpudefs.v -// -// Project: Zip CPU -- a small, lightweight, RISC CPU soft core -// -// Purpose: Some architectures have some needs, others have other needs. -// Some of my projects need a Zip CPU with pipelining, others -// can't handle the timing required to get the answer from the ALU -// back into the input for the ALU. As each different projects has -// different needs, I can either 1) reconfigure my entire baseline prior -// to building each project, or 2) host a configuration file which contains -// the information regarding each baseline. This file is that -// configuration file. It controls how the CPU (not the system, -// peripherals, or other) is defined and implemented. Several options -// are available within here, making the Zip CPU pipelined or not, -// able to handle a faster clock with more stalls or a slower clock with -// no stalls, etc. -// -// This file encapsulates those control options. -// -// The number of LUTs the Zip CPU uses varies dramatically with the -// options defined in this file. -// -// -// Creator: Dan Gisselquist, Ph.D. -// Gisselquist Technology, LLC -// -//////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// -// This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published -// by the Free Software Foundation, either version 3 of the License, or (at -// your option) any later version. -// -// This program is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program. (It's in the $(ROOT)/doc directory. Run make with no -// target there if the PDF file isn't present.) If not, see -// for a copy. -// -// License: GPL, v3, as defined and found on www.gnu.org, -// http://www.gnu.org/licenses/gpl.html -// -// -//////////////////////////////////////////////////////////////////////////////// -// -// -`ifndef CPUDEFS_H -`define CPUDEFS_H -// -// -// The first couple options control the Zip CPU instruction set, and how -// it handles various instructions within the set: -// -// -// -// OPT_MULTIPLY controls whether or not the multiply is built and included -// in the ALU by default. Set this option and a parameter will be set that -// includes the multiply. (This parameter may still be overridden, as with -// any parameter ...) If the multiply is not included and -// OPT_ILLEGAL_INSTRUCTION is set, then the multiply will create an illegal -// instruction that will then trip the illegal instruction trap. -// -// Either not defining this value, or defining it to zero will disable the -// hardware multiply. A value of '1' will cause the multiply to occurr in one -// clock cycle only--often at the expense of the rest of the CPUs speed. -// A value of 2 will cause the multiply to have a single delay cycle, 3 will -// have two delay cycles, and 4 (or more) will have 3 delay cycles. -// -// -`define OPT_MULTIPLY 3 -// -// -// -// OPT_DIVIDE controls whether or not the divide instruction is built and -// included into the ZipCPU by default. Set this option and a parameter will -// be set that causes the divide unit to be included. (This parameter may -// still be overridden, as with any parameter ...) If the divide is not -// included and OPT_ILLEGAL_INSTRUCTION is set, then the multiply will create -// an illegal instruction exception that will send the CPU into supervisor -// mode. -// -// -`define OPT_DIVIDE -// -// -// -// OPT_IMPLEMENT_FPU will (one day) control whether or not the floating point -// unit (once I have one) is built and included into the ZipCPU by default. -// At that time, if this option is set then a parameter will be set that -// causes the floating point unit to be included. (This parameter may -// still be overridden, as with any parameter ...) If the floating point unit -// is not included and OPT_ILLEGAL_INSTRUCTION is set, then as with the -// multiply and divide any floating point instruction will result in an illegal -// instruction exception that will send the CPU into supervisor mode. -// -// -// `define OPT_IMPLEMENT_FPU -// -// -// -// -// OPT_SINGLE_FETCH controls whether or not the prefetch has a cache, and -// whether or not it can issue one instruction per clock. When set, the -// prefetch has no cache, and only one instruction is fetched at a time. -// This effectively sets the CPU so that only one instruction is ever -// in the pipeline at once, and hence you may think of this as a "kill -// pipeline" option. However, since the pipelined fetch component uses so -// much area on the FPGA, this is an important option to use in trimming down -// used area if necessary. Hence, it needs to be maintained for that purpose. -// Be aware, though, it will drop your performance by a factor between 2x and -// 3x. -// -// We can either pipeline our fetches, or issue one fetch at a time. Pipelined -// fetches are more complicated and therefore use more FPGA resources, while -// single fetches will cause the CPU to stall for about 5 stalls each -// instruction cycle, effectively reducing the instruction count per clock to -// about 0.2. However, the area cost may be worth it. Consider: -// -// Slice LUTs ZipSystem ZipCPU -// Single Fetching 2521 1734 -// Pipelined fetching 2796 2046 -// (These numbers may be dated, but should still be representative ...) -// -// I recommend only defining this if you "need" to, if area is tight and -// speed isn't as important. Otherwise, just leave this undefined. -// -// `define OPT_SINGLE_FETCH -// -// -// OPT_DOUBLE_FETCH is an alternative to OPT_SINGLE_FETCH. It is designed to -// increase performance primarily when using an instruction memory which has -// one cost for a random access, and a second (lower) cost for sequential -// access. The driving example behind this implementation was flash memory -// with 34 clocks for an initial access and 16 clocks for any subsequent access, -// but SDRAM memory with 27 clocks for an initial access and 1 clock for a -// subsequent access is also a good example. Even block RAM might be a good -// example, if there were any bus delays in getting to the RAM device. Using -// OPT_DOUBLE_FETCH also increases the pipeline speed, as it allows CIS -// instructions and therefore partial pipelining. (No work is done to resolve -// pipeline conflicts past the decode stage, as is the case with full pipeline -// mode. -// -// Do not define OPT_DOUBLE_FETCH if you wish to fully pipeline the CPU. Do -// not define both OPT_DOUBLE_FETCH and OPT_SINGLE_FETCH (the ifndef below -// should prevent that). -// -// -`ifndef OPT_SINGLE_FETCH -// `define OPT_DOUBLE_FETCH -`endif -// -// -// -// The ZipCPU ISA defines an optional compressed instruction set (CIS) -// complement. This compressed instruction format allows two instructions to -// be packed into the same instruction word. Some instructions can be so -// compressed, although not all. Compressed instructions take the same time to -// complete--they are just compressed within memory to spare troubles with the -// prefetch. Set OPT_CIS to include these compressed instructions as part of -// the instruction set. -// -`define OPT_CIS // COST: about 87 LUTs -// -// -// -// -// OPT_EARLY_BRANCHING is an attempt to execute a BRA statement as early -// as possible, to avoid as many pipeline stalls on a branch as possible. -// With the OPT_TRADITIONAL_PFCACHE, BRA instructions cost only a single -// extra stall cycle, while LJMP instructions cost two (assuming the target is -// in the cache). Indeed, the result is that a BRA instruction can be used as -// the compiler's branch prediction optimizer: BRA's barely stall, while -// conditional branches will always suffer about 4 stall cycles or so. -// -// I recommend setting this flag, so as to turn early branching on---if you -// have the LUTs available to afford it. -// -`define OPT_EARLY_BRANCHING -// -// -// -// -// The next several options are pipeline optimization options. They make no -// sense in a single instruction fetch mode, hence we #ifndef them so they -// are only defined if we are in a full pipelined mode (i.e. OPT_SINGLE_FETCH -// is not defined). -// -`ifndef OPT_SINGLE_FETCH -`ifndef OPT_DOUBLE_FETCH -// -// -// -// OPT_PIPELINED is the natural result and opposite of using the single -// instruction fetch unit. If you are not using that unit, the ZipCPU will -// be pipelined. The option is defined here more for readability than -// anything else, since OPT_PIPELINED makes more sense than OPT_SINGLE_FETCH, -// well ... that and it does a better job of explaining what is going on. -// -// In other words, leave this define alone--lest you break the ZipCPU. -// -`define OPT_PIPELINED -// -// -// -// OPT_TRADITIONAL_PFCACHE allows you to switch between one of two prefetch -// caches. If enabled, a more traditional cache is implemented. This more -// traditional cache (currently) uses many more LUTs, but it also reduces -// the stall count tremendously over the alternative hacked pipeline cache. -// (The traditional pfcache is also pipelined, whereas the pipeline cache -// implements a windowed approach to caching.) -// -// If you have the fabric to support this option, I recommend including it. -// -`define OPT_TRADITIONAL_PFCACHE -// -// -// -// -// OPT_DCACHE enables a CPU data cache for (hopefully) better performance -// in terms of speed. It requires telling the CPU which parts of memory -// can be cachable in terms of three separate address regions: one for the -// SDRAM, one for the flash, and another for the block RAM. -// -`define OPT_DCACHE -// -// -// -// OPT_PIPELINED_BUS_ACCESS controls whether or not LOD/STO instructions -// can take advantaged of pipelined bus instructions. To be eligible, the -// operations must be identical (cannot pipeline loads and stores, just loads -// only or stores only), and the addresses must either be identical or one up -// from the previous address. Further, the load/store string must all have -// the same conditional. This approach gains the must use, in my humble -// opinion, when saving registers to or restoring registers from the stack -// at the beginning/end of a procedure, or when doing a context swap. -// -// I recommend setting this flag, for performance reasons, especially if your -// wishbone bus can handle pipelined bus accesses. -// -`define OPT_PIPELINED_BUS_ACCESS -// -// -// -// -// -`endif // OPT_DOUBLE_FETCH -`endif // OPT_SINGLE_FETCH -// -// -// [EXPERIMENTAL--and not (yet) finished] -// OPT_MMU determines whether or not an MMU will be included in the ZipSystem -// containing the ZipCPU. When set, the ZipCPU will route all memory accesses -// through the MMU as an address translator, creating a form of Virtual memory. -// -// `define OPT_MMU -// -// Now let's talk about peripherals for a moment. These next two defines -// control whether the DMA controller is included in the Zip System, and -// whether or not the 8 accounting timers are also included. Set these to -// include the respective peripherals, comment them out not to. -// -`define INCLUDE_DMA_CONTROLLER -`define INCLUDE_ACCOUNTING_COUNTERS -// -// -`define DEBUG_SCOPE -// -`endif // CPUDEFS_H diff --git a/rtl/cpu/cpuops.v b/rtl/cpu/cpuops.v index b5ef639..faa8ab9 100644 --- a/rtl/cpu/cpuops.v +++ b/rtl/cpu/cpuops.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: cpuops.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: This is the ZipCPU ALU function. It handles all of the @@ -12,11 +12,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -29,35 +29,53 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -// -`include "cpudefs.v" -// -module cpuops(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_c, o_f, o_valid, - o_busy); - parameter IMPLEMENT_MPY = `OPT_MULTIPLY; - parameter [0:0] OPT_SHIFTS = 1'b1; - input wire i_clk, i_reset, i_stb; - input wire [3:0] i_op; - input wire [31:0] i_a, i_b; - output reg [31:0] o_c; - output wire [3:0] o_f; - output reg o_valid; - output wire o_busy; +// }}} +module cpuops #( + // {{{ + parameter OPT_MPY = 0, + parameter [0:0] OPT_SHIFTS = 1'b1, + parameter [0:0] OPT_LOWPOWER = 1'b1 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, i_stb, + input wire [3:0] i_op, + input wire [31:0] i_a, i_b, + output reg [31:0] o_c, + output wire [3:0] o_f, + output reg o_valid, + output wire o_busy + // }}} + ); + + // Declarations + // {{{ + wire [31:0] w_brev_result; + wire z, n, v, vx; + reg c, pre_sign, set_ovfl, keep_sgn_on_ovfl; + + wire [32:0] w_lsr_result, w_asr_result, w_lsl_result; + + wire [63:0] mpy_result; // Where we dump the multiply result + wire mpyhi; // Return the high half of the multiply + wire mpybusy; // The multiply is busy if true + wire mpydone; // True if we'll be valid on the next clock; + wire this_is_a_multiply_op; + reg r_busy; genvar k; + // }}} // Shift register pre-logic - wire [32:0] w_lsr_result, w_asr_result, w_lsl_result; + // {{{ generate if (OPT_SHIFTS) begin : IMPLEMENT_SHIFTS wire signed [32:0] w_pre_asr_input, w_pre_asr_shifted; @@ -79,19 +97,20 @@ module cpuops(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_c, o_f, o_valid, assign w_lsl_result = { i_a[31:0], 1'b0 }; end endgenerate + // }}} // // Bit reversal pre-logic - wire [31:0] w_brev_result; + // {{{ generate for(k=0; k<32; k=k+1) begin : bit_reversal_cpuop assign w_brev_result[k] = i_b[31-k]; end endgenerate + // }}} - // Prelogic for our flags registers - wire z, n, v; - reg c, pre_sign, set_ovfl, keep_sgn_on_ovfl; + // Prelogic for our flags registers : set_ovfl and keep_sgn_on_ovfl + // {{{ always @(posedge i_clk) if (i_stb) // 1 LUT set_ovfl<=(((i_op==4'h0)&&(i_a[31] != i_b[31]))//SUB&CMP @@ -104,11 +123,15 @@ module cpuops(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_c, o_f, o_valid, keep_sgn_on_ovfl<= (((i_op==4'h0)&&(i_a[31] != i_b[31]))//SUB&CMP ||((i_op==4'h2)&&(i_a[31] == i_b[31]))); // ADD + // }}} - wire [63:0] mpy_result; // Where we dump the multiply result - wire mpyhi; // Return the high half of the multiply - wire mpybusy; // The multiply is busy if true - wire mpydone; // True if we'll be valid on the next clock; + //////////////////////////////////////////////////////////////////////// + // + // Multiply handling + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // // A 4-way multiplexer can be done in one 6-LUT. // A 16-way multiplexer can therefore be done in 4x 6-LUT's with @@ -116,23 +139,31 @@ module cpuops(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_c, o_f, o_valid, // Given that we wish to apply this multiplexer approach to 33-bits, // this will cost a minimum of 132 6-LUTs. - wire this_is_a_multiply_op; assign this_is_a_multiply_op = (i_stb)&&((i_op[3:1]==3'h5)||(i_op[3:0]==4'hc)); - // - // Pull in the multiply logic from elsewhere - // `ifdef FORMAL `define MPYOP abs_mpy `else `define MPYOP mpyop `endif - `MPYOP #(.IMPLEMENT_MPY(IMPLEMENT_MPY)) thempy(i_clk, i_reset, this_is_a_multiply_op, i_op[1:0], - i_a, i_b, mpydone, mpybusy, mpy_result, mpyhi); - + `MPYOP #( + // {{{ + .OPT_MPY(OPT_MPY), + .OPT_LOWPOWER(OPT_LOWPOWER) + // }}} + ) thempy( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + .i_stb(this_is_a_multiply_op), .i_op(i_op[1:0]), + .i_a(i_a), .i_b(i_b), .o_valid(mpydone), + .o_busy(mpybusy), .o_result(mpy_result), .o_hi(mpyhi) + // }}} + ); + // }}} + //////////////////////////////////////////////////////////////////////// // // The master ALU case statement - // + // {{{ always @(posedge i_clk) if (i_stb) begin @@ -154,40 +185,57 @@ module cpuops(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_c, o_f, o_valid, 4'b1100: o_c <= mpy_result[31:0]; // MPY default: o_c <= i_b; // MOV, LDI endcase - end else // if (mpydone) + end else if (!OPT_LOWPOWER || mpydone) // set the output based upon the multiply result o_c <= (mpyhi)?mpy_result[63:32]:mpy_result[31:0]; + // }}} - reg r_busy; + // o_busy, r_busy + // {{{ initial r_busy = 1'b0; always @(posedge i_clk) if (i_reset) r_busy <= 1'b0; - else if (IMPLEMENT_MPY > 1) + else if (OPT_MPY > 1) r_busy <= ((i_stb)&&(this_is_a_multiply_op))||mpybusy; else r_busy <= 1'b0; - assign o_busy = (r_busy); // ||((IMPLEMENT_MPY>1)&&(this_is_a_multiply_op)); - + assign o_busy = (r_busy); // ||((OPT_MPY>1)&&(this_is_a_multiply_op)); + // }}} + // Flags assignment and determination + // {{{ assign z = (o_c == 32'h0000); assign n = (o_c[31]); assign v = (set_ovfl)&&(pre_sign != o_c[31]); - wire vx = (keep_sgn_on_ovfl)&&(pre_sign != o_c[31]); + assign vx = (keep_sgn_on_ovfl)&&(pre_sign != o_c[31]); assign o_f = { v, n^vx, c, z }; + // }}} + // o_valid + // {{{ initial o_valid = 1'b0; always @(posedge i_clk) if (i_reset) o_valid <= 1'b0; - else if (IMPLEMENT_MPY <= 1) + else if (OPT_MPY <= 1) o_valid <= (i_stb); else o_valid <=((i_stb)&&(!this_is_a_multiply_op))||(mpydone); - + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // Formal properties for this module are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/dblfetch.v b/rtl/cpu/dblfetch.v index 4979024..6c35b14 100644 --- a/rtl/cpu/dblfetch.v +++ b/rtl/cpu/dblfetch.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: dblfetch.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: This is one step beyond the simplest instruction fetch, @@ -22,11 +22,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -39,89 +39,117 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module dblfetch(i_clk, i_reset, i_new_pc, i_clear_cache, - i_stall_n, i_pc, o_insn, o_pc, o_valid, - o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, - i_wb_stall, i_wb_ack, i_wb_err, i_wb_data, - o_illegal); - parameter ADDRESS_WIDTH=30; - localparam AW=ADDRESS_WIDTH, DW = 32; - input wire i_clk, i_reset, i_new_pc, i_clear_cache, - i_stall_n; - input wire [(AW+1):0] i_pc; - output reg [(DW-1):0] o_insn; - output reg [(AW+1):0] o_pc; - output reg o_valid; - // Wishbone outputs - output reg o_wb_cyc, o_wb_stb; - output wire o_wb_we; - output reg [(AW-1):0] o_wb_addr; - output wire [(DW-1):0] o_wb_data; - // And return inputs - input wire i_wb_stall, i_wb_ack, i_wb_err; - input wire [(DW-1):0] i_wb_data; - // And ... the result if we got an error - output reg o_illegal; - - assign o_wb_we = 1'b0; - assign o_wb_data = 32'h0000; +// }}} +module dblfetch #( + // {{{ + parameter ADDRESS_WIDTH=30, // Byte addr + parameter INSN_WIDTH=32, + parameter DATA_WIDTH = INSN_WIDTH, + localparam AW=ADDRESS_WIDTH, + DW=DATA_WIDTH, + parameter [0:0] OPT_LITTLE_ENDIAN = 1'b1 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // CPU signals--from the CPU + input wire i_new_pc, i_clear_cache,i_ready, + input wire [AW-1:0] i_pc, + // ... and in return + output reg o_valid, + output reg o_illegal, + output reg [INSN_WIDTH-1:0] o_insn, + output reg [AW-1:0] o_pc, + // Wishbone outputs + output reg o_wb_cyc, o_wb_stb, + // verilator coverage_off + output wire o_wb_we, + // verilator coverage_on + output reg [AW-$clog2(DW/8)-1:0] o_wb_addr, + // verilator coverage_off + output wire [DW-1:0] o_wb_data, + // verilator coverage_on + // And return inputs + input wire i_wb_stall, i_wb_ack, i_wb_err, + input wire [DW-1:0] i_wb_data + // }}} + ); - reg last_stb, invalid_bus_cycle; + // Local declarations + // {{{ + wire last_stb; + reg invalid_bus_cycle; reg [(DW-1):0] cache_word; reg cache_valid; reg [1:0] inflight; reg cache_illegal; + wire r_valid; + wire [DATA_WIDTH-1:0] r_insn, i_wb_shifted; + // }}} + + assign o_wb_we = 1'b0; + assign o_wb_data = {(DATA_WIDTH){1'b0}}; + + // o_wb_cyc, o_wb_stb + // {{{ initial o_wb_cyc = 1'b0; initial o_wb_stb = 1'b0; always @(posedge i_clk) - if ((i_reset)||((o_wb_cyc)&&(i_wb_err))) + if (i_reset || i_clear_cache || (o_wb_cyc && i_wb_err)) + begin : RESET_ABORT + // {{{ + o_wb_cyc <= 1'b0; + o_wb_stb <= 1'b0; + // }}} + end else if (o_wb_cyc) + begin : END_CYCLE + // {{{ + if (!o_wb_stb || !i_wb_stall) + o_wb_stb <= (!last_stb); + + // Relase the bus on the second ack + if ((!o_wb_stb || !i_wb_stall) && last_stb + && inflight + (o_wb_stb ? 1:0) == (i_wb_ack ? 1:0)) begin o_wb_cyc <= 1'b0; o_wb_stb <= 1'b0; - end else if (o_wb_cyc) - begin - if ((!o_wb_stb)||(!i_wb_stall)) - o_wb_stb <= (!last_stb); - - // Relase the bus on the second ack - if (((i_wb_ack)&&(!o_wb_stb)&&(inflight<=1)) - ||((!o_wb_stb)&&(inflight == 0)) - // Or any new transaction request - ||((i_new_pc)||(i_clear_cache))) - begin - o_wb_cyc <= 1'b0; - o_wb_stb <= 1'b0; - end - - end else if ((i_new_pc)||(invalid_bus_cycle) - ||((o_valid)&&(i_stall_n)&&(!o_illegal))) - begin - // Initiate a bus cycle if ... the last bus cycle was - // aborted (bus error or new_pc), we've been given a - // new PC to go get, or we just exhausted our one - // instruction cache - o_wb_cyc <= 1'b1; - o_wb_stb <= 1'b1; end + if (i_new_pc) // || i_clear_cache) + { o_wb_cyc, o_wb_stb } <= 2'b0; + // }}} + end else if ((i_new_pc || invalid_bus_cycle) + ||(o_valid && i_ready && !r_valid && !cache_illegal)) + begin : START_CYCLE + // {{{ + // Initiate a bus cycle if ... the last bus cycle was + // aborted (bus error or new_pc), we've been given a + // new PC to go get, or we just exhausted our one + // instruction cache + o_wb_cyc <= 1'b1; + o_wb_stb <= 1'b1; + // }}} + end + // }}} + + // inflight + // {{{ initial inflight = 2'b00; always @(posedge i_clk) if (!o_wb_cyc) inflight <= 2'b00; else begin - case({ ((o_wb_stb)&&(!i_wb_stall)), i_wb_ack }) + case({ (o_wb_stb && !i_wb_stall), i_wb_ack }) 2'b01: inflight <= inflight - 1'b1; 2'b10: inflight <= inflight + 1'b1; // If neither ack nor request, then no change. Likewise @@ -130,112 +158,295 @@ module dblfetch(i_clk, i_reset, i_new_pc, i_clear_cache, default: begin end endcase end + // }}} - always @(*) - last_stb = (inflight != 2'b00)||((o_valid)&&(!i_stall_n)); + // last_stb + // {{{ + // assign last_stb = (inflight != 2'b00)||(o_valid&& (!i_ready||r_valid)); + assign last_stb = (!o_wb_stb||!i_wb_stall)&&(inflight + + (o_wb_stb ? 1:0) + + (o_valid&&(!i_ready || r_valid)) >= 2'b10); + // }}} + // invalid_bus_cycle + // {{{ initial invalid_bus_cycle = 1'b0; always @(posedge i_clk) - if ((o_wb_cyc)&&(i_new_pc)) + if (i_reset) + invalid_bus_cycle <= 1'b0; + else if (o_wb_cyc && i_new_pc) invalid_bus_cycle <= 1'b1; else if (!o_wb_cyc) invalid_bus_cycle <= 1'b0; + // }}} - initial o_wb_addr = {(AW){1'b1}}; + // o_wb_addr + // {{{ + initial o_wb_addr = {(AW-$clog2(DATA_WIDTH/8)){1'b1}}; always @(posedge i_clk) if (i_new_pc) - o_wb_addr <= i_pc[AW+1:2]; - else if ((o_wb_stb)&&(!i_wb_stall)) + o_wb_addr <= i_pc[AW-1:$clog2(DATA_WIDTH/8)]; + // else if (i_clear_cache) + // o_wb_addr <= o_pc[AW-1:$clog2(DATA_WIDTH/8)]; + else if (o_wb_stb && !i_wb_stall) o_wb_addr <= o_wb_addr + 1'b1; + // }}} //////////////////////////////////////////////////////////////////////// // // Now for the immediate output word to the CPU - // + // {{{ //////////////////////////////////////////////////////////////////////// + // + // + + // This only applies when the bus size doesn't match the instruction + // word size. Here, we only support bus sizes greater than the + // instruction word size. + + generate if (DATA_WIDTH > INSN_WIDTH) + begin : GEN_SUBSHIFT + // {{{ + localparam NSHIFT = $clog2(DATA_WIDTH/INSN_WIDTH); + + reg rg_valid; + reg [DATA_WIDTH-1:0] rg_insn; + reg [NSHIFT:0] r_count; + reg [NSHIFT-1:0] r_shift; + + // rg_valid + // {{{ + always @(posedge i_clk) + if (i_reset || i_new_pc) // || i_clear_cache) + rg_valid <= 1'b0; + else if (r_valid) + rg_valid <= !i_ready || (r_count > 1); + else if (!o_valid || i_ready) + begin + rg_valid <= 1'b0; + if (cache_valid) + rg_valid <= 1'b1; + if (o_wb_cyc && i_wb_ack && !(&r_shift)) + rg_valid <= 1'b1; + end + // }}} + + // rg_insn + // {{{ + always @(posedge i_clk) + if (!o_valid || i_ready) + begin + if (cache_valid && !r_valid) + begin + if (OPT_LITTLE_ENDIAN) + rg_insn <= cache_word >> INSN_WIDTH; + else + rg_insn <= cache_word << INSN_WIDTH; + end else if (i_wb_ack && !r_valid) + begin + rg_insn <= i_wb_data; + if (OPT_LITTLE_ENDIAN) + rg_insn <= i_wb_shifted >> INSN_WIDTH; + else + rg_insn <= i_wb_shifted << INSN_WIDTH; + end else begin + if (OPT_LITTLE_ENDIAN) + rg_insn <= rg_insn >> INSN_WIDTH; + else + rg_insn <= rg_insn << INSN_WIDTH; + end + end + // }}} + + // r_count + // {{{ + always @(posedge i_clk) + if (i_reset || i_new_pc) // || i_clear_cache) + r_count <= 0; + else if (o_valid && i_ready && r_valid) + begin + r_count <= r_count - 1; + end else if (!o_valid || (i_ready && !r_valid)) + begin + if (cache_valid) + r_count <= { 1'b0, {(NSHIFT){1'b1}} }; + else if (o_wb_cyc && i_wb_ack) + r_count <= { 1'b0, ~r_shift }; + end + // }}} + + // r_shift + // {{{ + always @(posedge i_clk) + if (i_new_pc) + r_shift <= i_pc[$clog2(DW/8)-1:$clog2(INSN_WIDTH/8)]; + // else if (i_clear_cache) + // r_shift <= o_pc[$clog2(DW/8)-1:$clog2(INSN_WIDTH/8)]; + else if (o_wb_cyc && (i_wb_ack || i_wb_err)) + r_shift <= 0; + // }}} + + assign r_valid = rg_valid; + assign r_insn = rg_insn; + if (OPT_LITTLE_ENDIAN) + begin + assign i_wb_shifted = i_wb_data >> (r_shift * INSN_WIDTH); + end else begin + assign i_wb_shifted = i_wb_data << (r_shift * INSN_WIDTH); + end + // Keep Verilator happy + // {{{ + // Verilator lint_off UNUSED + wire unused_shift; + assign unused_shift = &{ 1'b0, + r_insn[DATA_WIDTH-1:INSN_WIDTH], + i_wb_shifted[DATA_WIDTH-1:INSN_WIDTH] }; + // Verilator lint_on UNUSED + // }}} +`ifdef FORMAL + assign f_bus_word = rg_insn << ((r_count-1)* INSN_WIDTH); + always @(*) + if (!i_reset && r_valid) + begin + assert(i_clear_cache || i_new_pc || o_valid); + assert(r_shift == 0); + assert((r_count + o_pc[$clog2(DW/8)-1:$clog2(INSN_WIDTH/8)]) + == ((1< OPT_FIFO_DEPTH)) - ? LS : OPT_FIFO_DEPTH); - localparam LGAUX = 3; // log_2 of the maximum number of piped data - localparam DW = 32; // Bus data width - localparam DP = OPT_FIFO_DEPTH; - // - localparam [1:0] DC_IDLE = 2'b00; // Bus is idle - localparam [1:0] DC_WRITE = 2'b01; // Write - localparam [1:0] DC_READS = 2'b10; // Read a single value(!cachd) - localparam [1:0] DC_READC = 2'b11; // Read a whole cache line - // - input wire i_clk, i_reset, i_clear; - // Interface from the CPU - input wire i_pipe_stb, i_lock; - input wire [2:0] i_op; - input wire [(DW-1):0] i_addr; - input wire [(DW-1):0] i_data; - input wire [(NAUX-1):0] i_oreg; // Aux data, such as reg to write to - // Outputs, going back to the CPU - output reg o_busy; - output reg o_pipe_stalled; - output reg o_valid, o_err; - output reg [(NAUX-1):0] o_wreg; - output reg [(DW-1):0] o_data; - // Wishbone bus master outputs - output wire o_wb_cyc_gbl, o_wb_cyc_lcl; - output reg o_wb_stb_gbl, o_wb_stb_lcl; - output reg o_wb_we; - output reg [(AW-1):0] o_wb_addr; - output reg [(DW-1):0] o_wb_data; - output wire [(DW/8-1):0] o_wb_sel; - // Wishbone bus slave response inputs - input wire i_wb_stall, i_wb_ack, i_wb_err; - input wire [(DW-1):0] i_wb_data; + + // Declarations + // {{{ + localparam FIF_WIDTH = (NAUX-1)+2+WBLSB; + integer ik; // // output reg [31:0] o_debug; - reg cyc, stb, last_ack, end_of_line, last_line_stb; reg r_wb_cyc_gbl, r_wb_cyc_lcl; // npending is the number of pending non-cached operations, counted @@ -139,12 +155,11 @@ module dcache(i_clk, i_reset, i_clear, i_pipe_stb, i_lock, reg [((1<> (i_addr[1:0]); + else + full_sel = { pre_sel, {(BUS_WIDTH/8-4){1'b0}} } + >> (i_addr[WBLSB-1:0]); + + initial r_wb_sel = -1; + always @(posedge i_clk) + if (i_reset) + r_wb_sel <= -1; + else if (i_pipe_stb && (i_op[0] || !w_cachable)) + r_wb_sel <= full_sel; + else if (!i_wb_stall) + r_wb_sel <= -1; + + assign o_wb_sel = r_wb_sel; + // }}} + end endgenerate + // }}} + + // o_wb_data + // {{{ + generate if (DATA_WIDTH == BUS_WIDTH) + begin + // {{{ + initial o_wb_data = 0; + always @(posedge i_clk) + if (i_reset) + o_wb_data <= 0; + else if ((!o_busy || !i_wb_stall) && (!OPT_LOWPOWER || i_pipe_stb)) + begin + if (DATA_WIDTH == 32) + begin + if (OPT_LOWPOWER) + begin : ZERO_UNUSED_DATA_BITS + casez({ i_op[2:1], i_addr[1:0] }) + 4'b0???: o_wb_data <= i_data; + 4'b100?: o_wb_data <= { i_data[15:0], 16'h0 }; + 4'b101?: o_wb_data <= { 16'h0, i_data[15:0] }; + 4'b1100: o_wb_data <= { i_data[7:0], 24'h0 }; + 4'b1101: o_wb_data <= { 8'h0, i_data[7:0], 16'h0 }; + 4'b1110: o_wb_data <= { 16'h0, i_data[7:0], 8'h0 }; + 4'b1111: o_wb_data <= { 24'h0, i_data[7:0] }; + endcase + end else begin : DUPLICATE_UNUSED_DATA_BITS + casez(i_op[2:1]) + 2'b0?: o_wb_data <= i_data; + 2'b10: o_wb_data <= { (2){i_data[15:0]} }; + 2'b11: o_wb_data <= { (4){i_data[ 7:0]} }; + endcase + end + end else begin + casez(i_op[2:1]) + 2'b0?: o_wb_data <= i_data << (8*i_addr[$clog2(DATA_WIDTH)-1:0]); + 2'b10: o_wb_data <= { 16'h0, i_data[15:0] } << (8*i_addr[$clog2(DATA_WIDTH)-1:0]); + 2'b11: o_wb_data <= { 24'h0, i_data[7:0] } << (8*i_addr[$clog2(DATA_WIDTH)-1:0]); + endcase + end + end else if (OPT_LOWPOWER && !i_wb_stall) + o_wb_data <= 0; + // }}} + end else begin + // {{{ + reg [DATA_WIDTH-1:0] pre_shift; + reg [BUS_WIDTH-1:0] wide_preshift, shifted_data; + + always @(*) + begin + casez(i_op[2:1]) + 2'b0?: pre_shift = i_data; + 2'b10: pre_shift = { i_data[15:0], + {(DATA_WIDTH-16){1'b0}} }; + 2'b11: pre_shift = { i_data[ 7:0], + {(DATA_WIDTH- 8){1'b0}} }; + endcase + + if (OPT_LOCAL_BUS && (&i_addr[DATA_WIDTH-1:DATA_WIDTH-8])) + begin + wide_preshift = { {(BUS_WIDTH-DATA_WIDTH){1'b0}}, pre_shift }; + + shifted_data = wide_preshift >> (8*i_addr[2-1:0]); + end else begin + wide_preshift = { pre_shift, + {(BUS_WIDTH-DATA_WIDTH){1'b0}} }; + + shifted_data = wide_preshift >> (8*i_addr[WBLSB-1:0]); + end + end + + initial o_wb_data = 0; + always @(posedge i_clk) + if (OPT_LOWPOWER && i_reset) + o_wb_data <= 0; + else if ((!o_busy || !i_wb_stall) + && (!OPT_LOWPOWER || (i_pipe_stb && i_op[0]))) + begin + if (!OPT_LOWPOWER) + begin + casez(i_op[2:1]) + 2'b0?: o_wb_data <= {(BUS_WIDTH/DATA_WIDTH){i_data}}; + 2'b10: o_wb_data <= {(BUS_WIDTH/16){i_data[15:0]}}; + 2'b11: o_wb_data <= {(BUS_WIDTH/ 8){i_data[ 7:0]}}; + endcase + end else begin + o_wb_data <= shifted_data; + end + end else if (OPT_LOWPOWER && !i_wb_stall) + o_wb_data <= 0; + // }}} + end endgenerate + // }}} + + // Register return FIFO + // {{{ generate if (OPT_PIPE) begin : OPT_PIPE_FIFO - reg [NAUX+4-2:0] fifo_data [0:((1< for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -// `include "cpudefs.v" -// -module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, - o_busy, o_valid, o_err, o_quotient, o_flags); - parameter BW=32, LGBW = 5; - input wire i_clk, i_reset; - // Input parameters - input wire i_wr, i_signed; - input wire [(BW-1):0] i_numerator, i_denominator; - // Output parameters - output reg o_busy, o_valid, o_err; - output reg [(BW-1):0] o_quotient; - output wire [3:0] o_flags; - +// }}} +module div #( + parameter BW=32, LGBW = 5, + parameter [0:0] OPT_LOWPOWER = 1'b0 + ) ( + // {{{ + input wire i_clk, i_reset, + // Input parameters + input wire i_wr, i_signed, + input wire [(BW-1):0] i_numerator, i_denominator, + // Output parameters + output reg o_busy, o_valid, o_err, + output reg [(BW-1):0] o_quotient, + output wire [3:0] o_flags + // }}} + ); + + // Local declarations + // {{{ // r_busy is an internal busy register. It will clear one clock // before we are valid, so it can't be o_busy ... // @@ -120,14 +123,18 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, wire [(BW):0] diff; // , xdiff[(BW-1):0]; assign diff = r_dividend[2*BW-2:BW-1] - r_divisor; - reg r_sign, pre_sign, r_z, r_c, last_bit; + reg r_sign, pre_sign, r_z, r_c, last_bit; reg [(LGBW-1):0] r_bit; - reg zero_divisor; + reg zero_divisor; + wire w_n; + // }}} + // r_busy + // {{{ // The Divide logic begins with r_busy. We use r_busy to determine // whether or not the divide is in progress, vs being complete. // Here, we clear r_busy on any reset and set it on i_wr (the request - // do to a divide). The divide ends when we are on the last bit, + // to do a divide). The divide ends when we are on the last bit, // or equivalently when we discover we are dividing by zero. initial r_busy = 1'b0; always @(posedge i_clk) @@ -137,7 +144,10 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, r_busy <= 1'b1; else if ((last_bit)||(zero_divisor)) r_busy <= 1'b0; + // }}} + // o_busy + // {{{ // o_busy is very similar to r_busy, save for some key differences. // Primary among them is that o_busy needs to (possibly) be true // for an extra clock after r_busy clears. This would be that extra @@ -154,11 +164,17 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, o_busy <= 1'b0; else if (!r_busy) o_busy <= 1'b0; + // }}} + // zero_divisor + // {{{ always @(posedge i_clk) if (i_wr) zero_divisor <= (i_denominator == 0); + // }}} + // o_valid + // {{{ // o_valid is part of the ZipCPU protocol. It will be set to true // anytime our answer is valid and may be used by the calling module. // Indeed, the ZipCPU will halt (and ignore us) once the i_wr has been @@ -183,7 +199,10 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, o_valid <= 1'b1; end else o_valid <= 1'b0; + // }}} + // o_err + // {{{ // Division by zero error reporting. Anytime we detect a zero divisor, // we set our output error, and then hold it until we are valid and // everything clears. @@ -195,9 +214,10 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, o_err <= 1'b1; else o_err <= 1'b0; + // }}} // r_bit - // + // {{{ // Keep track of which "bit" of our divide we are on. This number // ranges from 31 down to zero. On any write, we set ourselves to // 5'h1f. Otherwise, while we are busy (but not within the pre-sign @@ -210,9 +230,10 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, r_bit <= r_bit + 1'b1; else r_bit <= 0; + // }}} // last_bit - // + // {{{ // This logic replaces a lot of logic that was inside our giant state // machine with ... something simpler. In particular, we'll use this // logic to determine if we are processing our last bit. The only trick @@ -227,9 +248,10 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, last_bit <= (r_bit == {(LGBW){1'b1}}-1'b1); else last_bit <= 1'b0; + // }}} // pre_sign - // + // {{{ // This is part of the state machine. pre_sign indicates that we need // a extra clock to take the absolute value of our inputs. It need only // be true for the one clock, and then it must clear itself. @@ -239,7 +261,10 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, pre_sign <= 1'b0; else pre_sign <= (i_wr)&&(i_signed)&&((i_numerator[BW-1])||(i_denominator[BW-1])); + // }}} + // r_z + // {{{ // As a result of our operation, we need to set the flags. The most // difficult of these is the "Z" flag indicating that the result is // zero. Here, we'll use the same logic that sets the low-order @@ -250,8 +275,10 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, r_z <= 1'b1; else if ((r_busy)&&(!pre_sign)&&(!diff[BW])) r_z <= 1'b0; + // }}} // r_dividend + // {{{ // This is initially the numerator. On a signed divide, it then becomes // the absolute value of the numerator. We'll subtract from this value // the divisor for every output bit we are looking for--just as with @@ -263,21 +290,24 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, // absolute value of the dividend if (r_dividend[BW-1]) begin - r_dividend[2*BW-2:0] <= {(2*BW-1){1'b1}}; - r_dividend[BW-1:0] <= -r_dividend[BW-1:0]; + r_dividend[2*BW-2:0] <= {(2*BW-1){1'b0}}; + r_dividend[BW:0] <= -{ 1'b1, r_dividend[BW-1:0] }; end end else if (r_busy) begin r_dividend <= { r_dividend[2*BW-3:0], 1'b0 }; if (!diff[BW]) r_dividend[2*BW-2:BW] <= diff[(BW-2):0]; - end else if (!r_busy) + end else if (!r_busy && (!OPT_LOWPOWER || i_wr)) // Once we are done, and r_busy is no longer high, we'll // always accept new values into our dividend. This // guarantees that, when i_wr is set, the new value // is already set as desired. r_dividend <= { 31'h0, i_numerator }; + // }}} + // r_divisor + // {{{ initial r_divisor = 0; always @(posedge i_clk) if (i_reset) @@ -286,10 +316,12 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, begin if (r_divisor[BW-1]) r_divisor <= -r_divisor; - end else if (!r_busy) + end else if (!r_busy && (!OPT_LOWPOWER || i_wr)) r_divisor <= i_denominator; + // }}} // r_sign + // {{{ // is a flag for our state machine control(s). r_sign will be set to // true any time we are doing a signed divide and the result must be // negative. In that case, we take a final logic stage at the end of @@ -308,7 +340,10 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, r_sign <= (r_sign)&&(!zero_divisor); else r_sign <= 1'b0; + // }}} + // o_quotient + // {{{ initial o_quotient = 0; always @(posedge i_clk) if (i_reset) @@ -322,7 +357,10 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, o_quotient <= -o_quotient; else o_quotient <= 0; + // }}} + // r_c + // {{{ // Set Carry on an exact divide // Perhaps nothing uses this, but ... well, I suppose we could remove // this logic eventually, just ... not yet. @@ -332,197 +370,30 @@ module div(i_clk, i_reset, i_wr, i_signed, i_numerator, i_denominator, r_c <= 1'b0; else r_c <= (r_busy)&&(diff == 0); + // }}} + // w_n + // {{{ // The last flag: Negative. This flag is set assuming that the result // of the divide was negative (i.e., the high order bit is set). This // will also be true of an unsigned divide--if the high order bit is // ever set upon completion. Indeed, you might argue that there's no // logic involved. - wire w_n; assign w_n = o_quotient[(BW-1)]; + // }}} assign o_flags = { 1'b0, w_n, r_c, r_z }; - +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL - reg f_past_valid; - initial f_past_valid = 0; - always @(posedge i_clk) - f_past_valid <= 1'b1; - -`ifdef DIV -`define ASSUME assume -`else -`define ASSUME assert -`endif - - initial `ASSUME(i_reset); - always @(*) - if (!f_past_valid) - `ASSUME(i_reset); - - always @(posedge i_clk) - if ((!f_past_valid)||($past(i_reset))) - begin - assert(!o_busy); - assert(!o_valid); - assert(!o_err); - // - assert(!r_busy); - // assert(!zero_divisor); - assert(r_bit==0); - assert(!last_bit); - assert(!pre_sign); - // assert(!r_z); - // assert(r_dividend==0); - assert(o_quotient==0); - assert(!r_c); - assert(r_divisor==0); - - `ASSUME(!i_wr); - end - - always @(*) - if (o_busy) - `ASSUME(!i_wr); - - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset))&&($past(o_busy))&&(!o_busy)) - begin - assert(o_valid); - end - - // A formal methods section - // - // This section isn't yet complete. For now, it is just - // a description of things I think should be in here ... not - // yet a description of what it would take to prove - // this divide (yet). - always @(*) - if (o_err) - assert(o_valid); - - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_wr))) - assert(!pre_sign); - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset))&&($past(i_wr))&&($past(i_signed)) - &&(|$past({i_numerator[BW-1],i_denominator[BW-1]}))) - assert(pre_sign); - - // always @(posedge i_clk) - // if ((f_past_valid)&&(!$past(pre_sign))) - // assert(!r_sign); - reg [BW:0] f_bits_set; - - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset))&&($past(i_wr))) - assert(o_busy); - - always @(posedge i_clk) - if ((f_past_valid)&&($past(o_valid))) - assert(!o_valid); - - always @(*) - if ((o_valid)&&(!o_err)) - assert(r_z == ((o_quotient == 0)? 1'b1:1'b0)); - else if (o_busy) - assert(r_z == (((o_quotient&f_bits_set[BW-1:0]) == 0)? 1'b1: 1'b0)); - - always @(*) - if ((o_valid)&&(!o_err)) - assert(w_n == o_quotient[BW-1]); - - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(r_busy))&&(!$past(i_wr))) - assert(!o_busy); - always @(posedge i_clk) - assert((!o_busy)||(!o_valid)); - - always @(*) - if(r_busy) - assert(o_busy); - - always @(posedge i_clk) - if (i_reset) - f_bits_set <= 0; - else if (i_wr) - f_bits_set <= 0; - else if ((r_busy)&&(!pre_sign)) - f_bits_set <= { f_bits_set[BW-1:0], 1'b1 }; - - always @(posedge i_clk) - if (r_busy) - assert(((1<= $past(r_divisor[BW-1:0])) - assert(o_quotient[0]); - else - assert(!o_quotient[0]); - end - */ - - always @(*) - if (r_busy) - assert((f_bits_set & r_dividend[BW-1:0])==0); - - always @(*) - if (r_busy) - assert((r_divisor == 0) == zero_divisor); - -`ifdef VERIFIC - // Verify unsigned division - assert property (@(posedge i_clk) - disable iff (i_reset) - (i_wr)&&(i_denominator != 0)&&(!i_signed) - |=> ((!o_err)&&(!o_valid)&&(o_busy)&&(!r_sign)&&(!pre_sign) - throughout (r_bit == 0) - ##1 ((r_bit == $past(r_bit)+1)&&({1'b0,r_bit}< BW-1)) - [*0:$] - ##1 ({ 1'b0, r_bit } == BW-1)) - ##1 (!o_err)&&(o_valid)); - - // Verify division by zero - assert property (@(posedge i_clk) - disable iff (i_reset) - (i_wr)&&(i_denominator == 0) - |=> (zero_divisor throughout - (!o_err)&&(!o_valid)&&(pre_sign) [*0:1] - ##1 ((r_busy)&&(!o_err)&&(!o_valid)) - ##1 ((o_err)&&(o_valid)))); - - -`endif // VERIFIC +// The formal properties for this module are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/icontrol.v b/rtl/cpu/icontrol.v index d7ce947..73b56fa 100644 --- a/rtl/cpu/icontrol.v +++ b/rtl/cpu/icontrol.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: icontrol.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: An interrupt controller, for managing many interrupt sources. @@ -51,11 +51,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015,2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -68,41 +68,47 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module icontrol(i_clk, i_reset, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, - i_wb_sel, - o_wb_stall, o_wb_ack, o_wb_data, - i_brd_ints, o_interrupt); - parameter IUSED = 12, DW=32; - input wire i_clk, i_reset; - input wire i_wb_cyc, i_wb_stb, i_wb_we; - input wire [DW-1:0] i_wb_data; - input wire [DW/8-1:0] i_wb_sel; - output wire o_wb_stall, o_wb_ack; - output reg [DW-1:0] o_wb_data; - input wire [(IUSED-1):0] i_brd_ints; - output reg o_interrupt; +// }}} +module icontrol #( + // {{{ + parameter IUSED = 12, DW=32 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + input wire i_wb_cyc, i_wb_stb, i_wb_we, + input wire [DW-1:0] i_wb_data, + input wire [DW/8-1:0] i_wb_sel, + output wire o_wb_stall, o_wb_ack, + output reg [DW-1:0] o_wb_data, + input wire [(IUSED-1):0] i_brd_ints, + output reg o_interrupt + // }}} + ); + // Local declarations + // {{{ reg [(IUSED-1):0] r_int_state; reg [(IUSED-1):0] r_int_enable; reg r_mie; wire w_any; wire wb_write, enable_ints, disable_ints; + // }}} assign wb_write = (i_wb_stb)&&(i_wb_we); assign enable_ints = (wb_write)&&( i_wb_data[15]); assign disable_ints = (wb_write)&&(!i_wb_data[15]); - // + // r_int_state + // {{{ // First step: figure out which interrupts have triggered. An // interrupt "triggers" when the incoming interrupt wire is high, and // stays triggered until cleared by the bus. @@ -115,8 +121,10 @@ module icontrol(i_clk, i_reset, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, | (r_int_state & (~i_wb_data[(IUSED-1):0])); else r_int_state <= (r_int_state | i_brd_ints); + // }}} - // + // r_int_enable + // {{{ // Second step: determine which interrupts are enabled. // Only interrupts that are enabled will be propagated forward on // the global interrupt line. @@ -128,9 +136,11 @@ module icontrol(i_clk, i_reset, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, r_int_enable <= r_int_enable | i_wb_data[16 +: IUSED]; else if (disable_ints) r_int_enable <= r_int_enable & (~ i_wb_data[16 +: IUSED]); + // }}} - // - // Third step: The global interrupt enable bit. + // r_mie + // {{{ + // Third step: The master (global) interrupt enable bit. initial r_mie = 1'b0; always @(posedge i_clk) if (i_reset) @@ -139,11 +149,14 @@ module icontrol(i_clk, i_reset, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, r_mie <= 1'b1; else if (disable_ints && i_wb_data[DW-1]) r_mie <= 1'b0; + // }}} // // Have "any" enabled interrupts triggered? assign w_any = ((r_int_state & r_int_enable) != 0); + // o_interrupt + // {{{ // How then shall the interrupt wire be set? initial o_interrupt = 1'b0; always @(posedge i_clk) @@ -151,8 +164,10 @@ module icontrol(i_clk, i_reset, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, o_interrupt <= 1'b0; else o_interrupt <= (r_mie)&&(w_any); + // }}} - // + // o_wb_data + // {{{ // Create the output data. Place this into the next clock, to keep // it synchronous with w_any. initial o_wb_data = 0; @@ -165,27 +180,38 @@ module icontrol(i_clk, i_reset, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, o_wb_data[16 +: IUSED] <= r_int_enable; o_wb_data[ 0 +: IUSED] <= r_int_state; end + // }}} + + assign o_wb_ack = i_wb_stb; + assign o_wb_stall = 1'b0; // Make verilator happy + // {{{ + // verilator coverage_off + // verilator lint_off UNUSED generate if (IUSED < 15) - begin - // verilator lint_off UNUSED - wire unused; - assign unused = &{ 1'b0, i_wb_data[32-2:(16+IUSED)], + begin : UNUSED_INTS + wire unused_int; + assign unused_int = &{ 1'b0, i_wb_data[32-2:(16+IUSED)], i_wb_data[16-2:IUSED] }; - // verilator lint_on UNUSED - end endgenerate - assign o_wb_ack = i_wb_stb; - assign o_wb_stall = 1'b0; - - // Verilator lint_off UNUSED wire unused; assign unused = &{ 1'b0, i_wb_cyc, i_wb_sel }; // verilator lint_on UNUSED - + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties section +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // Formal properties for this module are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/idecode.v b/rtl/cpu/idecode.v index 59afce7..cd58772 100644 --- a/rtl/cpu/idecode.v +++ b/rtl/cpu/idecode.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: idecode.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: This RTL file specifies how instructions are to be decoded @@ -18,11 +18,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -36,79 +36,75 @@ // target there if the PDF file isn't present.) If not, see // for a copy. // +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -`define CPU_SP_REG 4'hd -`define CPU_CC_REG 4'he -`define CPU_PC_REG 4'hf -// -`define CISBIT 31 -`define CISIMMSEL 23 -`define IMMSEL 18 -// -// -// -module idecode(i_clk, i_reset, i_ce, i_stalled, - i_instruction, i_gie, i_pc, i_pf_valid, - i_illegal, - o_valid, - o_phase, o_illegal, - o_pc, - o_dcdR, o_dcdA, o_dcdB, - o_preA, o_preB, - o_I, o_zI, - o_cond, o_wF, - o_op, o_ALU, o_M, o_DV, o_FP, o_break, o_lock, - o_wR, o_rA, o_rB, - o_early_branch, o_early_branch_stb, o_branch_pc, o_ljmp, - o_pipe, - o_sim, o_sim_immv - ); - parameter ADDRESS_WIDTH=24; - parameter [0:0] OPT_MPY = 1'b1; - parameter [0:0] OPT_EARLY_BRANCHING = 1'b1; - parameter [0:0] OPT_PIPELINED = 1'b1; - parameter [0:0] OPT_DIVIDE = (OPT_PIPELINED); - parameter [0:0] OPT_FPU = 1'b0; - parameter [0:0] OPT_CIS = 1'b1; - parameter [0:0] OPT_LOCK = (OPT_PIPELINED); - parameter [0:0] OPT_OPIPE = (OPT_PIPELINED); - parameter [0:0] OPT_SIM = 1'b0; - parameter [0:0] OPT_NO_USERMODE = 1'b0; - localparam AW = ADDRESS_WIDTH; - // - input wire i_clk, i_reset, i_ce, i_stalled; - input wire [31:0] i_instruction; - input wire i_gie; - input wire [(AW+1):0] i_pc; - input wire i_pf_valid, i_illegal; - output wire o_valid, o_phase; - output reg o_illegal; - output reg [(AW+1):0] o_pc; - output reg [6:0] o_dcdR, o_dcdA, o_dcdB; - output wire [4:0] o_preA, o_preB; - output wire [31:0] o_I; - output reg o_zI; - output reg [3:0] o_cond; - output reg o_wF; - output reg [3:0] o_op; - output reg o_ALU, o_M, o_DV, o_FP, o_break; - output reg o_lock; - output reg o_wR, o_rA, o_rB; - output wire o_early_branch, o_early_branch_stb; - output wire [(AW+1):0] o_branch_pc; - output wire o_ljmp; - output wire o_pipe; - output reg o_sim /* verilator public_flat */; - output reg [22:0] o_sim_immv /* verilator public_flat */; - +// }}} +module idecode #( + // {{{ + parameter ADDRESS_WIDTH=24, + parameter [0:0] OPT_MPY = 1'b1, + parameter [0:0] OPT_SHIFTS = 1'b1, + parameter [0:0] OPT_EARLY_BRANCHING = 1'b1, + parameter [0:0] OPT_PIPELINED = 1'b1, + parameter [0:0] OPT_DIVIDE = (OPT_PIPELINED), + parameter [0:0] OPT_FPU = 1'b0, + parameter [0:0] OPT_CIS = 1'b1, + parameter [0:0] OPT_LOCK = (OPT_PIPELINED), + parameter [0:0] OPT_OPIPE = (OPT_PIPELINED), + parameter [0:0] OPT_SIM = 1'b0, + parameter [0:0] OPT_SUPPRESS_NULL_BRANCHES = 1'b0, + parameter [0:0] OPT_USERMODE = 1'b1, + parameter [0:0] OPT_LOWPOWER = 1'b0, + localparam AW = ADDRESS_WIDTH + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, i_ce, i_stalled, + input wire [31:0] i_instruction, + input wire i_gie, + input wire [(AW+1):0] i_pc, + input wire i_pf_valid, i_illegal, + output wire o_valid, o_phase, + output reg o_illegal, + output reg [(AW+1):0] o_pc, + output reg [6:0] o_dcdR, o_dcdA, o_dcdB, + output wire [4:0] o_preA, o_preB, + output wire [31:0] o_I, + output reg o_zI, + output reg [3:0] o_cond, + output reg o_wF, + output reg [3:0] o_op, + output reg o_ALU, o_M, o_DV, o_FP, o_break, + output reg o_lock, + output reg o_wR, o_rA, o_rB, + output wire o_early_branch, o_early_branch_stb, + output wire [(AW+1):0] o_branch_pc, + output wire o_ljmp, + output wire o_pipe, + output reg o_sim /* verilator public_flat */, + output reg [22:0] o_sim_immv /* verilator public_flat */ +`ifdef FORMAL + , output reg [31:0] f_insn_word, + output reg f_insn_gie, + output wire f_insn_is_pipeable +`endif + // }}} + ); + + // Declarations + // {{{ + localparam [3:0] CPU_SP_REG = 4'hd, + CPU_CC_REG = 4'he, + CPU_PC_REG = 4'hf; + localparam CISBIT = 31, + CISIMMSEL = 23, + IMMSEL = 18; wire [4:0] w_op; wire w_ldi, w_mov, w_cmptst, w_ldilo, w_ALU, w_brev, @@ -125,11 +121,21 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, wire [31:0] iword; wire pf_valid; - assign pf_valid = (i_pf_valid)&&(!o_early_branch_stb); + reg [14:0] r_nxt_half; + reg [4:0] w_cis_op; + reg [22:0] r_I, w_fullI; + wire [22:0] w_I; + wire w_Iz; + reg [1:0] w_immsrc; + reg r_valid; + wire insn_is_pipeable, illegal_shift; + // }}} - reg [14:0] r_nxt_half; + assign pf_valid = (i_pf_valid)&&(!o_early_branch_stb); + // iword + // {{{ generate if (OPT_CIS) begin : SET_IWORD @@ -141,15 +147,20 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, end else begin : CLR_IWORD assign iword = { 1'b0, i_instruction[30:0] }; + // verilator coverage_off // verilator lint_off UNUSED - wire [14:0] unused_nxt_half; - assign unused_nxt_half = r_nxt_half; + wire unused_nxt_half; + assign unused_nxt_half = &{ 1'b0, r_nxt_half }; // verilator lint_on UNUSED + // verilator coverage_on end endgenerate + // }}} + // w_ljmp, w_cis_ljmp : Long jump early branching + // {{{ generate if (OPT_EARLY_BRANCHING) - begin + begin : GEN_CIS_LONGJUMP if (OPT_CIS) begin : CIS_EARLY_BRANCHING @@ -163,19 +174,20 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, assign w_ljmp = (iword == 32'h7c87c000); - end else begin : NO_EARLY_BRANCHING + end else begin : NO_CIS_JUMPING assign w_cis_ljmp = 1'b0; assign w_ljmp = 1'b0; end endgenerate + // }}} - reg [4:0] w_cis_op; - + // w_cis_op : Get the opcode + // {{{ generate if (OPT_CIS) begin : GEN_CIS_OP always @(*) - if (!iword[`CISBIT]) + if (!iword[CISBIT]) w_cis_op = iword[26:22]; else case(iword[26:24]) 3'h0: w_cis_op = 5'h00; // ADD @@ -194,8 +206,10 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, w_cis_op = w_op; end endgenerate + // }}} // Decode instructions + // {{{ assign w_op= iword[26:22]; assign w_mov = (w_cis_op == 5'h0d); assign w_ldi = (w_cis_op[4:1] == 4'hc); @@ -208,61 +222,67 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, assign w_add = (w_cis_op[4:0] == 5'h02); assign w_mem = (w_cis_op[4:3] == 2'b10)&&(w_cis_op[2:1] !=2'b00); assign w_sto = (w_mem)&&( w_cis_op[0]); - assign w_div = (!iword[`CISBIT])&&(w_op[4:1] == 4'h7); - assign w_fpu = (!iword[`CISBIT])&&(w_op[4:3] == 2'b11) + assign w_div = (!iword[CISBIT])&&(w_op[4:1] == 4'h7); + assign w_fpu = (!iword[CISBIT])&&(w_op[4:3] == 2'b11) &&(w_dcdR[3:1] != 3'h7)&&(w_op[2:1] != 2'b00); // If the result register is either CC or PC, and this would otherwise // be a floating point instruction with floating point opcode of 0, // then this is a NOOP. - assign w_special= (!iword[`CISBIT])&&((!OPT_FPU)||(w_dcdR[3:1]==3'h7)) + assign w_special= (!iword[CISBIT])&&((!OPT_FPU)||(w_dcdR[3:1]==3'h7)) &&(w_op[4:2] == 3'b111); assign w_break = (w_special)&&(w_op[4:0]==5'h1c); assign w_lock = (w_special)&&(w_op[4:0]==5'h1d); assign w_sim = (w_special)&&(w_op[4:0]==5'h1e); assign w_noop = (w_special)&&(w_op[4:1]==4'hf); // Must include w_sim + // }}} - - // w_dcdR (4 LUTs) - // + // w_dcdR, w_dcdA + // {{{ // What register will we be placing results into (if at all)? // // Two parts to the result register: the register set, given for // moves in iword[18] but only for the supervisor, and the other // four bits encoded in the instruction. // - assign w_dcdR = { ((!iword[`CISBIT])&&(!OPT_NO_USERMODE)&&(w_mov)&&(!i_gie))?iword[`IMMSEL]:i_gie, + assign w_dcdR = { ((!iword[CISBIT])&&(OPT_USERMODE)&&(w_mov)&&(!i_gie))?iword[IMMSEL]:i_gie, iword[30:27] }; - // dcdB - What register is used in the opB? - // - assign w_dcdB[4] = ((!iword[`CISBIT])&&(w_mov)&&(!OPT_NO_USERMODE)&&(!i_gie))?iword[13]:i_gie; - assign w_dcdB[3:0]= (iword[`CISBIT]) - ? (((!iword[`CISIMMSEL])&&(iword[26:25]==2'b10)) - ? `CPU_SP_REG : iword[22:19]) - : iword[17:14]; - // 0 LUTs assign w_dcdA = w_dcdR; // on ZipCPU, A is always result reg - // 2 LUTs, 1 delay each - assign w_dcdR_pc = (w_dcdR == {i_gie, `CPU_PC_REG}); - assign w_dcdR_cc = (w_dcdR == {i_gie, `CPU_CC_REG}); // 0 LUTs assign w_dcdA_pc = w_dcdR_pc; assign w_dcdA_cc = w_dcdR_cc; + // 2 LUTs, 1 delay each + assign w_dcdR_pc = (w_dcdR == {i_gie, CPU_PC_REG}); + assign w_dcdR_cc = (w_dcdR == {i_gie, CPU_CC_REG}); + // }}} + + // dcdB - What register is used in the opB? + // {{{ + assign w_dcdB[4] = ((!iword[CISBIT])&&(w_mov)&&(OPT_USERMODE)&&(!i_gie))?iword[13]:i_gie; + assign w_dcdB[3:0]= (iword[CISBIT]) + ? (((!iword[CISIMMSEL])&&(iword[26:25]==2'b10)) + ? CPU_SP_REG : iword[22:19]) + : iword[17:14]; + // 2 LUTs, 1 delays each - assign w_dcdB_pc = (w_rB)&&(w_dcdB[3:0] == `CPU_PC_REG); - assign w_dcdB_cc = (w_rB)&&(w_dcdB[3:0] == `CPU_CC_REG); + assign w_dcdB_pc = (w_rB)&&(w_dcdB[3:0] == CPU_PC_REG); + assign w_dcdB_cc = (w_rB)&&(w_dcdB[3:0] == CPU_CC_REG); + // }}} - // + // w_cond + // {{{ // Under what condition will we execute this instruction? Only the // load immediate instruction and the CIS instructions are completely // unconditional. Well ... not quite. The BREAK, LOCK, and SIM/NOOP // instructions are also unconditional. // - assign w_cond = ((w_ldi)||(w_special)||(iword[`CISBIT])) ? 4'h8 : + assign w_cond = ((w_ldi)||(w_special)||(iword[CISBIT])) ? 4'h8 : { (iword[21:19]==3'h0), iword[21:19] }; + // }}} // rA - do we need to read register A? + // {{{ assign w_rA = // Floating point reads reg A ((w_fpu)&&(OPT_FPU)) // Divide's read A @@ -274,50 +294,58 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, ||(w_sto) // Test/compares ||(w_cmptst); - // rB -- do we read a register for operand B? Specifically, do we - // add the registers value to the immediate to create opB? + // }}} + + // rB -- do we read a register for operand B? + // {{{ + // Specifically, do we add the registers value to the immediate to + // create opB? assign w_rB = (w_mov) - ||((!iword[`CISBIT])&&(iword[`IMMSEL])&&(!w_ldi)&&(!w_special)) - ||(( iword[`CISBIT])&&(iword[`CISIMMSEL])&&(!w_ldi)) + ||((!iword[CISBIT])&&(iword[IMMSEL])&&(!w_ldi)&&(!w_special)) + ||(( iword[CISBIT])&&(iword[CISIMMSEL])&&(!w_ldi)) // If using compressed instruction sets, // we *always* read on memory operands. - ||(( iword[`CISBIT])&&(w_mem)); + ||(( iword[CISBIT])&&(w_mem)); + // }}} // wR -- will we be writing our result back? + // {{{ // wR_n = !wR // All but STO, NOOP/BREAK/LOCK, and CMP/TST write back to w_dcdR assign w_wR_n = (w_sto) ||(w_special) ||(w_cmptst); assign w_wR = !w_wR_n; + // }}} // // wF -- do we write flags when we are done? - // + // {{{ assign w_wF = (w_cmptst) ||((w_cond[3])&&(((w_fpu)&&(OPT_FPU))||(w_div) ||((w_ALU)&&(!w_mov)&&(!w_ldilo)&&(!w_brev) &&(w_dcdR[3:1] != 3'h7)))); + // }}} + // w_immsrc - where does the immediate value come from + // {{{ // Bottom 13 bits: no LUT's // w_dcd[12: 0] -- no LUTs // w_dcd[ 13] -- 2 LUTs // w_dcd[17:14] -- (5+i0+i1) = 3 LUTs, 1 delay // w_dcd[22:18] : 5 LUTs, 1 delay (assuming high bit is o/w determined) - reg [22:0] r_I, w_fullI; - wire [22:0] w_I; - wire w_Iz; - - reg [1:0] w_immsrc; always @(*) if (w_ldi) w_immsrc = 0; else if (w_mov) w_immsrc = 1; - else if (!iword[`IMMSEL]) + else if (!iword[IMMSEL]) w_immsrc = 2; - else // if (!iword[`IMMSEL]) + else // if (!iword[IMMSEL]) w_immsrc = 3; + // }}} + // w_fullI -- extracting the immediate value from the insn word + // {{{ always @(*) case(w_immsrc) 2'b00: w_fullI = { iword[22:0] }; // LDI @@ -330,22 +358,24 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, // MOVE immediates have one less bit :((w_mov) ?{ {(23-13){iword[12]}}, iword[12:0] } // Normal Op-B immediate ... 18 or 14 bits - :((!iword[`IMMSEL]) ? { {(23-18){iword[17]}}, iword[17:0] } + :((!iword[IMMSEL]) ? { {(23-18){iword[17]}}, iword[17:0] } : { {(23-14){iword[13]}}, iword[13:0] } )); */ + // }}} + // w_I and w_Iz: Immediate value decoding + // {{{ generate if (OPT_CIS) begin : GEN_CIS_IMMEDIATE - wire [7:0] w_halfbits; - assign w_halfbits = iword[`CISIMMSEL:16]; + wire [7:0] w_halfI, w_halfbits; - wire [7:0] w_halfI; + assign w_halfbits = iword[CISIMMSEL:16]; assign w_halfI = (iword[26:24]==3'h6) ? w_halfbits[7:0] // 8'b for LDI :(w_halfbits[7])? { {(6){w_halfbits[2]}}, w_halfbits[1:0]} :{ w_halfbits[6], w_halfbits[6:0] }; - assign w_I = (iword[`CISBIT]) + assign w_I = (iword[CISBIT]) ? {{(23-8){w_halfI[7]}}, w_halfI } : w_fullI; @@ -356,9 +386,10 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, end endgenerate assign w_Iz = (w_I == 0); + // }}} - - // + // o_phase + // {{{ // The o_phase parameter is special. It needs to let the software // following know that it cannot break/interrupt on an o_phase asserted // instruction, lest the break take place between the first and second @@ -367,6 +398,7 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, // a 32-bit instruction or the second half of a 2x16-bit instruction. generate if (OPT_CIS) begin : GEN_CIS_PHASE + // {{{ reg r_phase; // Phase is '1' on the first instruction of a two-part set @@ -388,32 +420,70 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, // back to the first r_phase <= 0; else - r_phase <= (i_instruction[`CISBIT])&&(!i_illegal); + r_phase <= (i_instruction[CISBIT])&&(!i_illegal); end else if (i_ce) r_phase <= 1'b0; assign o_phase = r_phase; - end else begin + // }}} + end else begin : NO_CIS + // {{{ assign o_phase = 1'b0; + // }}} end endgenerate + // }}} + + // illegal_shift + // {{{ + generate if (OPT_SHIFTS) + begin : LEGAL_SHIFTS + assign illegal_shift = 1'b0; + end else begin : GEN_ILLEGAL_SHIFT + reg r_illegal_shift; + always @(*) + begin + r_illegal_shift = 1'b1; + if (i_instruction[CISBIT]) + r_illegal_shift = 1'b0; + else if ((i_instruction[26:22] != 5'h5) + &&(i_instruction[26:22] != 5'h6) + &&(i_instruction[26:22] != 5'h7)) + r_illegal_shift = 1'b0; + else if (!i_instruction[18] + && (i_instruction[17:0] == 18'h1)) + r_illegal_shift = 1'b0; + end + assign illegal_shift = r_illegal_shift; + end endgenerate + // }}} + + // o_illegal + // {{{ initial o_illegal = 1'b0; always @(posedge i_clk) if (i_reset) o_illegal <= 1'b0; else if (i_ce && o_phase) begin + // {{{ o_illegal <= o_illegal; // Cannot happen in compressed word ... // 1. multiply op-codes // 2. divide opcodes // 3. FPU opcodes // 4. special opcodes - end else if (i_ce && i_pf_valid) begin + // }}} + end else if (i_ce && i_pf_valid) + begin + // {{{ o_illegal <= 1'b0; - if ((!OPT_CIS)&&(i_instruction[`CISBIT])) + if (illegal_shift) + o_illegal <= 1'b1; + + if ((!OPT_CIS)&&(i_instruction[CISBIT])) o_illegal <= 1'b1; if ((!OPT_MPY)&&(w_mpy)) o_illegal <= 1'b1; @@ -445,10 +515,15 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, if ((!OPT_LOCK)&&(w_lock)) o_illegal <= 1'b1; + // Bus errors always create illegal instructions if (i_illegal) o_illegal <= 1'b1; + // }}} end + // }}} + // o_pc + // {{{ initial o_pc = 0; always @(posedge i_clk) if ((i_ce)&&((o_phase)||(i_pf_valid))) @@ -457,7 +532,7 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, if (OPT_CIS) begin - if (iword[`CISBIT]) + if (iword[CISBIT]) begin if (o_phase) o_pc[AW+1:1] <= o_pc[AW+1:1] + 1'b1; @@ -472,16 +547,25 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, o_pc <= { i_pc[AW+1:2] + 1'b1, 2'b00 }; end end + // }}} + // Generate output products + // {{{ initial o_dcdR = 0; initial o_dcdA = 0; initial o_dcdB = 0; initial o_DV = 0; initial o_FP = 0; initial o_lock = 0; + initial o_sim = 1'b0; + initial o_sim_immv = 0; + // r_I, o_zI, o_wR, o_rA, o_rB, o_dcdR, o_dcdA, o_dcdB always @(posedge i_clk) if (i_ce) begin + // {{{ + // o_cond, o_wF + // {{{ // Under what condition will we execute this // instruction? Only the load immediate instruction // is completely unconditional. @@ -490,7 +574,10 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, // UNLESS: the conditional instruction was a CMP // or TST instruction. o_wF <= w_wF; + // }}} + // o_op + // {{{ // Record what operation/op-code (4-bits) we are doing // Note that LDI magically becomes a MOV // instruction here. That way it's a pass through @@ -505,8 +592,8 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, o_op <= w_cis_op[3:0]; if ((w_ldi)||(w_noop)||(w_lock)) o_op <= 4'hd; + // }}} - // Default values o_dcdR <= { w_dcdR_cc, w_dcdR_pc, w_dcdR}; o_dcdA <= { w_dcdA_cc, w_dcdA_pc, w_dcdA}; o_dcdB <= { w_dcdB_cc, w_dcdB_pc, w_dcdB}; @@ -516,6 +603,8 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, r_I <= w_I; o_zI <= w_Iz; + // o_ALU, o_M, o_DV, o_FP + // {{{ // Turn a NOOP into an ALU operation--subtract in // particular, although it doesn't really matter as long // as it doesn't take longer than one clock. Note @@ -526,31 +615,45 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, o_M <= w_mem; o_DV <= (OPT_DIVIDE)&&(w_div); o_FP <= (OPT_FPU)&&(w_fpu); + // }}} + // o_break, o_lock + // {{{ o_break <= w_break; o_lock <= (OPT_LOCK)&&(w_lock); + // }}} if (OPT_CIS) r_nxt_half <= { iword[14:0] }; else r_nxt_half <= 0; + // o_sim, o_sim_immv -- simulation instructions vs NOOPs + // {{{ if (OPT_SIM) begin // Support the SIM instruction(s) o_sim <= (w_sim)||(w_noop); o_sim_immv <= iword[22:0]; + if (OPT_LOWPOWER && !w_sim && !w_noop) + o_sim_immv <= 0; end else begin o_sim <= 1'b0; o_sim_immv <= 0; end + // }}} + // }}} end + // }}} assign o_preA = w_dcdA; assign o_preB = w_dcdB; + // o_early_branch, o_early_branch_stb, o_branch_pc + // {{{ generate if (OPT_EARLY_BRANCHING) begin : GEN_EARLY_BRANCH_LOGIC + // {{{ reg r_early_branch, r_early_branch_stb, r_ljmp; @@ -568,11 +671,11 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, r_ljmp <= 1'b0; else if (pf_valid) begin - if ((OPT_CIS)&&(iword[`CISBIT])) + if ((OPT_CIS)&&(iword[CISBIT])) r_ljmp <= w_cis_ljmp; else r_ljmp <= (w_ljmp); - end else if ((OPT_CIS)&&(o_phase)&&(iword[`CISBIT])) + end else if ((OPT_CIS)&&(o_phase)&&(iword[CISBIT])) r_ljmp <= w_cis_ljmp; end assign o_ljmp = r_ljmp; @@ -591,14 +694,14 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, // LW (PC),PC r_early_branch <= 1'b1; r_early_branch_stb <= 1'b1; - end else if ((!iword[`CISBIT])&&(iword[30:27]==`CPU_PC_REG) + end else if ((!iword[CISBIT])&&(iword[30:27]==CPU_PC_REG) &&(w_cond[3])) begin - if ((w_add)&&(!iword[`IMMSEL])) + if ((w_add)&&(!iword[IMMSEL])) begin // Add x,PC r_early_branch <= 1'b1; - r_early_branch_stb <= 1'b1; + r_early_branch_stb <= (!OPT_SUPPRESS_NULL_BRANCHES) || !w_Iz; end else begin r_early_branch <= 1'b0; r_early_branch_stb <= 1'b0; @@ -617,38 +720,54 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, initial r_branch_pc = 0; always @(posedge i_clk) - if (i_ce) + if (OPT_LOWPOWER && i_reset) + r_branch_pc <= 0; + else if (i_ce) begin if (r_ljmp) r_branch_pc <= { iword[(AW+1):2], 2'b00 }; - else begin - // Add x,PC - r_branch_pc[AW+1:2] <= i_pc[AW+1:2] - + {{(AW-15){iword[17]}},iword[16:2]} - + {{(AW-1){1'b0}},1'b1}; - r_branch_pc[1:0] <= 2'b00; - end + else if (!OPT_LOWPOWER + || (!iword[CISBIT]&&(iword[30:27]==CPU_PC_REG) + &&w_cond[3]&& w_add && !iword[IMMSEL])) + begin + // Add x,PC + r_branch_pc[AW+1:2] <= i_pc[AW+1:2] + + {{(AW-15){iword[17]}},iword[16:2]} + + {{(AW-1){1'b0}},1'b1}; + r_branch_pc[1:0] <= 2'b00; + end else if (OPT_LOWPOWER) + r_branch_pc <= 0; + + if (OPT_LOWPOWER && !pf_valid) + r_branch_pc <= 0; end assign w_ljmp_dly = r_ljmp; assign o_early_branch = r_early_branch; assign o_early_branch_stb = r_early_branch_stb; assign o_branch_pc = r_branch_pc; - end else begin + // }}} + end else begin : NO_EARLY_BRANCHING + // {{{ assign w_ljmp_dly = 1'b0; assign o_early_branch = 1'b0; assign o_early_branch_stb = 1'b0; assign o_branch_pc = {(AW+2){1'b0}}; assign o_ljmp = 1'b0; + // verilator coverage_off // verilator lint_off UNUSED wire early_branch_unused; - assign early_branch_unused = w_add; + assign early_branch_unused = &{ 1'b0, w_add }; // verilator lint_on UNUSED + // verilator coverage_on + // }}} end endgenerate + // }}} - + // o_pipe + // {{{ // To be a pipeable operation there must be ... // 1. Two valid adjacent instructions // 2. Both must be memory operations, of the same time (both lods @@ -659,13 +778,10 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, // Note that we're not using iword here ... there's a lot of logic // taking place, and it's only valid if the new word is not compressed. // - reg r_valid, r_insn_is_pipeable; generate if (OPT_OPIPE) begin : GEN_OPIPE - reg r_pipe; - - wire [13:0] pipe_addr_diff; - assign pipe_addr_diff = w_I[13:0] - r_I[13:0]; + // {{{ + reg r_pipe, r_insn_is_pipeable; // Pipeline logic is too extreme for a single clock. // Let's break it into two clocks, using r_insn_is_pipeable @@ -682,7 +798,7 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, r_insn_is_pipeable <= 1'b0; else if (o_ljmp) r_insn_is_pipeable <= 1'b0; - else if ((i_ce)&&((!OPT_CIS)&&(i_instruction[`CISBIT]))) + else if ((i_ce)&&((!OPT_CIS)&&(i_instruction[CISBIT]))) r_insn_is_pipeable <= 1'b0; else if (i_ce) begin // This is a valid instruction @@ -705,6 +821,7 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, end // else // The pipeline is stalled + assign insn_is_pipeable = r_insn_is_pipeable; initial r_pipe = 1'b0; always @(posedge i_clk) @@ -722,34 +839,40 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, // Both must be register ops &&(w_rB) // Both must use the same register for B - &&(w_dcdB[3:0] == o_dcdB[3:0]) - // CC or PC registers are not valid addresses - // Captured above - // But ... the result can never be B - // Captured above - // - // Reads to CC or PC not allowed - // &&((o_op[0])||(w_dcdR[3:1] != 3'h7)) - // Prior-reads to CC or PC not allowed - // Captured above - // Same condition, or no condition before - &&((w_cond[2:0]==o_cond[2:0]) - ||(o_cond[2:0] == 3'h0)) - // Same or incrementing immediate - &&(w_I[13]==r_I[13]) - &&(pipe_addr_diff <= 14'h4); + &&(w_dcdB[3:0] == o_dcdB[3:0]); + // // CC or PC registers are not valid addresses + // // Captured above + // // But ... the result can never be B + // // Captured above + // // + // // Reads to CC or PC not allowed + // // &&((o_op[0])||(w_dcdR[3:1] != 3'h7)) + // // Prior-reads to CC or PC not allowed + // // Captured above + // // Same condition, or no condition now + // &&((w_cond[2:0]==o_cond[2:0]) + // ||(w_cond[2:0] == 3'h0)); + // // Same or incrementing immediate + // &&(w_I[22]==r_I[22]); assign o_pipe = r_pipe; + // }}} end else begin - assign o_pipe = 1'b0; - always @(*) - r_insn_is_pipeable = 1'b0; + // {{{ + assign o_pipe = 1'b0; + assign insn_is_pipeable = 1'b0; + // verilator coverage_off // verilator lint_off UNUSED wire unused_pipable; - assign unused_pipable = r_insn_is_pipeable; + assign unused_pipable = &{ 1'b0, insn_is_pipeable }; // verilator lint_on UNUSED + // verilator coverage_on + // }}} end endgenerate + // }}} + // o_valid + // {{{ initial r_valid = 1'b0; generate if (OPT_PIPELINED) begin : GEN_DCD_VALID @@ -775,16 +898,31 @@ module idecode(i_clk, i_reset, i_ce, i_stalled, end endgenerate assign o_valid = r_valid; - + // }}} assign o_I = { {(32-22){r_I[22]}}, r_I[21:0] }; // Make Verilator happy across all our various options + // {{{ + // verilator coverage_off // verilator lint_off UNUSED - wire [5:0] possibly_unused; - assign possibly_unused = { w_lock, w_ljmp, w_ljmp_dly, w_cis_ljmp, i_pc[1:0] }; + wire possibly_unused; + assign possibly_unused = &{ 1'b0, w_lock, w_ljmp, w_ljmp_dly, + insn_is_pipeable, w_cis_ljmp, i_pc[1:0] }; // verilator lint_on UNUSED + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // Formal properties for this module are maintained elsewhere -`endif +`endif // FORMAL +// }}} endmodule diff --git a/rtl/cpu/iscachable.cpp b/rtl/cpu/iscachable.cpp deleted file mode 100644 index 255ebd9..0000000 --- a/rtl/cpu/iscachable.cpp +++ /dev/null @@ -1,62 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// -// Filename: ./iscachable.v -// -// Project: ZBasic, a generic toplevel implementation using the full ZipCPU -// -// DO NOT EDIT THIS FILE! -// Computer Generated: This file is computer generated by AUTOFPGA. DO NOT EDIT. -// DO NOT EDIT THIS FILE! -// -// CmdLine: autofpga autofpga -o . clock.txt global.txt version.txt buserr.txt pic.txt pwrcount.txt gpio.txt rtclight.txt rtcdate.txt busconsole.txt bkram.txt flash.txt zipmaster.txt sdspi.txt mem_flash_bkram.txt mem_bkram_only.txt -// -// Creator: Dan Gisselquist, Ph.D. -// Gisselquist Technology, LLC -// -//////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2017-2020, Gisselquist Technology, LLC -// -// This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published -// by the Free Software Foundation, either version 3 of the License, or (at -// your option) any later version. -// -// This program is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program. (It's in the $(ROOT)/doc directory. Run make with no -// target there if the PDF file isn't present.) If not, see -// for a copy. -// -// License: GPL, v3, as defined and found on www.gnu.org, -// http://www.gnu.org/licenses/gpl.html -// -// -//////////////////////////////////////////////////////////////////////////////// -// -// -`default_nettype none -// -module iscachable(i_addr, o_cachable); - localparam AW = 23; - input wire [AW-1:0] i_addr; - output reg o_cachable; - - always @(*) - begin - o_cachable = 1'b0; - // Bus master: wb - // Bus master: wb_sio - // bkram - if ((i_addr[22:0] & 23'h780000) == 23'h300000) - o_cachable = 1'b1; - // flash - if ((i_addr[22:0] & 23'h400000) == 23'h400000) - o_cachable = 1'b1; - end - -endmodule diff --git a/rtl/cpu/iscachable.v b/rtl/cpu/iscachable.v index 4ae5c00..126a053 100644 --- a/rtl/cpu/iscachable.v +++ b/rtl/cpu/iscachable.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: ./iscachable.v -// +// {{{ // Project: ZBasic, a generic toplevel implementation using the full ZipCPU // // DO NOT EDIT THIS FILE! @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,20 +31,22 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} `default_nettype none // -module iscachable(i_addr, o_cachable); - localparam AW = 23; - input wire [AW-1:0] i_addr; - output reg o_cachable; +module iscachable( + // {{{ + input wire [25-1:0] i_addr, + output reg o_cachable + // }}} + ); always @(*) begin @@ -52,10 +54,10 @@ module iscachable(i_addr, o_cachable); // Bus master: wb // Bus master: wb_sio // bkram - if ((i_addr[22:0] & 23'h780000) == 23'h300000) + if ((i_addr[24:0] & 25'h1e00000) == 25'h0c00000) o_cachable = 1'b1; // flash - if ((i_addr[22:0] & 23'h400000) == 23'h400000) + if ((i_addr[24:0] & 25'h1000000) == 25'h1000000) o_cachable = 1'b1; end diff --git a/rtl/cpu/memops.v b/rtl/cpu/memops.v index 39c92cf..bc0ca64 100644 --- a/rtl/cpu/memops.v +++ b/rtl/cpu/memops.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: memops.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: A memory unit to support a CPU. @@ -11,18 +11,19 @@ // before it completes the last one. Unpredictable results might then // occurr. // -// 20150919 -- Added support for handling BUS ERR's (i.e., the WB -// error signal). +// BIG ENDIAN +// Note that this core assumes a big endian bus, with the MSB +// of the bus word being the least bus address // // Creator: Dan Gisselquist, Ph.D. // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -35,79 +36,114 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module memops(i_clk, i_reset, i_stb, i_lock, - i_op, i_addr, i_data, i_oreg, - o_busy, o_valid, o_err, o_wreg, o_result, - o_wb_cyc_gbl, o_wb_cyc_lcl, - o_wb_stb_gbl, o_wb_stb_lcl, - o_wb_we, o_wb_addr, o_wb_data, o_wb_sel, - i_wb_stall, i_wb_ack, i_wb_err, i_wb_data - ); - parameter ADDRESS_WIDTH=30; - parameter [0:0] IMPLEMENT_LOCK=1'b1, - WITH_LOCAL_BUS=1'b1, - OPT_ALIGNMENT_ERR=1'b1, - OPT_ZERO_ON_IDLE=1'b0; - localparam AW=ADDRESS_WIDTH; - input wire i_clk, i_reset; - input wire i_stb, i_lock; - // CPU interface - input wire [2:0] i_op; - input wire [31:0] i_addr; - input wire [31:0] i_data; - input wire [4:0] i_oreg; - // CPU outputs - output wire o_busy; - output reg o_valid; - output reg o_err; - output reg [4:0] o_wreg; - output reg [31:0] o_result; - // Wishbone outputs - output wire o_wb_cyc_gbl; - output reg o_wb_stb_gbl; - output wire o_wb_cyc_lcl; - output reg o_wb_stb_lcl; - output reg o_wb_we; - output reg [(AW-1):0] o_wb_addr; - output reg [31:0] o_wb_data; - output reg [3:0] o_wb_sel; - // Wishbone inputs - input wire i_wb_stall, i_wb_ack, i_wb_err; - input wire [31:0] i_wb_data; - - reg misaligned; +// }}} +module memops #( + // {{{ + parameter ADDRESS_WIDTH=30, + parameter DATA_WIDTH=32, // CPU's register width + parameter BUS_WIDTH=32, + parameter [0:0] OPT_LOCK=1'b1, + WITH_LOCAL_BUS=1'b1, + OPT_ALIGNMENT_ERR=1'b1, + OPT_LOWPOWER=1'b0, + OPT_LITTLE_ENDIAN = 1'b0, + localparam AW=ADDRESS_WIDTH +`ifdef FORMAL + , parameter F_LGDEPTH = 2 +`endif + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // CPU interface + // {{{ + input wire i_stb, i_lock, + input wire [2:0] i_op, + input wire [31:0] i_addr, + input wire [DATA_WIDTH-1:0] i_data, + input wire [4:0] i_oreg, + // CPU outputs + output wire o_busy, + output reg o_rdbusy, + output reg o_valid, + output reg o_err, + output reg [4:0] o_wreg, + output reg [DATA_WIDTH-1:0] o_result, + // }}} + // Wishbone + // {{{ + output wire o_wb_cyc_gbl, + output wire o_wb_cyc_lcl, + output reg o_wb_stb_gbl, + output reg o_wb_stb_lcl, + output reg o_wb_we, + output reg [AW-1:0] o_wb_addr, + output reg [BUS_WIDTH-1:0] o_wb_data, + output reg [BUS_WIDTH/8-1:0] o_wb_sel, + // Wishbone inputs + input wire i_wb_stall, i_wb_ack, i_wb_err, + input wire [BUS_WIDTH-1:0] i_wb_data + // }}} + // }}} + ); + // Declarations + // {{{ + localparam WBLSB = $clog2(BUS_WIDTH/8); + + wire misaligned; + reg r_wb_cyc_gbl, r_wb_cyc_lcl; + reg [2+WBLSB-1:0] r_op; + wire lock_gbl, lock_lcl; + wire lcl_bus, gbl_stb, lcl_stb; + + reg [BUS_WIDTH/8-1:0] oword_sel; + wire [BUS_WIDTH/8-1:0] pre_sel; + wire [BUS_WIDTH-1:0] pre_result; + + wire [1:0] oshift2; + wire [WBLSB-1:0] oshift; + + // }}} + + // misaligned + // {{{ generate if (OPT_ALIGNMENT_ERR) begin : GENERATE_ALIGNMENT_ERR + reg r_misaligned; + always @(*) casez({ i_op[2:1], i_addr[1:0] }) - 4'b01?1: misaligned = i_stb; // Words must be halfword aligned - 4'b0110: misaligned = i_stb; // Words must be word aligned - 4'b10?1: misaligned = i_stb; // Halfwords must be aligned - // 4'b11??: misaligned <= 1'b0; Byte access are never misaligned - default: misaligned = 1'b0; + 4'b01?1: r_misaligned = i_stb; // Words must be halfword aligned + 4'b0110: r_misaligned = i_stb; // Words must be word aligned + 4'b10?1: r_misaligned = i_stb; // Halfwords must be aligned + // 4'b11??: r_misaligned <= 1'b0; Byte access are never misaligned + default: r_misaligned = 1'b0; endcase + + assign misaligned = r_misaligned; end else - always @(*) misaligned = 1'b0; + assign misaligned = 1'b0; endgenerate + // }}} - reg r_wb_cyc_gbl, r_wb_cyc_lcl; - wire gbl_stb, lcl_stb; - assign lcl_stb = (i_stb)&&(WITH_LOCAL_BUS!=0)&&(i_addr[31:24]==8'hff) - &&(!misaligned); - assign gbl_stb = (i_stb)&&((WITH_LOCAL_BUS==0)||(i_addr[31:24]!=8'hff)) - &&(!misaligned); + // lcl_stb, gbl_stb + // {{{ + assign lcl_bus = (WITH_LOCAL_BUS)&&(i_addr[31:24]==8'hff); + assign lcl_stb = (i_stb)&&( lcl_bus)&&(!misaligned); + assign gbl_stb = (i_stb)&&(!lcl_bus)&&(!misaligned); + // }}} + // r_wb_cyc_gbl, r_wb_cyc_lcl + // {{{ initial r_wb_cyc_gbl = 1'b0; initial r_wb_cyc_lcl = 1'b0; always @(posedge i_clk) @@ -127,31 +163,87 @@ module memops(i_clk, i_reset, i_stb, i_lock, r_wb_cyc_lcl <= (lcl_stb); r_wb_cyc_gbl <= (gbl_stb); end + // }}} + + // o_wb_stb_gbl + // {{{ initial o_wb_stb_gbl = 1'b0; always @(posedge i_clk) if (i_reset) o_wb_stb_gbl <= 1'b0; else if ((i_wb_err)&&(r_wb_cyc_gbl)) o_wb_stb_gbl <= 1'b0; + else if (gbl_stb) + o_wb_stb_gbl <= 1'b1; else if (o_wb_cyc_gbl) o_wb_stb_gbl <= (o_wb_stb_gbl)&&(i_wb_stall); - else - // Grab wishbone on any new transaction to the gbl bus - o_wb_stb_gbl <= (gbl_stb); + // }}} + // o_wb_stb_lcl + // {{{ initial o_wb_stb_lcl = 1'b0; always @(posedge i_clk) if (i_reset) o_wb_stb_lcl <= 1'b0; else if ((i_wb_err)&&(r_wb_cyc_lcl)) o_wb_stb_lcl <= 1'b0; + else if (lcl_stb) + o_wb_stb_lcl <= 1'b1; else if (o_wb_cyc_lcl) o_wb_stb_lcl <= (o_wb_stb_lcl)&&(i_wb_stall); - else - // Grab wishbone on any new transaction to the lcl bus - o_wb_stb_lcl <= (lcl_stb); + // }}} + + // o_wb_we, o_wb_data, o_wb_sel + // {{{ + always @(*) + begin + oword_sel = 0; + + casez({ OPT_LITTLE_ENDIAN, i_op[2:1], i_addr[1:0] }) + 5'b00???: oword_sel[3:0] = 4'b1111; + 5'b0100?: oword_sel[3:0] = 4'b1100; + 5'b0101?: oword_sel[3:0] = 4'b0011; + 5'b01100: oword_sel[3:0] = 4'b1000; + 5'b01101: oword_sel[3:0] = 4'b0100; + 5'b01110: oword_sel[3:0] = 4'b0010; + 5'b01111: oword_sel[3:0] = 4'b0001; + // + // verilator coverage_off + 5'b10???: oword_sel[3:0] = 4'b1111; + 5'b1100?: oword_sel[3:0] = 4'b0011; + 5'b1101?: oword_sel[3:0] = 4'b1100; + 5'b11100: oword_sel[3:0] = 4'b0001; + 5'b11101: oword_sel[3:0] = 4'b0010; + 5'b11110: oword_sel[3:0] = 4'b0100; + 5'b11111: oword_sel[3:0] = 4'b1000; + // verilator coverage_on + // + default: oword_sel[3:0] = 4'b1111; + endcase + end + + // pre_sel + // {{{ + generate if (BUS_WIDTH == 32) + begin : COPY_PRESEL + assign pre_sel = oword_sel; + end else if (OPT_LITTLE_ENDIAN) + begin : GEN_LILPRESEL + wire [WBLSB-3:0] shift; + + assign shift = i_addr[WBLSB-1:2]; + assign pre_sel = oword_sel << (4 * i_addr[WBLSB-1:2]); + end else begin : GEN_PRESEL + wire [WBLSB-3:0] shift; + + assign shift = {(WBLSB-2){1'b1}} ^ i_addr[WBLSB-1:2]; + assign pre_sel = oword_sel << (4 * shift); + end endgenerate + // }}} + + assign oshift = i_addr[WBLSB-1:0]; + assign oshift2 = i_addr[1:0]; - reg [3:0] r_op; initial o_wb_we = 1'b0; initial o_wb_data = 0; initial o_wb_sel = 0; @@ -159,44 +251,67 @@ module memops(i_clk, i_reset, i_stb, i_lock, if (i_stb) begin o_wb_we <= i_op[0]; - if (OPT_ZERO_ON_IDLE) + if (OPT_LOWPOWER) begin - casez({ i_op[2:1], i_addr[1:0] }) - 4'b100?: o_wb_data <= { i_data[15:0], 16'h00 }; - 4'b101?: o_wb_data <= { 16'h00, i_data[15:0] }; - 4'b1100: o_wb_data <= { i_data[7:0], 24'h00 }; - 4'b1101: o_wb_data <= { 8'h00, i_data[7:0], 16'h00 }; - 4'b1110: o_wb_data <= { 16'h00, i_data[7:0], 8'h00 }; - 4'b1111: o_wb_data <= { 24'h00, i_data[7:0] }; - default: o_wb_data <= i_data; - endcase + if (lcl_bus) + begin + // {{{ + o_wb_data <= 0; + casez({ OPT_LITTLE_ENDIAN, i_op[2:1] }) + 3'b010: o_wb_data[31:0] <= { i_data[15:0], {(16){1'b0}} } >> (8*oshift2); + 3'b011: o_wb_data[31:0] <= { i_data[ 7:0], {(24){1'b0}} } >> (8*oshift2); + 3'b00?: o_wb_data[31:0] <= i_data[31:0]; + // + // verilator coverage_off + 3'b110: o_wb_data <= { {(BUS_WIDTH-16){1'b0}}, i_data[15:0] } << (8*oshift2); + 3'b111: o_wb_data <= { {(BUS_WIDTH-8){1'b0}}, i_data[ 7:0] } << (8*oshift2); + 3'b10?: o_wb_data <= { {(BUS_WIDTH-32){1'b0}}, i_data[31:0] } << (8*oshift2); + // verilator coverage_on + // + endcase + // }}} + end else begin + // {{{ + casez({ OPT_LITTLE_ENDIAN, i_op[2:1] }) + 3'b010: o_wb_data <= { i_data[15:0], {(BUS_WIDTH-16){1'b0}} } >> (8*oshift); + 3'b011: o_wb_data <= { i_data[ 7:0], {(BUS_WIDTH- 8){1'b0}} } >> (8*oshift); + 3'b00?: o_wb_data <= { i_data[31:0], {(BUS_WIDTH-32){1'b0}} } >> (8*oshift); + // + 3'b110: o_wb_data <= { {(BUS_WIDTH-16){1'b0}}, i_data[15:0] } << (8*oshift); + 3'b111: o_wb_data <= { {(BUS_WIDTH-8){1'b0}}, i_data[ 7:0] } << (8*oshift); + 3'b10?: o_wb_data <= { {(BUS_WIDTH-32){1'b0}}, i_data[31:0] } << (8*oshift); + // + endcase + // }}} + end end else - casez({ i_op[2:1], i_addr[1:0] }) - 4'b10??: o_wb_data <= { (2){ i_data[15:0] } }; - 4'b11??: o_wb_data <= { (4){ i_data[7:0] } }; - default: o_wb_data <= i_data; + casez({ i_op[2:1] }) + 2'b10: o_wb_data <= { (BUS_WIDTH/16){ i_data[15:0] } }; + 2'b11: o_wb_data <= { (BUS_WIDTH/ 8){ i_data[7:0] } }; + default: o_wb_data <= {(BUS_WIDTH/32){i_data}}; endcase - o_wb_addr <= i_addr[(AW+1):2]; - casez({ i_op[2:1], i_addr[1:0] }) - 4'b01??: o_wb_sel <= 4'b1111; - 4'b100?: o_wb_sel <= 4'b1100; - 4'b101?: o_wb_sel <= 4'b0011; - 4'b1100: o_wb_sel <= 4'b1000; - 4'b1101: o_wb_sel <= 4'b0100; - 4'b1110: o_wb_sel <= 4'b0010; - 4'b1111: o_wb_sel <= 4'b0001; - default: o_wb_sel <= 4'b1111; - endcase - r_op <= { i_op[2:1] , i_addr[1:0] }; - end else if ((OPT_ZERO_ON_IDLE)&&(!o_wb_cyc_gbl)&&(!o_wb_cyc_lcl)) + if (lcl_bus) + begin + o_wb_addr <= i_addr[2 +: (AW+2>32 ? (32-2) : AW)]; + o_wb_sel <= oword_sel; + end else begin + o_wb_addr <= i_addr[WBLSB +: (AW+WBLSB>32 ? (32-WBLSB) : AW)]; + o_wb_sel <= pre_sel; + end + + r_op <= { i_op[2:1] , i_addr[WBLSB-1:0] }; + end else if ((OPT_LOWPOWER)&&(!o_wb_cyc_gbl)&&(!o_wb_cyc_lcl)) begin o_wb_we <= 1'b0; o_wb_addr <= 0; - o_wb_data <= 32'h0; - o_wb_sel <= 4'h0; + o_wb_data <= {(BUS_WIDTH){1'b0}}; + o_wb_sel <= {(BUS_WIDTH/8){1'b0}}; end + // }}} + // o_valid + // {{{ initial o_valid = 1'b0; always @(posedge i_clk) if (i_reset) @@ -204,6 +319,10 @@ module memops(i_clk, i_reset, i_stb, i_lock, else o_valid <= (((o_wb_cyc_gbl)||(o_wb_cyc_lcl)) &&(i_wb_ack)&&(!o_wb_we)); + // }}} + + // o_err + // {{{ initial o_err = 1'b0; always @(posedge i_clk) if (i_reset) @@ -214,81 +333,161 @@ module memops(i_clk, i_reset, i_stb, i_lock, o_err <= misaligned; else o_err <= 1'b0; + // }}} assign o_busy = (r_wb_cyc_gbl)||(r_wb_cyc_lcl); + // o_rdbusy + // {{{ + initial o_rdbusy = 1'b0; + always @(posedge i_clk) + if (i_reset|| ((o_wb_cyc_gbl || o_wb_cyc_lcl)&&(i_wb_err || i_wb_ack))) + o_rdbusy <= 1'b0; + else if (i_stb && !i_op[0] && !misaligned) + o_rdbusy <= 1'b1; + else if (o_valid) + o_rdbusy <= 1'b0; + // }}} + always @(posedge i_clk) if (i_stb) o_wreg <= i_oreg; + + // o_result + // {{{ + generate if (OPT_LITTLE_ENDIAN) + begin : LILEND_RESULT + + assign pre_result = i_wb_data >> (8*r_op[$clog2(BUS_WIDTH/8)-1:0]); + + end else begin : BIGEND_RESULT + + assign pre_result = i_wb_data << (8*r_op[$clog2(BUS_WIDTH/8)-1:0]); + + end endgenerate + always @(posedge i_clk) - if ((OPT_ZERO_ON_IDLE)&&(!i_wb_ack)) + if ((OPT_LOWPOWER)&&(!i_wb_ack)) o_result <= 32'h0; - else begin - casez(r_op) - 4'b01??: o_result <= i_wb_data; - 4'b100?: o_result <= { 16'h00, i_wb_data[31:16] }; - 4'b101?: o_result <= { 16'h00, i_wb_data[15: 0] }; - 4'b1100: o_result <= { 24'h00, i_wb_data[31:24] }; - 4'b1101: o_result <= { 24'h00, i_wb_data[23:16] }; - 4'b1110: o_result <= { 24'h00, i_wb_data[15: 8] }; - 4'b1111: o_result <= { 24'h00, i_wb_data[ 7: 0] }; - default: o_result <= i_wb_data; + else if (o_wb_cyc_lcl && (BUS_WIDTH != 32)) + begin + // The Local bus is naturally (and only) a 32-bit bus + casez({ OPT_LITTLE_ENDIAN, r_op[WBLSB +: 2], r_op[1:0] }) + 5'b?01??: o_result <= i_wb_data[31:0]; + // + // Big endian + 5'b0100?: o_result <= { 16'h00, i_wb_data[31:16] }; + 5'b0101?: o_result <= { 16'h00, i_wb_data[15: 0] }; + 5'b01100: o_result <= { 24'h00, i_wb_data[31:24] }; + 5'b01101: o_result <= { 24'h00, i_wb_data[23:16] }; + 5'b01110: o_result <= { 24'h00, i_wb_data[15: 8] }; + 5'b01111: o_result <= { 24'h00, i_wb_data[ 7: 0] }; + // + // Little endian : Same bus result, just grab a different bits + // from the bus return to send back to the CPU. + // verilator coverage_off + 5'b1100?: o_result <= { 16'h00, i_wb_data[15: 0] }; + 5'b1101?: o_result <= { 16'h00, i_wb_data[31:16] }; + 5'b11100: o_result <= { 24'h00, i_wb_data[ 7: 0] }; + 5'b11101: o_result <= { 24'h00, i_wb_data[15: 8] }; + 5'b11110: o_result <= { 24'h00, i_wb_data[23:16] }; + 5'b11111: o_result <= { 24'h00, i_wb_data[31:24] }; + // verilator coverage_on + default: o_result <= i_wb_data[31:0]; + endcase + end else begin + casez({ OPT_LITTLE_ENDIAN, r_op[$clog2(BUS_WIDTH/8) +: 2] }) + // Word + // + // Big endian + 3'b00?: o_result <= pre_result[BUS_WIDTH-1:BUS_WIDTH-32]; + 3'b010: o_result <= { 16'h00, pre_result[BUS_WIDTH-1:BUS_WIDTH-16] }; + 3'b011: o_result <= { 24'h00, pre_result[BUS_WIDTH-1:BUS_WIDTH-8] }; + // + // Little endian : Same bus result, just grab a different bits + // from the bus return to send back to the CPU. + // verilator coverage_off + 3'b10?: o_result <= pre_result[31: 0]; + 3'b110: o_result <= { 16'h00, pre_result[15: 0] }; + 3'b111: o_result <= { 24'h00, pre_result[ 7: 0] }; + // verilator coverage_on + // + // Just to have an (unused) default + // default: o_result <= pre_result[31:0]; (Messes w/ coverage) endcase end + // }}} - reg lock_gbl, lock_lcl; - + // lock_gbl and lock_lcl + // {{{ generate - if (IMPLEMENT_LOCK != 0) + if (OPT_LOCK) begin + // {{{ + reg r_lock_gbl, r_lock_lcl; - initial lock_gbl = 1'b0; - initial lock_lcl = 1'b0; + initial r_lock_gbl = 1'b0; + initial r_lock_lcl = 1'b0; always @(posedge i_clk) if (i_reset) begin - lock_gbl <= 1'b0; - lock_lcl <= 1'b0; + r_lock_gbl <= 1'b0; + r_lock_lcl <= 1'b0; end else if (((i_wb_err)&&((r_wb_cyc_gbl)||(r_wb_cyc_lcl))) ||(misaligned)) begin // Kill the lock if // there's a bus error, or // User requests a misaligned memory op - lock_gbl <= 1'b0; - lock_lcl <= 1'b0; + r_lock_gbl <= 1'b0; + r_lock_lcl <= 1'b0; end else begin // Kill the lock if // i_lock goes down // User starts on the global bus, then switches // to local or vice versa - lock_gbl <= (i_lock)&&((r_wb_cyc_gbl)||(lock_gbl)) + r_lock_gbl <= (i_lock)&&((r_wb_cyc_gbl)||(lock_gbl)) &&(!lcl_stb); - lock_lcl <= (i_lock)&&((r_wb_cyc_lcl)||(lock_lcl)) + r_lock_lcl <= (i_lock)&&((r_wb_cyc_lcl)||(lock_lcl)) &&(!gbl_stb); end + assign lock_gbl = r_lock_gbl; + assign lock_lcl = r_lock_lcl; + assign o_wb_cyc_gbl = (r_wb_cyc_gbl)||(lock_gbl); assign o_wb_cyc_lcl = (r_wb_cyc_lcl)||(lock_lcl); - end else begin - + // }}} + end else begin : NO_LOCK + // {{{ assign o_wb_cyc_gbl = (r_wb_cyc_gbl); assign o_wb_cyc_lcl = (r_wb_cyc_lcl); - always @(*) - { lock_gbl, lock_lcl } = 2'b00; + assign { lock_gbl, lock_lcl } = 2'b00; // Make verilator happy // verilator lint_off UNUSED wire [2:0] lock_unused; assign lock_unused = { i_lock, lock_gbl, lock_lcl }; // verilator lint_on UNUSED - + // }}} end endgenerate + // }}} + +`ifdef VERILATOR + always @(posedge i_clk) + if ((r_wb_cyc_gbl)||(r_wb_cyc_lcl)) + assert(!i_stb); +`endif + // Make verilator happy + // {{{ + // verilator coverage_off // verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, pre_result }; generate if (AW < 22) begin : TOO_MANY_ADDRESS_BITS @@ -297,8 +496,19 @@ module memops(i_clk, i_reset, i_stb, i_lock, end endgenerate // verilator lint_on UNUSED - + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // Formal properties for this module are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/mpyop.v b/rtl/cpu/mpyop.v index 429fb20..856ff77 100644 --- a/rtl/cpu/mpyop.v +++ b/rtl/cpu/mpyop.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: mpyop.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: This code has been pulled from the cpuops.v file so as to @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,32 +31,43 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module mpyop(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_valid, o_busy, o_result, o_hi); - // The following parameter selects which multiply algorithm we use. - // Timing performance is strictly dependent upon it. - parameter IMPLEMENT_MPY = 1; - input wire i_clk, i_reset, i_stb; - input wire [1:0] i_op; // 2'b00=MPY, 2'b10=MPYUHI, 2'b11=MPYSHI - input wire [31:0] i_a, i_b; - output wire o_valid; // True if we'll be valid on the next clock; - output wire o_busy; // The multiply is busy if true - output wire [63:0] o_result; // Where we dump the multiply result - output reg o_hi; // Return the high half of the multiply +// }}} +module mpyop #( + // {{{ + // The following parameter selects which multiply algorithm we + // use. Timing performance is strictly dependent upon it. + parameter OPT_MPY = 1, + parameter [0:0] OPT_LOWPOWER = 1'b0 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, i_stb, + // + // Three types of multiply operations. + // 2'b00: 32x32 multiply, returning the low order 32 bits + // 2'b10: 32x32 unsigned multiply, returning upper 32 bits + // 2'b11: 32x32 signed multiply, returning upper 32 bits + input wire [1:0] i_op, + input wire [31:0] i_a, i_b, + output wire o_valid, // True if the result is valid + output wire o_busy, // + output wire [63:0] o_result, // multiply result + output wire o_hi // Return the high half of mpy + // }}} + ); // A 4-way multiplexer can be done in one 6-LUT. // A 16-way multiplexer can therefore be done in 4x 6-LUT's with - // the Xilinx multiplexer fabric that follows. + // the Xilinx multiplexer fabric that follows. // Given that we wish to apply this multiplexer approach to 33-bits, // this will cost a minimum of 132 6-LUTs. @@ -65,95 +76,142 @@ module mpyop(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_valid, o_busy, o_result, o_ // o_busy // o_done generate - if (IMPLEMENT_MPY == 0) + if (OPT_MPY == 0) begin : MPYNONE // No multiply support. - + // {{{ assign o_result = 64'h00; assign o_busy = 1'b0; assign o_valid = i_stb; - always @(*) o_hi = 1'b0; // Not needed + assign o_hi = 1'b0; // Not needed `ifdef VERILATOR + // verilator coverage_off // verilator lint_off UNUSED - wire [32+32+5-1:0] mpy_unused; - assign mpy_unused = { i_clk, i_reset, i_stb, i_op, i_a, i_b }; + wire mpy_unused; + assign mpy_unused = &{ 1'b0, i_clk, i_reset, i_stb, i_op, i_a, i_b }; // verilator lint_on UNUSED + // verilator coverage_on `endif + // }}} end else begin : IMPY - if (IMPLEMENT_MPY == 1) + if (OPT_MPY == 1) begin : MPY1CK // Our single clock option (no extra clocks) - + // {{{ wire signed [63:0] w_mpy_a_input, w_mpy_b_input; assign w_mpy_a_input = {{(32){(i_a[31])&(i_op[0])}},i_a[31:0]}; assign w_mpy_b_input = {{(32){(i_b[31])&(i_op[0])}},i_b[31:0]}; - assign o_result = w_mpy_a_input * w_mpy_b_input; + assign o_result = (OPT_LOWPOWER && !i_stb) ? 0 : (w_mpy_a_input * w_mpy_b_input); assign o_busy = 1'b0; - assign o_valid = 1'b0; - always @(*) o_hi = i_op[1]; + assign o_valid = i_stb; + assign o_hi = i_op[1]; `ifdef VERILATOR + // verilator coverage_off // verilator lint_off UNUSED - wire [3:0] mpy_unused; - assign mpy_unused = { i_clk, i_reset, i_stb, i_op[1] }; + wire mpy_unused; + assign mpy_unused = &{ 1'b0, i_clk, i_reset, i_stb, i_op[1] }; // verilator lint_on UNUSED + // verilator coverage_on `endif - + // }}} end else begin: MPN1 - if (IMPLEMENT_MPY == 2) + if (OPT_MPY == 2) begin : MPY2CK // Our two clock option (ALU must pause for 1 clock) + // {{{ + // Declarations + // {{{ reg signed [63:0] r_mpy_a_input, r_mpy_b_input; + reg mpypipe, r_hi; + // }}} + + // r_mpy_?_input: Register the inputs + // {{{ always @(posedge i_clk) + if (!OPT_LOWPOWER || i_stb) begin r_mpy_a_input <={{(32){(i_a[31])&(i_op[0])}},i_a[31:0]}; r_mpy_b_input <={{(32){(i_b[31])&(i_op[0])}},i_b[31:0]}; + end else begin + r_mpy_a_input <= 0; + r_mpy_b_input <= 0; end + // }}} assign o_result = r_mpy_a_input * r_mpy_b_input; assign o_busy = 1'b0; - reg mpypipe; + // mpypipe + // {{{ initial mpypipe = 1'b0; always @(posedge i_clk) - if (i_reset) - mpypipe <= 1'b0; - else - mpypipe <= (i_stb); + if (i_reset) + mpypipe <= 1'b0; + else + mpypipe <= (i_stb); + // }}} assign o_valid = mpypipe; // this_is_a_multiply_op; + + // o_hi + // {{{ always @(posedge i_clk) if (i_stb) - o_hi <= i_op[1]; + r_hi <= i_op[1]; + assign o_hi = r_hi; + // }}} + // }}} end else begin : MPN2 - if (IMPLEMENT_MPY == 3) + if (OPT_MPY == 3) begin : MPY3CK // Our three clock option (ALU pauses for 2 clocks) + // {{{ + + // Declarations + // {{{ reg signed [63:0] r_smpy_result; reg [63:0] r_umpy_result; reg signed [31:0] r_mpy_a_input, r_mpy_b_input; reg [1:0] mpypipe; reg [1:0] r_sgn; + reg r_hi; + // }}} + // mpypipe (FSM state) + // {{{ initial mpypipe = 2'b0; always @(posedge i_clk) - if (i_reset) - mpypipe <= 2'b0; - else + if (i_reset) + mpypipe <= 2'b0; + else mpypipe <= { mpypipe[0], i_stb }; + // }}} + + // First clock : register r_mpy_?_input, r_sgn + // {{{ + always @(posedge i_clk) + r_sgn <= { r_sgn[0], + (i_op[0] && (!OPT_LOWPOWER || i_stb)) }; - // First clock always @(posedge i_clk) + if (!OPT_LOWPOWER || i_stb) begin r_mpy_a_input <= i_a[31:0]; r_mpy_b_input <= i_b[31:0]; - r_sgn <= { r_sgn[0], i_op[0] }; + end else begin + r_mpy_a_input <= 0; + r_mpy_b_input <= 0; end + // }}} - // Second clock + // Second clock : perform the multiply + // {{{ `ifdef VERILATOR + // Veri1ator only implementation + // {{{ wire signed [63:0] s_mpy_a_input, s_mpy_b_input; wire [63:0] u_mpy_a_input, u_mpy_b_input; @@ -162,45 +220,64 @@ module mpyop(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_valid, o_busy, o_result, o_ assign u_mpy_a_input = {32'h00,r_mpy_a_input}; assign u_mpy_b_input = {32'h00,r_mpy_b_input}; always @(posedge i_clk) + if (!OPT_LOWPOWER || mpypipe[0]) r_smpy_result <= s_mpy_a_input * s_mpy_b_input; always @(posedge i_clk) + if (!OPT_LOWPOWER || mpypipe[0]) r_umpy_result <= u_mpy_a_input * u_mpy_b_input; + // }}} `else - + // Synthesis implementation + // {{{ wire [31:0] u_mpy_a_input, u_mpy_b_input; assign u_mpy_a_input = r_mpy_a_input; assign u_mpy_b_input = r_mpy_b_input; always @(posedge i_clk) + if (!OPT_LOWPOWER || mpypipe[0]) r_smpy_result <= r_mpy_a_input * r_mpy_b_input; always @(posedge i_clk) + if (!OPT_LOWPOWER || mpypipe[0]) r_umpy_result <= u_mpy_a_input * u_mpy_b_input; + // }}} `endif always @(posedge i_clk) if (i_stb) - o_hi <= i_op[1]; + r_hi <= i_op[1]; + + assign o_hi = r_hi; assign o_busy = mpypipe[0]; assign o_result = (r_sgn[1])?r_smpy_result:r_umpy_result; assign o_valid = mpypipe[1]; + // }}} - // Results are then set on the third clock + // Results are then available and registered on the third clock + // }}} end else begin : MPN3 - if (IMPLEMENT_MPY == 4) - begin : MPY4CK // The three clock option + if (OPT_MPY == 4) + begin : MPY4CK // The four clock option, polynomial multiplication + // {{{ + // Declarations + // {{{ reg [63:0] r_mpy_result; reg [31:0] r_mpy_a_input, r_mpy_b_input; - reg r_mpy_signed; + reg r_mpy_signed, r_hi; reg [2:0] mpypipe; + reg [31:0] pp_f, pp_l; // F and L from FOIL + reg [32:0] pp_oi; // The O and I from FOIL + reg [32:0] pp_s; + // }}} - // First clock, latch in the inputs + // First clock, latch in the inputs : mpypipe, r_mpy_?_input + // {{{ initial mpypipe = 3'b0; always @(posedge i_clk) begin // mpypipe indicates we have a multiply in the // pipeline. In this case, the multiply - // pipeline is a two stage pipeline, so we need + // pipeline is a two stage pipeline, so we need // two bits in the pipe. if (i_reset) mpypipe <= 3'h0; @@ -225,15 +302,23 @@ module mpyop(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_valid, o_busy, o_result, o_ r_mpy_signed <= i_op[0]; if (i_stb) - o_hi <= i_op[1]; + r_hi <= i_op[1]; + else if (OPT_LOWPOWER) + begin + r_mpy_a_input <= 0; + r_mpy_b_input <= 0; + r_mpy_signed <= 0; + end end + // }}} + assign o_hi = r_hi; assign o_busy = |mpypipe[1:0]; assign o_valid = mpypipe[2]; - // Second clock, do the multiplies, get the "partial - // products". Here, we break our input up into two - // halves, + // Second clock, do the multiplies, get the "partial products". + // {{{ + // Here, we break our input up into two halves, // // A = (2^16 ah + al) // B = (2^16 bh + bl) @@ -264,10 +349,8 @@ module mpyop(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_valid, o_busy, o_result, o_ // +(al*bl) // - 2^31 (2^16 bh+bl + 2^16 ah+al + 2^31) // - reg [31:0] pp_f, pp_l; // F and L from FOIL - reg [32:0] pp_oi; // The O and I from FOIL - reg [32:0] pp_s; always @(posedge i_clk) + if (!OPT_LOWPOWER || mpypipe[0]) begin pp_f<=r_mpy_a_input[31:16]*r_mpy_b_input[31:16]; pp_oi<=r_mpy_a_input[31:16]*r_mpy_b_input[15: 0] @@ -281,9 +364,12 @@ module mpyop(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_valid, o_busy, o_result, o_ else pp_s <= 33'h0; end + // }}} - // Third clock, add the results and produce a product + // Third clock, add the results and get a product: r_mpy_result + // {{{ always @(posedge i_clk) + if (!OPT_LOWPOWER || mpypipe[1]) begin r_mpy_result[15:0] <= pp_l[15:0]; r_mpy_result[63:16] <= @@ -292,15 +378,27 @@ module mpyop(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_valid, o_busy, o_result, o_ + { pp_s, 15'h00 } + { pp_f, 16'h00 }; end + // }}} assign o_result = r_mpy_result; // Fourth clock -- results are clocked into writeback + // }}} end else begin : MPYSLOW + // {{{ + // Use an external multiply implementation, for when DSPs aren't + // available. + // + // Declarations + // {{{ + reg r_hi; + // verilator coverage_off // verilator lint_off UNUSED wire unused_aux; wire [65:0] full_result; // verilator lint_on UNUSED + // verilator coverage_on + // }}} slowmpy #(.LGNA(6), .NA(33)) slowmpyi(i_clk, i_reset, i_stb, { (i_op[0])&(i_a[31]), i_a }, @@ -311,8 +409,10 @@ module mpyop(i_clk,i_reset, i_stb, i_op, i_a, i_b, o_valid, o_busy, o_result, o_ always @(posedge i_clk) if (i_stb) - o_hi <= i_op[1]; + r_hi <= i_op[1]; + assign o_hi = r_hi; + // }}} end end end end end endgenerate // All possible multiply results have been determined diff --git a/rtl/cpu/pfcache.v b/rtl/cpu/pfcache.v index fcafb25..8f70c46 100644 --- a/rtl/cpu/pfcache.v +++ b/rtl/cpu/pfcache.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: pfcache.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: Keeping our CPU fed with instructions, at one per clock and @@ -34,11 +34,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -51,54 +51,77 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, - // i_early_branch, i_from_addr, - i_stall_n, i_pc, o_insn, o_pc, o_valid, - o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, - i_wb_stall, i_wb_ack, i_wb_err, i_wb_data, - o_illegal - ); - parameter LGCACHELEN = 12, ADDRESS_WIDTH=30, - LGLINES=8; // Log of the number of separate cache lines - localparam CACHELEN=(1<> (INSN_WIDTH*shift); + assign o_insn= shifted[INSN_WIDTH-1:0]; + + end else begin + + assign shifted = cache_word << (INSN_WIDTH*shift); + assign o_insn=shifted[BUS_WIDTH-1:BUS_WIDTH-INSN_WIDTH]; + + end + + // Verilator coverage_off + // Verilator lint_off UNUSED + wire unused_shift; + assign unused_shift = &{ 1'b0, shifted }; + // Verilator lint_on UNUSED + // Verilator coverage_on + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// // - ///////////////////////////////////////////////// + // Read the tag value associated with this cache line + // {{{ + //////////////////////////////////////////////////////////////////////// // // + assign pc_tag = i_pc[WBLSB+LS +: (AW-LS)]; + assign pc_line = i_pc[WBLSB+LS +: (CW-LS)]; + assign last_line = lastpc[WBLSB+LS +: (CW-LS)]; + // // Read the tag value associated with this i_pc value - initial tagvalipc = 0; always @(posedge i_clk) - tagvalipc <= cache_tags[i_pc[(CW+1):LS+2]]; + pc_tag_lookup <= { cache_tags[pc_line], pc_line }; + // tagvalipc <= cache_tags[i_pc[WBLSB + LS +: (CW-LS)]]; // // Read the tag value associated with the lastpc value, from what // i_pc was when we could not tell if this value was in our cache or // not, or perhaps from when we determined that i was not in the cache. - initial tagvallst = 0; + // initial tagvallst = 0; always @(posedge i_clk) - tagvallst <= cache_tags[lastpc[(CW+1):LS+2]]; + last_tag_lookup <= { cache_tags[last_line], last_line }; + // tagvallst <= cache_tags[lastpc[WBLSB + LS +: (CW-LS)]]; // Select from between these two values on the next clock - assign tagval = (isrc)?tagvalipc : tagvallst; + assign tag_lookup = (isrc)? pc_tag_lookup : last_tag_lookup; // i_pc will only increment when everything else isn't stalled, thus // we can set it without worrying about that. Doing this enables @@ -228,25 +291,24 @@ module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, if (w_advance) lastpc <= i_pc; - assign lasttag = lastpc[(AW+1):LS+2]; - - ///////////////////////////////////////////////// + assign lasttag = lastpc[WBLSB + LS +: (AW-LS)]; + // }}} + //////////////////////////////////////////////////////////////////////// // // Use the tag value to determine if our output instruction will be // valid. + // {{{ + //////////////////////////////////////////////////////////////////////// // - ///////////////////////////////////////////////// // - // - assign w_v_from_pc = ((i_pc[(AW+1):LS+2] == lasttag) - &&(tagval == i_pc[(AW+1):CW+2]) - &&(valid_mask[i_pc[(CW+1):LS+2]])); - assign w_v_from_last = ((tagval == lastpc[(AW+1):CW+2]) - &&(valid_mask[lastpc[(CW+1):LS+2]])); + assign w_v_from_pc = ((pc_tag == lasttag) &&(tag_lookup == pc_tag) + && valid_mask[pc_line]); + assign w_v_from_last = ((tag_lookup == lasttag) + &&(valid_mask[last_line])); initial delay = 2'h3; always @(posedge i_clk) - if ((i_reset)||(i_clear_cache)||(w_advance)) + if (i_reset || i_clear_cache || w_advance) begin // Source our valid signal from i_pc rvsrc <= 1'b1; @@ -254,7 +316,8 @@ module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, // we have an invalid result. This will give us time // to check the tag value of what's in the cache. delay <= 2'h2; - end else if ((!r_v)&&(!o_illegal)) begin + end else if (!r_v && !o_illegal) + begin // If we aren't sourcing our valid signal from the // i_pc clock, then we are sourcing it from the // lastpc clock (one clock later). If r_v still @@ -282,43 +345,36 @@ module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, delay <= 2'h2; end - wire w_invalidate_result; assign w_invalidate_result = (i_reset)||(i_clear_cache); - reg r_prior_illegal; - initial r_prior_illegal = 0; - initial r_new_request = 0; initial r_v_from_pc = 0; initial r_v_from_last = 0; always @(posedge i_clk) begin - r_new_request <= w_invalidate_result; r_v_from_pc <= (w_v_from_pc)&&(!w_invalidate_result) &&(!o_illegal); r_v_from_last <= (w_v_from_last)&&(!w_invalidate_result); - - r_prior_illegal <= (o_wb_cyc)&&(i_wb_err); end // Now use rvsrc to determine which of the two valid flags we'll be // using: r_v_from_pc (the i_pc address), or r_v_from_last (the lastpc // address) - assign r_v = ((rvsrc)?(r_v_from_pc):(r_v_from_last))&&(!r_new_request); - assign o_valid = (((rvsrc)?(r_v_from_pc):(r_v_from_last)) - ||(o_illegal)) - &&(!i_new_pc)&&(!r_prior_illegal); + assign r_v = ((rvsrc)?(r_v_from_pc):(r_v_from_last)); - ///////////////////////////////////////////////// + always @(*) + o_valid = r_v || o_illegal; + // }}} + //////////////////////////////////////////////////////////////////////// // // If the instruction isn't in our cache, then we need to load // a new cache line from memory. - // - ///////////////////////////////////////////////// + // {{{ + //////////////////////////////////////////////////////////////////////// // // initial needload = 1'b0; always @(posedge i_clk) - if ((i_clear_cache)||(o_wb_cyc)) + if (i_clear_cache || o_wb_cyc) needload <= 1'b0; else if ((w_advance)&&(!o_illegal)) needload <= 1'b0; @@ -327,8 +383,7 @@ module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, // Prevent us from reloading an illegal address // (i.e. one that produced a bus error) over and over // and over again - &&((!illegal_valid) - ||(lastpc[(AW+1):LS+2] != illegal_cache)); + &&(!illegal_valid ||(lasttag != illegal_cache)); // // Working from the rule that you want to keep complex logic out of @@ -359,7 +414,7 @@ module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, always @(posedge i_clk) if (!o_wb_cyc) bus_abort <= 1'b0; - else if ((i_clear_cache)||(i_new_pc)) + else if (i_clear_cache || i_new_pc) bus_abort <= 1'b1; // @@ -370,7 +425,7 @@ module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, initial o_wb_cyc = 1'b0; initial o_wb_stb = 1'b0; always @(posedge i_clk) - if ((i_reset)||(i_clear_cache)) + if (i_reset || i_clear_cache) begin o_wb_cyc <= 1'b0; o_wb_stb <= 1'b0; @@ -378,13 +433,13 @@ module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, begin if (i_wb_err) o_wb_stb <= 1'b0; - else if ((o_wb_stb)&&(!i_wb_stall)&&(last_addr)) + else if (o_wb_stb && !i_wb_stall && last_addr) o_wb_stb <= 1'b0; - if (((i_wb_ack)&&(last_ack))||(i_wb_err)) + if ((i_wb_ack && last_ack )|| i_wb_err) o_wb_cyc <= 1'b0; - end else if ((needload)&&(!i_new_pc)) + end else if (needload && !i_new_pc) begin o_wb_cyc <= 1'b1; o_wb_stb <= 1'b1; @@ -393,7 +448,7 @@ module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, // If we are reading from this cache line, then once we get the first // acknowledgement, this cache line has the new tag value always @(posedge i_clk) - if ((o_wb_cyc)&&(i_wb_ack)) + if (o_wb_cyc && i_wb_ack) cache_tags[o_wb_addr[(CW-1):LS]] <= o_wb_addr[(AW-1):CW]; @@ -402,10 +457,10 @@ module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, // RAM. initial wraddr = 0; always @(posedge i_clk) - if ((o_wb_cyc)&&(i_wb_ack)&&(!last_ack)) - wraddr <= wraddr + 1'b1; + if (o_wb_cyc && i_wb_ack && !last_ack) + wraddr[LS-1:0] <= wraddr[LS-1:0] + 1'b1; else if (!o_wb_cyc) - wraddr <= { lastpc[(CW+1):LS+2], {(LS){1'b0}} }; + wraddr <= { last_line, {(LS){1'b0}} }; // // The wishbone request address. This has meaning anytime o_wb_stb @@ -420,7 +475,7 @@ module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, if ((o_wb_stb)&&(!i_wb_stall)&&(!last_addr)) o_wb_addr[(LS-1):0] <= o_wb_addr[(LS-1):0]+1'b1; else if (!o_wb_cyc) - o_wb_addr <= { lastpc[(AW+1):LS+2], {(LS){1'b0}} }; + o_wb_addr <= { lasttag, {(LS){1'b0}} }; // Since it is impossible to initialize an array, our cache will start // up cache uninitialized. We'll also never get a valid ack without @@ -451,26 +506,25 @@ module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, valid_mask <= 0; svmask<= 1'b0; end else begin - svmask <= ((o_wb_cyc)&&(i_wb_ack)&&(last_ack)&&(!bus_abort)); + svmask <= (o_wb_cyc && i_wb_ack && last_ack && !bus_abort); if (svmask) - valid_mask[saddr] <= (!bus_abort); - if ((!o_wb_cyc)&&(needload)) - valid_mask[lastpc[(CW+1):LS+2]] <= 1'b0; + valid_mask[saddr] <= !bus_abort; + if (!o_wb_cyc && needload) + valid_mask[last_line] <= 1'b0; end always @(posedge i_clk) if ((o_wb_cyc)&&(i_wb_ack)) saddr <= wraddr[(CW-1):LS]; - - ///////////////////////////////////////////////// + // }}} + //////////////////////////////////////////////////////////////////////// // // Handle bus errors here. If a bus read request // returns an error, then we'll mark the entire // line as having a (valid) illegal value. - // - ///////////////////////////////////////////////// - // + // {{{ + //////////////////////////////////////////////////////////////////////// // // // @@ -485,19 +539,33 @@ module pfcache(i_clk, i_reset, i_new_pc, i_clear_cache, begin illegal_cache <= o_wb_addr[(AW-1):LS]; illegal_valid <= 1'b1; - end + end else if ((o_wb_cyc)&&(i_wb_ack)&&(last_ack)&&(!bus_abort) + &&(wraddr[(CW-1):LS] == illegal_cache[CW-1:LS])) + illegal_valid <= 1'b0; initial o_illegal = 1'b0; always @(posedge i_clk) - if ((i_reset)||(i_clear_cache)||(i_new_pc)) - o_illegal <= 1'b0; - else if ((o_illegal)||((o_valid)&&(i_stall_n))) + if (i_reset || i_clear_cache || i_new_pc) o_illegal <= 1'b0; - else - o_illegal <= (illegal_valid) - &&(illegal_cache == lastpc[(AW+1):LS+2]); - + // else if ((o_illegal)||((o_valid)&&(i_ready))) + // o_illegal <= 1'b0; + else if (!o_illegal) + begin + o_illegal <= (!i_wb_err)&&(illegal_valid)&&(!isrc) + &&(illegal_cache == lasttag); + end + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal property section +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // Formal properties for this module are maintained elsewhere `endif // FORMAL +// }}} endmodule diff --git a/rtl/cpu/pipefetch.v b/rtl/cpu/pipefetch.v deleted file mode 100644 index 9fcc857..0000000 --- a/rtl/cpu/pipefetch.v +++ /dev/null @@ -1,306 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// -// Filename: pipefetch.v -// -// Project: Zip CPU -- a small, lightweight, RISC CPU soft core -// -// Purpose: Keeping our CPU fed with instructions, at one per clock and -// with no stalls, can be quite a chore. Worse, the Wishbone -// takes a couple of cycles just to read one instruction from -// the bus. However, if we use pipeline accesses to the Wishbone -// bus, then we can read more and faster. Further, if we cache -// these results so that we have them before we need them, then -// we have a chance of keeping our CPU from stalling. Those are -// the purposes of this instruction fetch module: 1) Pipeline -// wishbone accesses, and 2) an instruction cache. -// -// 20150919 -- Fixed a nasty race condition whereby the pipefetch routine -// would produce either the same instruction twice, or skip -// an instruction. This condition was dependent on the CPU stall -// condition, and would only take place if the pipeline wasn't -// completely full throughout the stall. -// -// Interface support was also added for trapping on illegal -// instructions (i.e., instruction fetches that cause bus errors), -// however the internal interface has not caught up to supporting -// these exceptions yet. -// -// Creator: Dan Gisselquist, Ph.D. -// Gisselquist Technology, LLC -// -//////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020 Gisselquist Technology, LLC -// -// This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published -// by the Free Software Foundation, either version 3 of the License, or (at -// your option) any later version. -// -// This program is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program. (It's in the $(ROOT)/doc directory. Run make with no -// target there if the PDF file isn't present.) If not, see -// for a copy. -// -// License: GPL, v3, as defined and found on www.gnu.org, -// http://www.gnu.org/licenses/gpl.html -// -// -//////////////////////////////////////////////////////////////////////////////// -// -// -`default_nettype none -// -module pipefetch(i_clk, i_rst, i_new_pc, i_clear_cache, i_stall_n, i_pc, - o_i, o_pc, o_v, - o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, - i_wb_stall, i_wb_ack, i_wb_err, i_wb_data, i_wb_request, - o_illegal); - parameter RESET_ADDRESS=32'h0010_0000, - LGCACHELEN = 6, ADDRESS_WIDTH=24, - CACHELEN=(1<= r_cache_base + CACHELEN) - ||(i_pc >= r_cache_base + bus_nvalid+5))); - wire w_ran_off_end_of_cache; - assign w_ran_off_end_of_cache =((r_addr_set)&&((r_addr < r_cache_base) - ||(r_addr >= r_cache_base + CACHELEN) - ||(r_addr >= r_cache_base + bus_nvalid+5))); - wire w_running_out_of_cache; - assign w_running_out_of_cache = (r_addr_set) - &&(r_addr >= r_cache_base + - // {{(AW-LGCACHELEN-1),{1'b0}},2'b11, - // {(LGCACHELEN-1){1'b0}}}) - // (1<<(LGCACHELEN-2)) + (1<<(LGCACHELEN-1))) - +(3<<(LGCACHELEN-2))) - &&(|r_nvalid[(LGCACHELEN):(LGCACHELEN-1)]); - - initial r_cache_base = RESET_ADDRESS[(AW+1):2]; - always @(posedge i_clk) - begin - if ((i_rst)||(i_clear_cache)||((o_wb_cyc)&&(i_wb_err))) - begin - o_wb_cyc <= 1'b0; - o_wb_stb <= 1'b0; - // r_cache_base <= RESET_ADDRESS; - // end else if ((~o_wb_cyc)&&(i_new_pc)&&(r_nvalid != 0) - // &&(i_pc >= r_cache_base) - // &&(i_pc < r_cache_base + bus_nvalid)) - // begin - // The new instruction is in our cache, do nothing - // with the bus here. - end else if ((o_wb_cyc)&&(w_pc_out_of_bounds)) - begin - // We need to abandon our bus action to start over in - // a new region, setting up a new cache. This may - // happen mid cycle while waiting for a result. By - // dropping o_wb_cyc, we state that we are no longer - // interested in that result--whatever it might be. - o_wb_cyc <= 1'b0; - o_wb_stb <= 1'b0; - end else if ((~o_wb_cyc)&&(~r_nvalid[LGCACHELEN])&&(~i_wb_request)&&(r_addr_set)) - begin - // Restart a bus cycle that was interrupted when the - // data section wanted access to our bus. - o_wb_cyc <= 1'b1; - o_wb_stb <= 1'b1; - // o_wb_addr <= r_cache_base + bus_nvalid; - end else if ((~o_wb_cyc)&&( - (w_pc_out_of_bounds)||(w_ran_off_end_of_cache))) - begin - // Start a bus transaction - o_wb_cyc <= 1'b1; - o_wb_stb <= 1'b1; - // o_wb_addr <= (i_new_pc) ? i_pc : r_addr; - // r_nvalid <= 0; - // r_cache_base <= (i_new_pc) ? i_pc : r_addr; - // w_cache_offset <= 0; - end else if ((~o_wb_cyc)&&(w_running_out_of_cache)) - begin - // If we're using the last quarter of the cache, then - // let's start a bus transaction to extend the cache. - o_wb_cyc <= 1'b1; - o_wb_stb <= 1'b1; - // o_wb_addr <= r_cache_base + (1<<(LGCACHELEN)); - // r_nvalid <= r_nvalid - (1<<(LGCACHELEN-2)); - // r_cache_base <= r_cache_base + (1<<(LGCACHELEN-2)); - // w_cache_offset <= w_cache_offset + (1<<(LGCACHELEN-2)); - end else if (o_wb_cyc) - begin - // This handles everything ... but the case where - // while reading we need to extend our cache. - if ((o_wb_stb)&&(~i_wb_stall)) - begin - // o_wb_addr <= o_wb_addr + 1; - if ((o_wb_addr - r_cache_base >= CACHELEN-1) - ||(i_wb_request)) - o_wb_stb <= 1'b0; - end - - if (i_wb_ack) - begin - // r_nvalid <= r_nvalid + 1; - if ((r_acks_waiting == 1)&&(~o_wb_stb)) - o_wb_cyc <= 1'b0; - end else if ((r_acks_waiting == 0)&&(~o_wb_stb)) - o_wb_cyc <= 1'b0; - end - end - - - initial r_nvalid = 0; - always @(posedge i_clk) - if ((i_rst)||(i_clear_cache)) // Required, so we can reload memoy and then reset - r_nvalid <= 0; - else if ((~o_wb_cyc)&&( - (w_pc_out_of_bounds)||(w_ran_off_end_of_cache))) - r_nvalid <= 0; - else if ((~o_wb_cyc)&&(w_running_out_of_cache)) - r_nvalid[LGCACHELEN:(LGCACHELEN-2)] - <= r_nvalid[LGCACHELEN:(LGCACHELEN-2)] +3'b111; - // i.e. - (1<<(LGCACHELEN-2)); - else if ((o_wb_cyc)&&(i_wb_ack)) - r_nvalid <= r_nvalid + {{(LGCACHELEN){1'b0}},1'b1}; // +1; - - always @(posedge i_clk) - if (i_clear_cache) - r_cache_base <= i_pc; - else if ((~o_wb_cyc)&&( - (w_pc_out_of_bounds) - ||(w_ran_off_end_of_cache))) - r_cache_base <= (i_new_pc) ? i_pc : r_addr; - else if ((~o_wb_cyc)&&(w_running_out_of_cache)) - r_cache_base[(AW-1):(LGCACHELEN-2)] - <= r_cache_base[(AW-1):(LGCACHELEN-2)] - + {{(AW-LGCACHELEN+1){1'b0}},1'b1}; - // i.e. + (1<<(LGCACHELEN-2)); - - always @(posedge i_clk) - if (i_clear_cache) - r_cache_offset <= 0; - else if ((~o_wb_cyc)&&( - (w_pc_out_of_bounds) - ||(w_ran_off_end_of_cache))) - r_cache_offset <= 0; - else if ((~o_wb_cyc)&&(w_running_out_of_cache)) - r_cache_offset[1:0] <= r_cache_offset[1:0] + 2'b01; - assign w_cache_offset = { r_cache_offset, {(LGCACHELEN-2){1'b0}} }; - - always @(posedge i_clk) - if (i_clear_cache) - o_wb_addr <= i_pc; - else if ((o_wb_cyc)&&(w_pc_out_of_bounds)) - begin - if (i_wb_ack) - o_wb_addr <= r_cache_base + bus_nvalid+1; - else - o_wb_addr <= r_cache_base + bus_nvalid; - end else if ((~o_wb_cyc)&&((w_pc_out_of_bounds) - ||(w_ran_off_end_of_cache))) - o_wb_addr <= (i_new_pc) ? i_pc : r_addr; - else if ((o_wb_stb)&&(~i_wb_stall)) // && o_wb_cyc - o_wb_addr <= o_wb_addr + 1; - - initial r_acks_waiting = 0; - always @(posedge i_clk) - if (~o_wb_cyc) - r_acks_waiting <= 0; - // o_wb_cyc *must* be true for all following - else if ((o_wb_stb)&&(~i_wb_stall)&&(~i_wb_ack)) //&&(o_wb_cyc) - r_acks_waiting <= r_acks_waiting + {{(LGCACHELEN){1'b0}},1'b1}; - else if ((i_wb_ack)&&((~o_wb_stb)||(i_wb_stall))) //&&(o_wb_cyc) - r_acks_waiting <= r_acks_waiting + {(LGCACHELEN+1){1'b1}}; // - 1; - - always @(posedge i_clk) - if ((o_wb_cyc)&&(i_wb_ack)) - cache[r_nvalid[(LGCACHELEN-1):0]+w_cache_offset] - <= i_wb_data; - - initial r_addr_set = 1'b0; - always @(posedge i_clk) - if ((i_rst)||(i_new_pc)) - r_addr_set <= 1'b1; - else if (i_clear_cache) - r_addr_set <= 1'b0; - - // Now, read from the cache - wire w_cv; // Cache valid, address is in the cache - reg r_cv; - assign w_cv = ((r_nvalid != 0)&&(r_addr>=r_cache_base) - &&(r_addr-r_cache_base < bus_nvalid)); - always @(posedge i_clk) - r_cv <= (~i_new_pc)&&((w_cv)||((~i_stall_n)&&(r_cv))); - assign o_v = (r_cv)&&(~i_new_pc); - - always @(posedge i_clk) - if (i_new_pc) - r_addr <= i_pc; - else if ( ((i_stall_n)&&(w_cv)) || ((~i_stall_n)&&(w_cv)&&(r_addr == o_pc)) ) - r_addr <= r_addr + {{(AW-1){1'b0}},1'b1}; - - wire [(LGCACHELEN-1):0] c_rdaddr, c_cache_base; - assign c_cache_base = r_cache_base[(LGCACHELEN-1):0]; - assign c_rdaddr = r_addr[(LGCACHELEN-1):0]-c_cache_base+w_cache_offset; - always @(posedge i_clk) - if ((~o_v)||((i_stall_n)&&(o_v))) - o_i <= cache[c_rdaddr]; - always @(posedge i_clk) - if ((~o_v)||((i_stall_n)&&(o_v))) - o_pc <= r_addr; - - reg [(AW-1):0] ill_address; - initial ill_address = 0; - always @(posedge i_clk) - if ((o_wb_cyc)&&(i_wb_err)) - ill_address <= o_wb_addr - {{(AW-LGCACHELEN-1){1'b0}}, r_acks_waiting}; - - assign o_illegal = (o_pc == ill_address)&&(~i_rst)&&(~i_new_pc)&&(~i_clear_cache); - -endmodule diff --git a/rtl/cpu/pipemem.v b/rtl/cpu/pipemem.v index e72c756..bb03e6d 100644 --- a/rtl/cpu/pipemem.v +++ b/rtl/cpu/pipemem.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: pipemem.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: A memory unit to support a CPU, this time one supporting @@ -16,11 +16,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -33,90 +33,123 @@ // with this program. (It's in the $(ROOT)/doc directory, run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module pipemem(i_clk, i_reset, i_pipe_stb, i_lock, - i_op, i_addr, i_data, i_oreg, - o_busy, o_pipe_stalled, o_valid, o_err, o_wreg, o_result, - o_wb_cyc_gbl, o_wb_cyc_lcl, - o_wb_stb_gbl, o_wb_stb_lcl, - o_wb_we, o_wb_addr, o_wb_data, o_wb_sel, - i_wb_stall, i_wb_ack, i_wb_err, i_wb_data - ); - parameter ADDRESS_WIDTH=30; - parameter [0:0] IMPLEMENT_LOCK=1'b1, - WITH_LOCAL_BUS=1'b1, - OPT_ZERO_ON_IDLE=1'b0, - // OPT_ALIGNMENT_ERR - OPT_ALIGNMENT_ERR=1'b0; - localparam AW=ADDRESS_WIDTH, - FLN=4; - parameter [(FLN-1):0] OPT_MAXDEPTH=4'hd; - input wire i_clk, i_reset; - input wire i_pipe_stb, i_lock; - // CPU interface - input wire [2:0] i_op; - input wire [31:0] i_addr; - input wire [31:0] i_data; - input wire [4:0] i_oreg; - // CPU outputs - output wire o_busy; - output wire o_pipe_stalled; - output reg o_valid; - output reg o_err; - output reg [4:0] o_wreg; - output reg [31:0] o_result; - // Wishbone outputs - output wire o_wb_cyc_gbl; - output reg o_wb_stb_gbl; - output wire o_wb_cyc_lcl; - output reg o_wb_stb_lcl, o_wb_we; - output reg [(AW-1):0] o_wb_addr; - output reg [31:0] o_wb_data; - output reg [3:0] o_wb_sel; - // Wishbone inputs - input wire i_wb_stall, i_wb_ack, i_wb_err; - input wire [31:0] i_wb_data; - - - reg cyc; - reg r_wb_cyc_gbl, r_wb_cyc_lcl, fifo_full; +// }}} +module pipemem #( + // {{{ + parameter ADDRESS_WIDTH=28, + parameter BUS_WIDTH=32, + parameter [0:0] OPT_LOCK=1'b1, + WITH_LOCAL_BUS=1'b1, + OPT_ZERO_ON_IDLE=1'b0, + // OPT_ALIGNMENT_ERR + OPT_ALIGNMENT_ERR=1'b0, + localparam AW=ADDRESS_WIDTH, + FLN=4, + parameter [(FLN-1):0] OPT_MAXDEPTH=4'hd + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // CPU interface + // {{{ + input wire i_pipe_stb, i_lock, + input wire [2:0] i_op, + input wire [31:0] i_addr, + input wire [31:0] i_data, + input wire [4:0] i_oreg, + // CPU outputs + output wire o_busy, o_rdbusy, + output wire o_pipe_stalled, + output reg o_valid, + output reg o_err, + output reg [4:0] o_wreg, + output reg [31:0] o_result, + // }}} + // Wishbone outputs + // {{{ + output wire o_wb_cyc_gbl, + output wire o_wb_cyc_lcl, + output reg o_wb_stb_gbl, + output reg o_wb_stb_lcl, o_wb_we, + output reg [(AW-1):0] o_wb_addr, + output reg [BUS_WIDTH-1:0] o_wb_data, + output reg [BUS_WIDTH/8-1:0] o_wb_sel, + // Wishbone inputs + input wire i_wb_stall, i_wb_ack, i_wb_err, + input wire [BUS_WIDTH-1:0] i_wb_data + // }}} + // }}} + ); + + // Declarations + // {{{ + localparam WBLSB = $clog2(BUS_WIDTH/8); + // Verilator lint_off UNUSED + localparam F_LGDEPTH=FLN+1; + // Verilator lint_on UNUSED + + reg cyc, r_wb_cyc_gbl, r_wb_cyc_lcl, + fifo_full; + wire gbl_stb, lcl_stb, lcl_bus; reg [(FLN-1):0] rdaddr, wraddr; wire [(FLN-1):0] nxt_rdaddr, fifo_fill; - reg [(3+5-1):0] fifo_oreg [0:15]; - reg fifo_gie; - initial rdaddr = 0; - initial wraddr = 0; + reg [4+2+WBLSB-1:0] fifo_mem [0:15]; + reg fifo_gie; + wire [4+2+WBLSB-1:0] w_wreg; + wire misaligned; - reg misaligned; + reg [BUS_WIDTH/8-1:0] oword_sel; + wire [BUS_WIDTH/8-1:0] pre_wb_sel; + reg [31:0] oword_data; + wire [BUS_WIDTH-1:0] pre_wb_data, pre_result; + // }}} - always @(*) - if (OPT_ALIGNMENT_ERR) - begin + // misaligned + // {{{ + generate if (OPT_ALIGNMENT_ERR) + begin : GEN_ALIGNMENT_ERR + reg r_mis; + + always @(*) casez({ i_op[2:1], i_addr[1:0] }) - 4'b01?1: misaligned = i_pipe_stb; - 4'b0110: misaligned = i_pipe_stb; - 4'b10?1: misaligned = i_pipe_stb; - default: misaligned = i_pipe_stb; + 4'b01?1: r_mis = i_pipe_stb; + 4'b0110: r_mis = i_pipe_stb; + 4'b10?1: r_mis = i_pipe_stb; + default: r_mis = i_pipe_stb; endcase - end else - misaligned = 1'b0; + assign misaligned = r_mis; + + end else begin : NO_MISALIGNMENT_ERRS + + assign misaligned = 1'b0; + + end endgenerate + // }}} + + // fifo_mem + // {{{ always @(posedge i_clk) - fifo_oreg[wraddr] <= { i_oreg[3:0], i_op[2:1], i_addr[1:0] }; + fifo_mem[wraddr] <= { i_oreg[3:0], i_op[2:1], i_addr[WBLSB-1:0] }; + // }}} + // fifo_gie + // {{{ always @(posedge i_clk) if (i_pipe_stb) fifo_gie <= i_oreg[4]; + // }}} + // wraddr + // {{{ initial wraddr = 0; always @(posedge i_clk) if (i_reset) @@ -125,7 +158,10 @@ module pipemem(i_clk, i_reset, i_pipe_stb, i_lock, wraddr <= 0; else if (i_pipe_stb) wraddr <= wraddr + 1'b1; + // }}} + // rdaddr + // {{{ initial rdaddr = 0; always @(posedge i_clk) if (i_reset) @@ -134,53 +170,73 @@ module pipemem(i_clk, i_reset, i_pipe_stb, i_lock, rdaddr <= 0; else if ((i_wb_ack)&&(cyc)) rdaddr <= rdaddr + 1'b1; + // }}} assign fifo_fill = wraddr - rdaddr; + // fifo_full + // {{{ initial fifo_full = 0; always @(posedge i_clk) - if (i_reset) + if (i_reset || !cyc) fifo_full <= 0; else if (((i_wb_err)&&(cyc))||((i_pipe_stb)&&(misaligned))) fifo_full <= 0; - else if (i_pipe_stb) - fifo_full <= (fifo_fill >= OPT_MAXDEPTH-1); - else - fifo_full <= (fifo_fill >= OPT_MAXDEPTH); + else case({ i_pipe_stb, i_wb_ack }) + 2'b10: fifo_full <= (fifo_fill >= OPT_MAXDEPTH-1); + 2'b01: fifo_full <= 1'b0; + default: begin end + endcase + // }}} assign nxt_rdaddr = rdaddr + 1'b1; - wire gbl_stb, lcl_stb, lcl_bus; + // lcl_bus, lcl_stb, gbl_stb + // {{{ assign lcl_bus = (i_addr[31:24]==8'hff)&&(WITH_LOCAL_BUS); assign lcl_stb = (lcl_bus)&&(!misaligned); assign gbl_stb = ((!lcl_bus)||(!WITH_LOCAL_BUS))&&(!misaligned); //= ((i_addr[31:8]!=24'hc00000)||(i_addr[7:5]!=3'h0)); + // }}} + // cyc, [or]_wb_[cyc|stb]_[lcl|gbl] + // {{{ initial cyc = 0; initial r_wb_cyc_lcl = 0; initial r_wb_cyc_gbl = 0; initial o_wb_stb_lcl = 0; initial o_wb_stb_gbl = 0; always @(posedge i_clk) - if (i_reset) begin - r_wb_cyc_gbl <= 1'b0; - r_wb_cyc_lcl <= 1'b0; - o_wb_stb_gbl <= 1'b0; - o_wb_stb_lcl <= 1'b0; - cyc <= 1'b0; - end else if (cyc) - begin - if (((!i_wb_stall)&&(!i_pipe_stb)&&(!misaligned)) - ||(i_wb_err)) + if (cyc) begin - o_wb_stb_gbl <= 1'b0; - o_wb_stb_lcl <= 1'b0; + if (((!i_wb_stall)&&(!i_pipe_stb)&&(!misaligned)) + ||(i_wb_err)) + begin + o_wb_stb_gbl <= 1'b0; + o_wb_stb_lcl <= 1'b0; + end + + if (((i_wb_ack)&&(nxt_rdaddr == wraddr) + &&((!i_pipe_stb)||(misaligned))) + ||(i_wb_err)) + begin + r_wb_cyc_gbl <= 1'b0; + r_wb_cyc_lcl <= 1'b0; + o_wb_stb_gbl <= 1'b0; + o_wb_stb_lcl <= 1'b0; + cyc <= 1'b0; + end + end else if (i_pipe_stb) // New memory operation + begin // Grab the wishbone + r_wb_cyc_lcl <= lcl_stb; + r_wb_cyc_gbl <= gbl_stb; + o_wb_stb_lcl <= lcl_stb; + o_wb_stb_gbl <= gbl_stb; + cyc <= (!misaligned); end - if (((i_wb_ack)&&(nxt_rdaddr == wraddr) - &&((!i_pipe_stb)||(misaligned))) - ||(i_wb_err)) + if (i_reset) begin r_wb_cyc_gbl <= 1'b0; r_wb_cyc_lcl <= 1'b0; @@ -188,78 +244,177 @@ module pipemem(i_clk, i_reset, i_pipe_stb, i_lock, o_wb_stb_lcl <= 1'b0; cyc <= 1'b0; end - end else if (i_pipe_stb) // New memory operation - begin // Grab the wishbone - r_wb_cyc_lcl <= lcl_stb; - r_wb_cyc_gbl <= gbl_stb; - o_wb_stb_lcl <= lcl_stb; - o_wb_stb_gbl <= gbl_stb; - cyc <= (!misaligned); + + if (!WITH_LOCAL_BUS) + begin + r_wb_cyc_lcl <= 1'b0; + o_wb_stb_lcl <= 1'b0; + end + end + // }}} + + // pre_wb_sel + // {{{ + always @(*) + begin + oword_sel = 0; + casez({ i_op[2:1], i_addr[1:0] }) + 4'b100?: oword_sel[3:0] = 4'b1100; // Op = 5 + 4'b101?: oword_sel[3:0] = 4'b0011; // Op = 5 + 4'b1100: oword_sel[3:0] = 4'b1000; // Op = 5 + 4'b1101: oword_sel[3:0] = 4'b0100; // Op = 7 + 4'b1110: oword_sel[3:0] = 4'b0010; // Op = 7 + 4'b1111: oword_sel[3:0] = 4'b0001; // Op = 7 + default: oword_sel[3:0] = 4'b1111; // Op = 7 + endcase end + generate if (BUS_WIDTH == 32) + begin : GEN_SEL32 + + assign pre_wb_sel = oword_sel; + + end else begin : GEN_WIDESEL32 + + // If we were little endian, we'd do ... + // assign pre_wb_sel = (oword_sel << (4* i_addr[WBLSB-1:2])); + assign pre_wb_sel = {oword_sel[3:0], {(BUS_WIDTH/8-4){1'b0}} } + >> (4* i_addr[WBLSB-1:2]); + + end endgenerate + // }}} + + // pre_wb_data + // {{{ + + always @(*) + casez({ i_op[2:1], i_addr[1:0] }) + 4'b100?: oword_data = { i_data[15:0], 16'h00 }; + 4'b101?: oword_data = { 16'h00, i_data[15:0] }; + 4'b1100: oword_data = { i_data[7:0], 24'h00 }; + 4'b1101: oword_data = { 8'h00, i_data[7:0], 16'h00 }; + 4'b1110: oword_data = { 16'h00, i_data[7:0], 8'h00 }; + 4'b1111: oword_data = { 24'h00, i_data[7:0] }; + default: oword_data = i_data; + endcase + + generate if (BUS_WIDTH == 32) + begin : GEN_DATA32 + + assign pre_wb_data = oword_data; + + end else begin : GEN_WIDEDATA32 + + // If we were little endian, we'd do ... + // assign pre_wb_sel = (word_sel << (4* i_addr[WBLSB-1:2])); + assign pre_wb_data = {oword_data, {(BUS_WIDTH-32){1'b0}} } + >> (32* i_addr[WBLSB-1:2]); + + end endgenerate + // }}} + + // o_wb_addr, o_wb_sel, and o_wb_data + // {{{ always @(posedge i_clk) if ((!cyc)||(!i_wb_stall)) begin + // o_wb_add + // {{{ if ((OPT_ZERO_ON_IDLE)&&(!i_pipe_stb)) o_wb_addr <= 0; + else if (lcl_bus) + o_wb_addr <= i_addr[2 +: AW]; else - o_wb_addr <= i_addr[(AW+1):2]; + o_wb_addr <= i_addr[WBLSB +: AW]; + // }}} + // o_wb_sel + // {{{ if ((OPT_ZERO_ON_IDLE)&&(!i_pipe_stb)) - o_wb_sel <= 4'b0000; - else casez({ i_op[2:1], i_addr[1:0] }) - 4'b100?: o_wb_sel <= 4'b1100; // Op = 5 - 4'b101?: o_wb_sel <= 4'b0011; // Op = 5 - 4'b1100: o_wb_sel <= 4'b1000; // Op = 5 - 4'b1101: o_wb_sel <= 4'b0100; // Op = 7 - 4'b1110: o_wb_sel <= 4'b0010; // Op = 7 - 4'b1111: o_wb_sel <= 4'b0001; // Op = 7 - default: o_wb_sel <= 4'b1111; // Op = 7 - endcase + o_wb_sel <= {(BUS_WIDTH/8){1'b0}}; + else if (lcl_bus) + o_wb_sel <= oword_sel; + else + o_wb_sel <= pre_wb_sel; + // }}} + // o_wb_data + // {{{ + o_wb_data <= 0; if ((OPT_ZERO_ON_IDLE)&&(!i_pipe_stb)) o_wb_data <= 0; - else casez({ i_op[2:1], i_addr[1:0] }) - 4'b100?: o_wb_data <= { i_data[15:0], 16'h00 }; - 4'b101?: o_wb_data <= { 16'h00, i_data[15:0] }; - 4'b1100: o_wb_data <= { i_data[7:0], 24'h00 }; - 4'b1101: o_wb_data <= { 8'h00, i_data[7:0], 16'h00 }; - 4'b1110: o_wb_data <= { 16'h00, i_data[7:0], 8'h00 }; - 4'b1111: o_wb_data <= { 24'h00, i_data[7:0] }; - default: o_wb_data <= i_data; - endcase + else if (lcl_bus) + o_wb_data[31:0] <= oword_data; + else + o_wb_data <= pre_wb_data; + // }}} end + // }}} + // o_wb_we + // {{{ always @(posedge i_clk) if ((i_pipe_stb)&&(!cyc)) o_wb_we <= i_op[0]; else if ((OPT_ZERO_ON_IDLE)&&(!cyc)) o_wb_we <= 1'b0; + // }}} + // o_valid + // {{{ initial o_valid = 1'b0; always @(posedge i_clk) if (i_reset) o_valid <= 1'b0; else o_valid <= (cyc)&&(i_wb_ack)&&(!o_wb_we); + // }}} + // o_err + // {{{ initial o_err = 1'b0; always @(posedge i_clk) if (i_reset) o_err <= 1'b0; else o_err <= ((cyc)&&(i_wb_err))||((i_pipe_stb)&&(misaligned)); + // }}} + assign o_busy = cyc; + assign o_rdbusy = o_busy && !o_wb_we; + + assign w_wreg = fifo_mem[rdaddr]; - wire [7:0] w_wreg; - assign w_wreg = fifo_oreg[rdaddr]; + // o_wreg + // {{{ always @(posedge i_clk) - o_wreg <= { fifo_gie, w_wreg[7:4] }; + o_wreg <= { fifo_gie, w_wreg[2 + WBLSB +: 4] }; + // }}} + + // o_result + // {{{ + generate if (BUS_WIDTH == 32) + begin : COPY_IDATA + + assign pre_result = i_wb_data; + + end else begin : GEN_PRERESULT + + assign pre_result = i_wb_data << (8*w_wreg[WBLSB-1:0]); + // Verilator coverage_off + // Verilator lint_off UNUSED + wire unused_preresult; + assign unused_preresult = &{1'b0, pre_result[BUS_WIDTH-33:0] }; + // Verilator lint_on UNUSED + // Verilator coverage_on + end endgenerate + always @(posedge i_clk) if ((OPT_ZERO_ON_IDLE)&&((!cyc)||((!i_wb_ack)&&(!i_wb_err)))) o_result <= 0; - else begin - casez(w_wreg[3:0]) + else if ((o_wb_cyc_lcl && WITH_LOCAL_BUS) || (BUS_WIDTH == 32)) + begin + casez({ w_wreg[WBLSB +: 2], w_wreg[1:0] }) 4'b1100: o_result <= { 24'h00, i_wb_data[31:24] }; 4'b1101: o_result <= { 24'h00, i_wb_data[23:16] }; 4'b1110: o_result <= { 24'h00, i_wb_data[15: 8] }; @@ -268,43 +423,86 @@ module pipemem(i_clk, i_reset, i_pipe_stb, i_lock, 4'b101?: o_result <= { 16'h00, i_wb_data[15: 0] }; default: o_result <= i_wb_data[31:0]; endcase + end else begin + casez(w_wreg[WBLSB +: 2]) + 2'b11: o_result <= { 24'h00, pre_result[BUS_WIDTH-1:BUS_WIDTH-8] }; + 2'b10: o_result <= { 16'h00, pre_result[BUS_WIDTH-1:BUS_WIDTH-16] }; + default: o_result <= pre_result[BUS_WIDTH-1:BUS_WIDTH-32]; + endcase end + // }}} + // o_pipe_stalled + // {{{ assign o_pipe_stalled = ((cyc)&&(fifo_full))||((cyc) &&((i_wb_stall)||((!o_wb_stb_lcl)&&(!o_wb_stb_gbl)))); + // }}} + // lock_gbl, lock_lcl + // {{{ generate - if (IMPLEMENT_LOCK != 0) - begin + if (OPT_LOCK) + begin : LOCK_REGISTER + // {{{ reg lock_gbl, lock_lcl; initial lock_gbl = 1'b0; initial lock_lcl = 1'b0; always @(posedge i_clk) - if ((i_reset)||((i_wb_err)&&(cyc)) - ||((i_pipe_stb)&&(misaligned))) begin - lock_gbl <= 1'b0; - lock_lcl <= 1'b0; - end else begin - lock_gbl <= (i_lock)&&((r_wb_cyc_gbl)||(lock_gbl)); - lock_lcl <= (i_lock)&&((r_wb_cyc_lcl)||(lock_lcl)); + lock_gbl <= r_wb_cyc_gbl || lock_gbl; + lock_lcl <= r_wb_cyc_lcl || lock_lcl; + + if (i_reset || (i_wb_err && cyc) + || (i_pipe_stb && misaligned) + || !i_lock) + begin + lock_gbl <= 1'b0; + lock_lcl <= 1'b0; + end + + if (!WITH_LOCAL_BUS) + lock_lcl <= 1'b0; end assign o_wb_cyc_gbl = (r_wb_cyc_gbl)||(lock_gbl); assign o_wb_cyc_lcl = (r_wb_cyc_lcl)||(lock_lcl); - end else begin + // }}} + end else begin : NO_LOCK + // {{{ assign o_wb_cyc_gbl = (r_wb_cyc_gbl); assign o_wb_cyc_lcl = (r_wb_cyc_lcl); + + // Verilator coverage_off + // verilator lint_off UNUSED + wire unused_lock; + assign unused_lock = &{ 1'b0, i_lock }; + // verilator lint_on UNUSED + // Verilator coverage_on + // }}} end endgenerate + // }}} // Make verilator happy + // {{{ + // Verilator coverage_off // verilator lint_off UNUSED wire unused; - assign unused = i_lock; + assign unused = { 1'b0 }; // verilator lint_on UNUSED - + // Verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal property section +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // Formal properties for this module are maintained elsewhere `endif // FORMAL +// }}} endmodule diff --git a/rtl/cpu/prefetch.v b/rtl/cpu/prefetch.v index bef0263..e768300 100644 --- a/rtl/cpu/prefetch.v +++ b/rtl/cpu/prefetch.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: prefetch.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: This is a very simple instruction fetch approach. It gets @@ -29,11 +29,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015,2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -46,79 +46,101 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -// -module prefetch(i_clk, i_reset, i_new_pc, i_clear_cache, i_stalled_n, i_pc, - o_insn, o_pc, o_valid, o_illegal, - o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, - i_wb_stall, i_wb_ack, i_wb_err, i_wb_data); - parameter ADDRESS_WIDTH=30, DATA_WIDTH=32; - localparam AW=ADDRESS_WIDTH, - DW=DATA_WIDTH; - input wire i_clk, i_reset; - // CPU interaction wires - input wire i_new_pc, i_clear_cache, i_stalled_n; - // We ignore i_pc unless i_new_pc is true as well - input wire [(AW+1):0] i_pc; - output reg [(DW-1):0] o_insn; // Instruction read from WB - output wire [(AW+1):0] o_pc; // Address of that instruction - output reg o_valid; // If the output is valid - output reg o_illegal; // Result is from a bus err - // Wishbone outputs - output reg o_wb_cyc, o_wb_stb; - output wire o_wb_we; - output reg [(AW-1):0] o_wb_addr; - output wire [(DW-1):0] o_wb_data; - // And return inputs - input wire i_wb_stall, i_wb_ack, i_wb_err; - input wire [(DW-1):0] i_wb_data; +// }}} +module prefetch #( + // {{{ + parameter ADDRESS_WIDTH=30, // Byte addr wid + INSN_WIDTH=32, + DATA_WIDTH=INSN_WIDTH, + localparam AW=ADDRESS_WIDTH, + DW=DATA_WIDTH, + parameter [0:0] OPT_ALIGNED = 1'b0, + parameter [0:0] OPT_LITTLE_ENDIAN = 1'b1 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // CPU interaction wires + input wire i_new_pc, i_clear_cache, + i_ready, + // We ignore i_pc unless i_new_pc is true as well + input wire [AW-1:0] i_pc, + output reg o_valid, // If output is valid + output reg o_illegal, // bus err result + output reg [INSN_WIDTH-1:0] o_insn, // Insn read from WB + output reg [AW-1:0] o_pc, // Byt addr of that insn + // Wishbone outputs + output reg o_wb_cyc, o_wb_stb, + // verilator coverage_off + output wire o_wb_we, // == const 0 + // verilator coverage_on + output reg [AW-$clog2(DW/8)-1:0] o_wb_addr, + // verilator coverage_off + output wire [DW-1:0] o_wb_data, // == const 0 + // verilator coverage_on + // And return inputs + input wire i_wb_stall, i_wb_ack, i_wb_err, + input wire [DW-1:0] i_wb_data + // }}} + ); // Declare local variables + // {{{ reg invalid; + wire r_valid; + wire [DATA_WIDTH-1:0] r_insn, i_wb_shifted; + // }}} + // These are kind of obligatory outputs when dealing with a bus, that // we'll set them here. Nothing's going to pay attention to these, // though, this is primarily for form. assign o_wb_we = 1'b0; - assign o_wb_data = 32'h0000; + assign o_wb_data = {(DATA_WIDTH){1'b0}}; + // o_wb_cyc, o_wb_stb + // {{{ // Let's build it simple and upgrade later: For each instruction // we do one bus cycle to get the instruction. Later we should // pipeline this, but for now let's just do one at a time. initial o_wb_cyc = 1'b0; initial o_wb_stb = 1'b0; always @(posedge i_clk) - if ((i_reset)||((o_wb_cyc)&&((i_wb_ack)||(i_wb_err)))) + if ((i_reset || i_clear_cache)||(o_wb_cyc &&(i_wb_ack||i_wb_err))) begin + // {{{ // End any bus cycle on a reset, or a return ACK // or error. o_wb_cyc <= 1'b0; o_wb_stb <= 1'b0; - end else if ((!o_wb_cyc)&&( + // }}} + end else if (!o_wb_cyc &&( // Start if the last instruction output was // accepted, *and* it wasn't a bus error // response - ((i_stalled_n)&&(!o_illegal)) + (i_ready && !o_illegal && !r_valid) // Start if the last bus result ended up // invalid ||(invalid) // Start on any request for a new address - ||(i_new_pc))) + ||i_new_pc)) begin + // {{{ // Initiate a bus transaction o_wb_cyc <= 1'b1; o_wb_stb <= 1'b1; + // }}} end else if (o_wb_cyc) begin + // {{{ // If our request has been accepted, then drop the // strobe line if (!i_wb_stall) @@ -134,9 +156,12 @@ module prefetch(i_clk, i_reset, i_new_pc, i_clear_cache, i_stalled_n, i_pc, o_wb_cyc <= 1'b0; o_wb_stb <= 1'b0; end + // }}} end + // }}} - // + // invalid + // {{{ // If during the current bus request, a command came in from the CPU // that will invalidate the results of that request, then we need to // keep track of an "invalid" flag to remember that and so squash @@ -144,13 +169,14 @@ module prefetch(i_clk, i_reset, i_new_pc, i_clear_cache, i_stalled_n, i_pc, // initial invalid = 1'b0; always @(posedge i_clk) - if ((i_reset)||(!o_wb_cyc)) + if (i_reset || !o_wb_cyc) invalid <= 1'b0; else if (i_new_pc) invalid <= 1'b1; + // }}} // The wishbone request address, o_wb_addr - // + // {{{ // The rule regarding this address is that it can *only* be changed // when no bus request is active. Further, since the CPU is depending // upon this value to know what "PC" is associated with the instruction @@ -161,16 +187,190 @@ module prefetch(i_clk, i_reset, i_new_pc, i_clear_cache, i_stalled_n, i_pc, initial o_wb_addr= 0; always @(posedge i_clk) if (i_new_pc) - o_wb_addr <= i_pc[AW+1:2]; - else if ((o_valid)&&(i_stalled_n)&&(!o_illegal)) + o_wb_addr <= i_pc[AW-1:$clog2(DATA_WIDTH/8)]; + else if (o_valid && i_ready && !r_valid) o_wb_addr <= o_wb_addr + 1'b1; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // (Optionally) shift the output word into place + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // This only applies when the bus size doesn't match the instruction + // word size. Here, we only support bus sizes greater than the + // instruction word size. +`ifdef FORMAL + wire [DATA_WIDTH-1:0] f_bus_word; +`endif + + generate if (DATA_WIDTH > INSN_WIDTH) + begin : GEN_SUBSHIFT + // {{{ + localparam NSHIFT = $clog2(DATA_WIDTH/INSN_WIDTH); + + reg rg_valid; + reg [DATA_WIDTH-1:0] rg_insn; + reg [NSHIFT:0] r_count; + reg [NSHIFT-1:0] r_shift; + + // rg_valid + // {{{ + always @(posedge i_clk) + if (i_reset || i_new_pc || i_clear_cache) + rg_valid <= 1'b0; + else if (r_count <= ((o_valid && i_ready) ? 1:0)) + begin + rg_valid <= 1'b0; + if (o_wb_cyc && i_wb_ack && !(&r_shift)) + rg_valid <= 1'b1; + end + // }}} + + // rg_insn + // {{{ + always @(posedge i_clk) + if (i_wb_ack && (r_count <= ((o_valid && i_ready) ? 1:0))) + begin + rg_insn <= i_wb_data; + if (OPT_LITTLE_ENDIAN) + begin + rg_insn <= i_wb_shifted >> INSN_WIDTH; + end else begin + rg_insn <= i_wb_shifted << INSN_WIDTH; + end + end else if (o_valid && i_ready) + begin + if (OPT_LITTLE_ENDIAN) + rg_insn <= rg_insn >> INSN_WIDTH; + else + rg_insn <= rg_insn << INSN_WIDTH; + end + // }}} + + // r_count + // {{{ + always @(posedge i_clk) + if (i_reset || i_new_pc || i_clear_cache) + r_count <= 0; + // Verilator lint_off CMPCONST + else if (o_valid && i_ready && r_valid) + // Verilator lint_on CMPCONST + r_count <= r_count - 1; + else if (o_wb_cyc && i_wb_ack) + begin + // if (OPT_LITTLE_ENDIAN) + r_count <= { 1'b0, ~r_shift }; + end +`ifdef FORMAL + always @(*) + if (!i_reset) + begin + assert(r_valid == (r_count > 0)); + assert(r_count <= (1< 0) + assert(!o_valid && !r_valid); +`endif + // }}} + + assign r_valid = rg_valid; +; + assign r_insn = rg_insn; + if (OPT_LITTLE_ENDIAN) + begin + assign i_wb_shifted = i_wb_data >> (r_shift * INSN_WIDTH); + end else begin + assign i_wb_shifted = i_wb_data << (r_shift * INSN_WIDTH); + end + + // Keep Verilator happy + // {{{ + // Verilator coverage_off + // Verilator lint_off UNUSED + wire unused_shift; + assign unused_shift = &{ 1'b0, + r_insn[DATA_WIDTH-1:INSN_WIDTH], + i_wb_shifted[DATA_WIDTH-1:INSN_WIDTH] }; + // Verilator lint_on UNUSED + // Verilator coverage_on + // }}} +`ifdef FORMAL + assign f_bus_word = rg_insn << ((r_count-1)* INSN_WIDTH); + always @(*) + if (!i_reset && r_valid) + begin + assert(o_valid); + assert(r_shift == 0); + // assert((r_count + o_pc[NSHIFT-1:0]) == ((1<1) + begin + assert(!r_empty); + end else if ($past(!i_rd && f_fill > 0)) + assert(!r_empty); + end + + always @(*) + if (!r_empty) + begin + // This also applies for the registered read case + assert(mem[rd_addr[LGFLEN-1:0]] == o_data); + end else if (OPT_READ_ON_EMPTY) + assert(o_data == i_data); + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Formal contract: (Twin write test) + // {{{ + // If you write two values in succession, you should be able to read + // those same two values in succession some time later. + // + //////////////////////////////////////////////////////////////////////// + // + // + + // Verilator lint_off UNDRIVEN + (* anyconst *) reg [LGFLEN:0] f_first_addr; + // Verilator lint_on UNDRIVEN + reg [LGFLEN:0] f_second_addr; + reg [BW-1:0] f_first_data, f_second_data; + + reg f_first_addr_in_fifo, f_first_in_fifo; + reg f_second_addr_in_fifo, f_second_in_fifo; + reg [LGFLEN:0] f_distance_to_first, f_distance_to_second; + + always @(*) + f_second_addr = f_first_addr + 1; + + always @(*) + begin + f_distance_to_first = (f_first_addr - rd_addr); + f_first_addr_in_fifo = 0; + if ((f_fill != 0) && (f_distance_to_first < f_fill)) + f_first_addr_in_fifo = 1; + end + + always @(*) + begin + f_distance_to_second = (f_second_addr - rd_addr); + f_second_addr_in_fifo = 0; + if ((f_fill != 0) && (f_distance_to_second < f_fill)) + f_second_addr_in_fifo = 1; + end + + always @(posedge i_clk) + if (w_wr && wr_addr == f_first_addr) + f_first_data <= i_data; + + always @(posedge i_clk) + if (w_wr && wr_addr == f_second_addr) + f_second_data <= i_data; + + always @(*) + if (f_first_addr_in_fifo) + assert(mem[f_first_addr[LGFLEN-1:0]] == f_first_data); + always @(*) + f_first_in_fifo = (f_first_addr_in_fifo && (mem[f_first_addr[LGFLEN-1:0]] == f_first_data)); + + always @(*) + if (f_second_addr_in_fifo) + assert(mem[f_second_addr[LGFLEN-1:0]] == f_second_data); + + always @(*) + f_second_in_fifo = (f_second_addr_in_fifo && (mem[f_second_addr[LGFLEN-1:0]] == f_second_data)); + + always @(posedge i_clk) + if (f_past_valid && !$past(i_reset)) + begin + case({$past(f_first_in_fifo), $past(f_second_in_fifo)}) + 2'b00: begin + if ($past(w_wr && (!w_rd || !r_empty)) + &&($past(wr_addr == f_first_addr))) + begin + assert(f_first_in_fifo); + end else begin + assert(!f_first_in_fifo); + end + // + // The second could be in the FIFO, since + // one might write other data than f_first_data + // + // assert(!f_second_in_fifo); + end + 2'b01: begin + assert(!f_first_in_fifo); + if ($past(w_rd && (rd_addr==f_second_addr))) + begin + assert((o_empty&&!OPT_ASYNC_READ)||!f_second_in_fifo); + end else begin + assert(f_second_in_fifo); + end + end + 2'b10: begin + if ($past(w_wr) + &&($past(wr_addr == f_second_addr))) + begin + assert(f_second_in_fifo); + end else begin + assert(!f_second_in_fifo); + end + if ($past(!w_rd ||(rd_addr != f_first_addr))) + assert(f_first_in_fifo); + end + 2'b11: begin + assert(f_second_in_fifo); + if ($past(!w_rd ||(rd_addr != f_first_addr))) + begin + assert(f_first_in_fifo); + if (rd_addr == f_first_addr) + assert(o_data == f_first_data); + end else begin + assert(!f_first_in_fifo); + assert(o_data == f_second_data); + end + end + endcase + end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Cover properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // +`ifdef SFIFO + reg f_was_full; + initial f_was_full = 0; + always @(posedge i_clk) + if (o_full) + f_was_full <= 1; + + always @(posedge i_clk) + cover($fell(f_empty)); + + always @(posedge i_clk) + cover($fell(o_empty)); + + always @(posedge i_clk) + cover(f_was_full && f_empty); + + always @(posedge i_clk) + cover($past(o_full,2)&&(!$past(o_full))&&(o_full)); + + always @(posedge i_clk) + if (f_past_valid) + cover($past(o_empty,2)&&(!$past(o_empty))&& o_empty); +`endif + // }}} + + // Make Verilator happy + // Verilator lint_off UNUSED + wire unused_formal; + assign unused_formal = &{ 1'b0, f_next[LGFLEN], f_empty }; + // Verilator lint_on UNUSED +`endif // FORMAL +// }}} +endmodule diff --git a/rtl/cpu/slowmpy.v b/rtl/cpu/slowmpy.v index cbeb585..0262162 100644 --- a/rtl/cpu/slowmpy.v +++ b/rtl/cpu/slowmpy.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: slowmpy.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: This is a signed (OPT_SIGNED=1) or unsigned (OPT_SIGNED=0) @@ -15,11 +15,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2018-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2018-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -32,72 +32,98 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -// -module slowmpy(i_clk, i_reset, i_stb, i_a, i_b, i_aux, o_busy, - o_done, o_p, o_aux); - parameter LGNA = 6; - parameter [LGNA:0] NA = 33; - parameter [0:0] OPT_SIGNED = 1'b1; - localparam NB = NA; // Must be = NA for OPT_SIGNED to work - // - input wire i_clk, i_reset; - // - input wire i_stb; - input wire signed [(NA-1):0] i_a; - input wire signed [(NB-1):0] i_b; - input wire i_aux; - output reg o_busy, o_done; - output reg signed [(NA+NB-1):0] o_p; - output reg o_aux; - +// }}} +module slowmpy #( + // {{{ + parameter LGNA = 6, + parameter [LGNA:0] NA = 33, + parameter [0:0] OPT_SIGNED = 1'b1, + parameter [0:0] OPT_LOWPOWER = 1'b0, + localparam NB = NA // Must be = NA for OPT_SIGNED to work + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // + input wire i_stb, + input wire signed [(NA-1):0] i_a, + input wire signed [(NB-1):0] i_b, + // verilator coverage_off + input wire i_aux, + // verilator coverage_on + output reg o_busy, o_done, + output reg signed [(NA+NB-1):0] o_p, + // verilator coverage_off + output reg o_aux + // verilator coverage_on + // }}} + ); + + // Declarations + // {{{ reg [LGNA-1:0] count; reg [NA-1:0] p_a; reg [NB-1:0] p_b; reg [NA+NB-1:0] partial; + // verilator coverage_off reg aux; + // verilator coverage_on + reg almost_done; + wire pre_done; + wire [NA-1:0] pwire; + // }}} - reg almost_done; - - wire pre_done; assign pre_done = (count == 0); + + // almost_done + // {{{ initial almost_done = 1'b0; always @(posedge i_clk) almost_done <= (!i_reset)&&(o_busy)&&(pre_done); + // }}} + // aux, o_done, o_busy + // {{{ initial aux = 0; initial o_done = 0; initial o_busy = 0; always @(posedge i_clk) if (i_reset) begin + // {{{ aux <= 0; o_done <= 0; o_busy <= 0; + // }}} end else if ((!o_busy)&&(i_stb)) begin + // {{{ o_done <= 0; o_busy <= 1; aux <= i_aux; + // }}} end else if ((o_busy)&&(almost_done)) begin + // {{{ o_done <= 1; o_busy <= 0; + // }}} end else o_done <= 0; + // }}} - wire [NA-1:0] pwire; assign pwire = (p_b[0] ? p_a : 0); + // count, partial, p_a, p_b + // {{{ always @(posedge i_clk) if (!o_busy) begin @@ -105,6 +131,12 @@ module slowmpy(i_clk, i_reset, i_stb, i_a, i_b, i_aux, o_busy, partial <= 0; p_a <= i_a; p_b <= i_b; + + if (OPT_LOWPOWER && !i_stb) + begin + p_a <= 0; + p_b <= 0; + end end else begin p_b <= (p_b >> 1); // partial[NA+NB-1:NB] <= partial[NA+NB @@ -120,98 +152,32 @@ module slowmpy(i_clk, i_reset, i_stb, i_a, i_b, i_aux, o_busy, + ((p_b[0]) ? {1'b0,p_a} : 0); count <= count - 1; end + // }}} + // o_p, o_aux + // {{{ always @(posedge i_clk) if (almost_done) begin if (OPT_SIGNED) o_p <= partial[NA+NB-1:0] - + {1'b1,{(NA-2){1'b0}},1'b1, {(NB){1'b0}}}; + + { 1'b1, {(NA-2){1'b0}}, 1'b1, {(NB){1'b0}} }; else o_p <= partial[NA+NB-1:0]; o_aux <= aux; end - + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL -`define ASSERT assert -`ifdef SLOWMPY -`define ASSUME assume -`else -`define ASSUME assert -`endif - - reg f_past_valid; - initial f_past_valid = 1'b0; - always @(posedge i_clk) - f_past_valid <= 1'b1; - initial assume(i_reset); - always @(*) - if (!f_past_valid) - `ASSUME(i_reset); - - always @(posedge i_clk) - if ((!f_past_valid)||($past(i_reset))) - begin - `ASSERT(almost_done == 0); - `ASSERT(o_done == 0); - `ASSERT(o_busy == 0); - `ASSERT(aux == 0); - end - - // Assumptions about our inputs - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset))&&($past(i_stb))&&($past(o_busy))) - begin - `ASSUME(i_stb); - `ASSUME($stable(i_a)); - `ASSUME($stable(i_b)); - end - - // - // For now, just formally verify our internal signaling - // - - always @(posedge i_clk) - `ASSERT(almost_done == (o_busy&&(&count))); - - always @(*) - if (!(&count[LGNA-1:1])||(count[0])) - `ASSERT(!o_done); - - always @(posedge i_clk) - if (o_done) - `ASSERT(!o_busy); - always @(posedge i_clk) - if (!o_busy) - `ASSERT(!almost_done); - - reg [NA-1:0] f_a, f_b; - always @(posedge i_clk) - if ((i_stb)&&(!o_busy)) - begin - f_a <= i_a; - f_b <= i_b; - end - - always @(*) - if (o_done) - begin - if ((f_a == 0)||(f_b == 0)) - `ASSERT(o_p == 0); - else - `ASSERT(o_p[NA+NB-1] == f_a[NA-1] ^ f_b[NA-1]); - end - - always @(posedge i_clk) - cover(o_done); - - reg f_past_done; - initial f_past_done = 1'b0; - always @(posedge i_clk) - if (o_done) - f_past_done = 1'b1; - - always @(posedge i_clk) - cover((o_done)&&(f_past_done)); +// Formal properties for this design are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/wbarbiter.v b/rtl/cpu/wbarbiter.v index 194fb3b..9354aeb 100644 --- a/rtl/cpu/wbarbiter.v +++ b/rtl/cpu/wbarbiter.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbarbiter.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: This is a priority bus arbiter. It allows two separate wishbone @@ -15,7 +15,7 @@ // 1. If 'A' or 'B' asserts the o_cyc line, a bus cycle will begin, // with acccess granted to whomever requested it. // 2. If both 'A' and 'B' assert o_cyc at the same time, only 'A' -// will be granted the bus. (If the alternating parameter +// will be granted the bus. (If the alternating parameter // is set, A and B will alternate who gets the bus in // this case.) // 3. The bus will remain owned by whomever the bus was granted to @@ -33,11 +33,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -50,140 +50,150 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none // `define WBA_ALTERNATING -// -module wbarbiter(i_clk, i_reset, - // Bus A -- the priority bus - i_a_cyc, i_a_stb, i_a_we, i_a_adr, i_a_dat, i_a_sel, - o_a_stall, o_a_ack, o_a_err, - // Bus B - i_b_cyc, i_b_stb, i_b_we, i_b_adr, i_b_dat, i_b_sel, - o_b_stall, o_b_ack, o_b_err, - // Combined/arbitrated bus - o_cyc, o_stb, o_we, o_adr, o_dat, o_sel, i_stall, i_ack, i_err +// }}} +module wbarbiter #( + // {{{ + parameter DW=32, AW=32, + parameter SCHEME="ALTERNATING", + parameter [0:0] OPT_ZERO_ON_IDLE = 1'b0 `ifdef FORMAL - , - f_a_nreqs, f_a_nacks, f_a_outstanding, - f_b_nreqs, f_b_nacks, f_b_outstanding, - f_nreqs, f_nacks, f_outstanding + , parameter F_MAX_STALL = 3, + parameter F_MAX_ACK_DELAY = 3, + parameter F_LGDEPTH=3 `endif - ); - parameter DW=32, AW=32; - parameter SCHEME="ALTERNATING"; - parameter [0:0] OPT_ZERO_ON_IDLE = 1'b0; - parameter F_MAX_STALL = 3; - parameter F_MAX_ACK_DELAY = 3; -`ifdef FORMAL - parameter F_LGDEPTH=3; -`endif - - // - input wire i_clk, i_reset; - // Bus A - input wire i_a_cyc, i_a_stb, i_a_we; - input wire [(AW-1):0] i_a_adr; - input wire [(DW-1):0] i_a_dat; - input wire [(DW/8-1):0] i_a_sel; - output wire o_a_stall, o_a_ack, o_a_err; - // Bus B - input wire i_b_cyc, i_b_stb, i_b_we; - input wire [(AW-1):0] i_b_adr; - input wire [(DW-1):0] i_b_dat; - input wire [(DW/8-1):0] i_b_sel; - output wire o_b_stall, o_b_ack, o_b_err; - // - output wire o_cyc, o_stb, o_we; - output wire [(AW-1):0] o_adr; - output wire [(DW-1):0] o_dat; - output wire [(DW/8-1):0] o_sel; - input wire i_stall, i_ack, i_err; - // + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Bus A -- the priority bus + // {{{ + input wire i_a_cyc, i_a_stb, i_a_we, + input wire [(AW-1):0] i_a_adr, + input wire [(DW-1):0] i_a_dat, + input wire [(DW/8-1):0] i_a_sel, + output wire o_a_stall, o_a_ack, o_a_err, + // }}} + // Bus B + // {{{ + input wire i_b_cyc, i_b_stb, i_b_we, + input wire [(AW-1):0] i_b_adr, + input wire [(DW-1):0] i_b_dat, + input wire [(DW/8-1):0] i_b_sel, + output wire o_b_stall, o_b_ack, o_b_err, + // }}} + // Combined/arbitrated bus + // {{{ + output wire o_cyc, o_stb, o_we, + output wire [(AW-1):0] o_adr, + output wire [(DW-1):0] o_dat, + output wire [(DW/8-1):0] o_sel, + input wire i_stall, i_ack, i_err + // }}} `ifdef FORMAL - output wire [(F_LGDEPTH-1):0] f_nreqs, f_nacks, f_outstanding, + , output wire [(F_LGDEPTH-1):0] + f_nreqs, f_nacks, f_outstanding, f_a_nreqs, f_a_nacks, f_a_outstanding, - f_b_nreqs, f_b_nacks, f_b_outstanding; + f_b_nreqs, f_b_nacks, f_b_outstanding `endif + // }}} + ); + // {{{ // Go high immediately (new cycle) if ... // Previous cycle was low and *someone* is requesting a bus cycle // Go low immadiately if ... // We were just high and the owner no longer wants the bus // WISHBONE Spec recommends no logic between a FF and the o_cyc // This violates that spec. (Rec 3.15, p35) + // }}} + + // Local declarations + // {{{ reg r_a_owner; + // }}} assign o_cyc = (r_a_owner) ? i_a_cyc : i_b_cyc; initial r_a_owner = 1'b1; + // r_a_owner -- determined through arbitration + // {{{ generate if (SCHEME == "PRIORITY") begin : PRI - + // {{{ always @(posedge i_clk) - if (!i_b_cyc) - r_a_owner <= 1'b1; - // Allow B to set its CYC line w/o activating this - // interface - else if ((i_b_stb)&&(!i_a_cyc)) - r_a_owner <= 1'b0; - + if (!i_b_cyc) + r_a_owner <= 1'b1; + // Allow B to set its CYC line w/o activating this + // interface + else if ((i_b_stb)&&(!i_a_cyc)) + r_a_owner <= 1'b0; + // }}} end else if (SCHEME == "ALTERNATING") begin : ALT - + // {{{ reg last_owner; + initial last_owner = 1'b0; always @(posedge i_clk) - if ((i_a_cyc)&&(r_a_owner)) - last_owner <= 1'b1; - else if ((i_b_cyc)&&(!r_a_owner)) - last_owner <= 1'b0; + if ((i_a_cyc)&&(r_a_owner)) + last_owner <= 1'b1; + else if ((i_b_cyc)&&(!r_a_owner)) + last_owner <= 1'b0; always @(posedge i_clk) - if ((!i_a_cyc)&&(!i_b_cyc)) - r_a_owner <= !last_owner; - else if ((r_a_owner)&&(!i_a_cyc)) - begin - - if (i_b_stb) - r_a_owner <= 1'b0; + if ((!i_a_cyc)&&(!i_b_cyc)) + r_a_owner <= !last_owner; + else if ((r_a_owner)&&(!i_a_cyc)) + begin - end else if ((!r_a_owner)&&(!i_b_cyc)) - begin + if (i_b_stb) + r_a_owner <= 1'b0; - if (i_a_stb) - r_a_owner <= 1'b1; + end else if ((!r_a_owner)&&(!i_b_cyc)) + begin - end + if (i_a_stb) + r_a_owner <= 1'b1; + end + // }}} end else // if (SCHEME == "LAST") begin : LST + // {{{ always @(posedge i_clk) - if ((!i_a_cyc)&&(i_b_stb)) - r_a_owner <= 1'b0; - else if ((!i_b_cyc)&&(i_a_stb)) - r_a_owner <= 1'b1; + if ((!i_a_cyc)&&(i_b_stb)) + r_a_owner <= 1'b0; + else if ((!i_b_cyc)&&(i_a_stb)) + r_a_owner <= 1'b1; + // ?}}} end endgenerate + // }}} - + // o_we + // {{{ // Realistically, if neither master owns the bus, the output is a // don't care. Thus we trigger off whether or not 'A' owns the bus. // If 'B' owns it all we care is that 'A' does not. Likewise, if // neither owns the bus than the values on the various lines are // irrelevant. assign o_we = (r_a_owner) ? i_a_we : i_b_we; + // }}} + // Other bus outputs + // {{{ generate if (OPT_ZERO_ON_IDLE) - begin - // + begin : LOW_POWER + // {{{ // OPT_ZERO_ON_IDLE will use up more logic and may even slow // down the master clock if set. However, it may also reduce // the power used by the FPGA by preventing things from toggling @@ -202,7 +212,9 @@ module wbarbiter(i_clk, i_reset, assign o_b_stall = (o_cyc)&&(!r_a_owner) ? i_stall : 1'b1; assign o_a_err = (o_cyc)&&( r_a_owner) ? i_err : 1'b0; assign o_b_err = (o_cyc)&&(!r_a_owner) ? i_err : 1'b0; - end else begin + // }}} + end else begin : LOW_LOGIC + // {{{ assign o_stb = (r_a_owner) ? i_a_stb : i_b_stb; assign o_adr = (r_a_owner) ? i_a_adr : i_b_adr; @@ -224,109 +236,31 @@ module wbarbiter(i_clk, i_reset, // assign o_a_err = ( r_a_owner) ? i_err : 1'b0; assign o_b_err = (!r_a_owner) ? i_err : 1'b0; + // }}} end endgenerate + // }}} // Make Verilator happy + // {{{ + // verilator coverage_off // verilator lint_off UNUSED wire unused; - assign unused = i_reset; + assign unused = &{ 1'b0, i_reset }; // verilator lint_on UNUSED - + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL - -`ifdef WBARBITER -`define ASSUME assume -`else -`define ASSUME assert -`endif - - reg f_past_valid; - initial f_past_valid = 1'b0; - always @(posedge i_clk) - f_past_valid <= 1'b1; - - initial `ASSUME(!i_a_cyc); - initial `ASSUME(!i_a_stb); - - initial `ASSUME(!i_b_cyc); - initial `ASSUME(!i_b_stb); - - initial `ASSUME(!i_ack); - initial `ASSUME(!i_err); - - always @(*) - if (!f_past_valid) - `ASSUME(i_reset); - - always @(posedge i_clk) - begin - if (o_cyc) - assert((i_a_cyc)||(i_b_cyc)); - if ((f_past_valid)&&($past(o_cyc))&&(o_cyc)) - assert($past(r_a_owner) == r_a_owner); - end - - fwb_master #(.DW(DW), .AW(AW), - .F_MAX_STALL(F_MAX_STALL), - .F_LGDEPTH(F_LGDEPTH), - .F_MAX_ACK_DELAY(F_MAX_ACK_DELAY), - .F_OPT_RMW_BUS_OPTION(1), - .F_OPT_DISCONTINUOUS(1), - .F_OPT_CLK2FFLOGIC(1'b0)) - f_wbm(i_clk, i_reset, - o_cyc, o_stb, o_we, o_adr, o_dat, o_sel, - i_ack, i_stall, 32'h0, i_err, - f_nreqs, f_nacks, f_outstanding); - - fwb_slave #(.DW(DW), .AW(AW), - .F_MAX_STALL(0), - .F_LGDEPTH(F_LGDEPTH), - .F_MAX_ACK_DELAY(0), - .F_OPT_RMW_BUS_OPTION(1), - .F_OPT_DISCONTINUOUS(1), - .F_OPT_CLK2FFLOGIC(1'b0)) - f_wba(i_clk, i_reset, - i_a_cyc, i_a_stb, i_a_we, i_a_adr, i_a_dat, i_a_sel, - o_a_ack, o_a_stall, 32'h0, o_a_err, - f_a_nreqs, f_a_nacks, f_a_outstanding); - - fwb_slave #(.DW(DW), .AW(AW), - .F_MAX_STALL(0), - .F_LGDEPTH(F_LGDEPTH), - .F_MAX_ACK_DELAY(0), - .F_OPT_RMW_BUS_OPTION(1), - .F_OPT_DISCONTINUOUS(1), - .F_OPT_CLK2FFLOGIC(1'b0)) - f_wbb(i_clk, i_reset, - i_b_cyc, i_b_stb, i_b_we, i_b_adr, i_b_dat, i_b_sel, - o_b_ack, o_b_stall, 32'h0, o_b_err, - f_b_nreqs, f_b_nacks, f_b_outstanding); - - always @(posedge i_clk) - if (r_a_owner) - begin - assert(f_b_nreqs == 0); - assert(f_b_nacks == 0); - assert(f_a_outstanding == f_outstanding); - end else begin - assert(f_a_nreqs == 0); - assert(f_a_nacks == 0); - assert(f_b_outstanding == f_outstanding); - end - - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset)) - &&($past(i_a_stb))&&(!$past(i_b_cyc))) - assert(r_a_owner); - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset)) - &&(!$past(i_a_cyc))&&($past(i_b_stb))) - assert(!r_a_owner); - - always @(posedge i_clk) - if ((f_past_valid)&&(r_a_owner != $past(r_a_owner))) - assert(!$past(o_cyc)); - +// The formal properties for this module are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/wbdblpriarb.v b/rtl/cpu/wbdblpriarb.v index 9f308cf..4782d05 100644 --- a/rtl/cpu/wbdblpriarb.v +++ b/rtl/cpu/wbdblpriarb.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbdblpriarb.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: This should almost be identical to the priority arbiter, save @@ -43,11 +43,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015,2018-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -60,65 +60,79 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbdblpriarb(i_clk, i_reset, - // Bus A - i_a_cyc_a,i_a_cyc_b,i_a_stb_a,i_a_stb_b,i_a_we,i_a_adr, i_a_dat, i_a_sel, o_a_stall, o_a_ack, o_a_err, - // Bus B - i_b_cyc_a,i_b_cyc_b,i_b_stb_a,i_b_stb_b,i_b_we,i_b_adr, i_b_dat, i_b_sel, o_b_stall, o_b_ack, o_b_err, - // Both buses - o_cyc_a, o_cyc_b, o_stb_a, o_stb_b, o_we, o_adr, o_dat, o_sel, - i_stall, i_ack, i_err +// }}} +module wbdblpriarb #( + // {{{ + parameter DW=32, AW=32, + // OPT_ZERO_ON_IDLE + // {{{ + // OPT_ZERO_ON_IDLE uses more logic than the alternative. It + // should be useful for reducing power, as these circuits tend + // to drive wires all the way across the design, but it may also + // slow down the master clock. I've used it as an option when + // using VER1LATOR, 'cause zeroing things on idle can make them + // stand out all the more when staring at wires and dumps and + // such. + parameter [0:0] OPT_ZERO_ON_IDLE = 1'b0 + // }}} + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Bus A + // {{{ + input wire i_a_cyc_a, i_a_cyc_b, + i_a_stb_a, i_a_stb_b, i_a_we, + input wire [(AW-1):0] i_a_adr, + input wire [(DW-1):0] i_a_dat, + input wire [(DW/8-1):0] i_a_sel, + output wire o_a_stall, o_a_ack, o_a_err, + // }}} + // Bus B + // {{{ + input wire i_b_cyc_a, i_b_cyc_b, + i_b_stb_a, i_b_stb_b, i_b_we, + input wire [(AW-1):0] i_b_adr, + input wire [(DW-1):0] i_b_dat, + input wire [(DW/8-1):0] i_b_sel, + output wire o_b_stall, o_b_ack, o_b_err, + // }}} + // Both buses (i.e. the outgoing, arbitrated bus) + // {{{ + output wire o_cyc_a, o_cyc_b, + o_stb_a, o_stb_b, o_we, + output wire [(AW-1):0] o_adr, + output wire [(DW-1):0] o_dat, + output wire [(DW/8-1):0] o_sel, + input wire i_stall, i_ack, i_err + // }}} +`ifdef FORMAL + // {{{ + // These wires are relics from when the ZipCPU was verified + // with this arbiter internal to it. Since this is no longer + // the case, they're now no more than declarations + , output wire [(F_LGDEPTH-1):0] + f_nreqs_a, f_nacks_a, f_outstanding_a, + f_nreqs_b, f_nacks_b, f_outstanding_b, + f_a_nreqs_a, f_a_nacks_a, f_a_outstanding_a, + f_a_nreqs_b, f_a_nacks_b, f_a_outstanding_b, + f_b_nreqs_a, f_b_nacks_a, f_b_outstanding_a, + f_b_nreqs_b, f_b_nacks_b, f_b_outstanding_b + // }}} +`endif + // }}} ); - parameter DW=32, AW=32; - // - // ZERO_ON_IDLE uses more logic than the alternative. It should be - // useful for reducing power, as these circuits tend to drive wires - // all the way across the design, but it may also slow down the master - // clock. I've used it as an option when using VERILATOR, 'cause - // zeroing things on idle can make them stand out all the more when - // staring at wires and dumps and such. - parameter [0:0] OPT_ZERO_ON_IDLE = 1'b0; - // - // - parameter F_LGDEPTH = 3; - // - // - parameter F_MAX_STALL = 0; - parameter F_MAX_ACK_DELAY=0; - // - // Wishbone doesn't use an i_ce signal. While it could, they dislike - // what it would (might) do to the synchronous reset signal, i_reset. - input wire i_clk, i_reset; - // Bus A - input wire i_a_cyc_a, i_a_cyc_b, i_a_stb_a, i_a_stb_b, i_a_we; - input wire [(AW-1):0] i_a_adr; - input wire [(DW-1):0] i_a_dat; - input wire [(DW/8-1):0] i_a_sel; - output wire o_a_stall, o_a_ack, o_a_err; - // Bus B - input wire i_b_cyc_a, i_b_cyc_b, i_b_stb_a, i_b_stb_b, i_b_we; - input wire [(AW-1):0] i_b_adr; - input wire [(DW-1):0] i_b_dat; - input wire [(DW/8-1):0] i_b_sel; - output wire o_b_stall, o_b_ack, o_b_err; - // - output wire o_cyc_a,o_cyc_b, o_stb_a, o_stb_b, o_we; - output wire [(AW-1):0] o_adr; - output wire [(DW-1):0] o_dat; - output wire [(DW/8-1):0] o_sel; - input wire i_stall, i_ack, i_err; - + // r_a_owner + // {{{ // All of our logic is really captured in the 'r_a_owner' register. // This register determines who owns the bus. If no one is requesting // the bus, ownership goes to A on the next clock. Otherwise, if B is @@ -131,49 +145,52 @@ module wbdblpriarb(i_clk, i_reset, initial r_a_owner = 1'b1; always @(posedge i_clk) - if (i_reset) - r_a_owner <= 1'b1; - /* - // Remain with the "last owner" until 1) the other bus requests - // access, and 2) the last owner no longer wants it. This - // logic "idles" on the last owner. - // - // This is an alternating bus owner strategy - // - else if ((!o_cyc_a)&&(!o_cyc_b)) - r_a_owner <= ((i_b_stb_a)||(i_b_stb_b))? 1'b0:1'b1; - // - // Expanding this out - // - // else if ((r_a_owner)&&((i_a_cyc_a)||(i_a_cyc_b))) - // r_a_owner <= 1'b1; - // else if ((!r_a_owner)&&((i_b_cyc_a)||(i_b_cyc_b))) - // r_a_owner <= 1'b0; - // else if ((r_a_owner)&&((i_b_stb_a)||(i_b_stb_b))) - // r_a_owner <= 1'b0; - // else if ((!r_a_owner)&&((i_a_stb_a)||(i_a_stb_b))) - // r_a_owner <= 1'b0; - // - // Logic required: - // - // Reset line - // + 9 inputs (data) - // + 9 inputs (CE) - // Could be done with three LUTs - // First two evaluate o_cyc_a and o_cyc_b (above) - */ - // Option 2: - // - // "Idle" on A as the owner. - // If a request is made from B, AND A is idle, THEN - // switch. Otherwise, if B is ever idle, revert back to A - // regardless of whether A wants it or not. - else if ((!i_b_cyc_a)&&(!i_b_cyc_b)) - r_a_owner <= 1'b1; - else if ((!i_a_cyc_a)&&(!i_a_cyc_b) - &&((i_b_stb_a)||(i_b_stb_b))) - r_a_owner <= 1'b0; + if (i_reset) + r_a_owner <= 1'b1; + /* + // Remain with the "last owner" until 1) the other bus requests + // access, and 2) the last owner no longer wants it. This + // logic "idles" on the last owner. + // + // This is an alternating bus owner strategy + // + else if ((!o_cyc_a)&&(!o_cyc_b)) + r_a_owner <= ((i_b_stb_a)||(i_b_stb_b))? 1'b0:1'b1; + // + // Expanding this out + // + // else if ((r_a_owner)&&((i_a_cyc_a)||(i_a_cyc_b))) + // r_a_owner <= 1'b1; + // else if ((!r_a_owner)&&((i_b_cyc_a)||(i_b_cyc_b))) + // r_a_owner <= 1'b0; + // else if ((r_a_owner)&&((i_b_stb_a)||(i_b_stb_b))) + // r_a_owner <= 1'b0; + // else if ((!r_a_owner)&&((i_a_stb_a)||(i_a_stb_b))) + // r_a_owner <= 1'b0; + // + // Logic required: + // + // Reset line + // + 9 inputs (data) + // + 9 inputs (CE) + // Could be done with three LUTs + // First two evaluate o_cyc_a and o_cyc_b (above) + */ + // Option 2: + // + // "Idle" on A as the owner. + // If a request is made from B, AND A is idle, THEN + // switch. Otherwise, if B is ever idle, revert back to A + // regardless of whether A wants it or not. + else if ((!i_b_cyc_a)&&(!i_b_cyc_b)) + r_a_owner <= 1'b1; + else if ((!i_a_cyc_a)&&(!i_a_cyc_b) + &&((i_b_stb_a)||(i_b_stb_b))) + r_a_owner <= 1'b0; + // }}} + // o_cyc*, o_stb*, o_we + // {{{ // Realistically, if neither master owns the bus, the output is a // don't care. Thus we trigger off whether or not 'A' owns the bus. // If 'B' owns it all we care is that 'A' does not. Likewise, if @@ -185,9 +202,15 @@ module wbdblpriarb(i_clk, i_reset, assign o_stb_a = (r_a_owner) ? i_a_stb_a : i_b_stb_a; assign o_stb_b = (r_a_owner) ? i_a_stb_b : i_b_stb_b; assign o_we = (r_a_owner) ? i_a_we : i_b_we; + // }}} + + // Other bus outputs and returns + // {{{ generate if (OPT_ZERO_ON_IDLE) - begin + begin : OPT_LOWPOWER + // {{{ wire o_cyc, o_stb; + assign o_cyc = ((o_cyc_a)||(o_cyc_b)); assign o_stb = ((o_stb_a)||(o_stb_b)); assign o_adr = (o_stb)?((r_a_owner) ? i_a_adr : i_b_adr):0; @@ -199,7 +222,9 @@ module wbdblpriarb(i_clk, i_reset, assign o_b_stall = (o_cyc)&&(!r_a_owner) ? i_stall : 1'b1; assign o_a_err = (o_cyc)&&( r_a_owner) ? i_err : 1'b0; assign o_b_err = (o_cyc)&&(!r_a_owner) ? i_err : 1'b0; - end else begin + // }}} + end else begin : OPT_LOWLOGIC + // {{{ assign o_adr = (r_a_owner) ? i_a_adr : i_b_adr; assign o_dat = (r_a_owner) ? i_a_dat : i_b_dat; assign o_sel = (r_a_owner) ? i_a_sel : i_b_sel; @@ -219,10 +244,21 @@ module wbdblpriarb(i_clk, i_reset, // assign o_a_err = ( r_a_owner) ? i_err : 1'b0; assign o_b_err = (!r_a_owner) ? i_err : 1'b0; + // }}} end endgenerate - + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // The formal properties for this module are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/wbdmac.v b/rtl/cpu/wbdmac.v index e158350..6142ad7 100644 --- a/rtl/cpu/wbdmac.v +++ b/rtl/cpu/wbdmac.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbdmac.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: Wishbone DMA controller @@ -47,7 +47,7 @@ // exactly where the controller is at mid-transfer. // 27..16 W WriteProtect When a 12'h3db is written to these // bits, the write protect bit will be cleared. -// +// // 15 R/W on_dev_trigger When set to '1', the controller will // wait for an external interrupt before starting. // 14..10 R/W device_id This determines which external interrupt @@ -64,7 +64,7 @@ // To use this, follow this checklist: // 1. Wait for any prior DMA operation to complete // (Read address 0, wait 'till either top bit is set or cfg_len==0) -// 2. Write values into length, source and destination address. +// 2. Write values into length, source and destination address. // (writei(3, &vals) should be sufficient for this.) // 3. Enable the DMAC interrupt in whatever interrupt controller is present // on the system. @@ -85,11 +85,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -102,97 +102,68 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // // //////////////////////////////////////////////////////////////////////////////// // -// -`define DMA_IDLE 3'b000 -`define DMA_WAIT 3'b001 -`define DMA_READ_REQ 3'b010 -`define DMA_READ_ACK 3'b011 -`define DMA_PRE_WRITE 3'b100 -`define DMA_WRITE_REQ 3'b101 -`define DMA_WRITE_ACK 3'b110 - -module wbdmac(i_clk, i_reset, - i_swb_cyc, i_swb_stb, i_swb_we, i_swb_addr, i_swb_data, - o_swb_stall, o_swb_ack, o_swb_data, - o_mwb_cyc, o_mwb_stb, o_mwb_we, o_mwb_addr, o_mwb_data, - i_mwb_stall, i_mwb_ack, i_mwb_data, i_mwb_err, - i_dev_ints, - o_interrupt); - parameter ADDRESS_WIDTH=30, LGMEMLEN = 10, DW=32; +`default_nettype none +// }}} +module wbdmac #( + // {{{ + parameter ADDRESS_WIDTH=30, LGMEMLEN = 10, + parameter SLV_WIDTH=32, + parameter BUS_WIDTH=32, + localparam AW=ADDRESS_WIDTH + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Slave port + // {{{ + // Slave/control wishbone inputs + input wire i_swb_cyc, i_swb_stb, i_swb_we, + input wire [1:0] i_swb_addr, + input wire [(SLV_WIDTH-1):0] i_swb_data, + // Slave/control wishbone outputs + output wire o_swb_stall, + output reg o_swb_ack, + output reg [(SLV_WIDTH-1):0] o_swb_data, + // }}} + // Master/DMA port + // {{{ + output wire o_mwb_cyc, o_mwb_stb, o_mwb_we, + output reg [(AW-1):0] o_mwb_addr, + output reg [(BUS_WIDTH-1):0] o_mwb_data, + // Master/DMA wishbone responses from the bus + input wire i_mwb_stall, i_mwb_ack, + input wire [(BUS_WIDTH-1):0] i_mwb_data, + input wire i_mwb_err, + // }}} + // The interrupt device interrupt lines + input wire [31:0] i_dev_ints, + // An interrupt to be set upon completion + output reg o_interrupt + // }}} + ); + + // Local declarations + // {{{ localparam LGDV=5; - localparam AW=ADDRESS_WIDTH; - input wire i_clk, i_reset; - // Slave/control wishbone inputs - input wire i_swb_cyc, i_swb_stb, i_swb_we; - input wire [1:0] i_swb_addr; - input wire [(DW-1):0] i_swb_data; - // Slave/control wishbone outputs - output reg o_swb_ack; - output wire o_swb_stall; - output reg [(DW-1):0] o_swb_data; - // Master/DMA wishbone control - output wire o_mwb_cyc, o_mwb_stb, o_mwb_we; - output reg [(AW-1):0] o_mwb_addr; - output reg [(DW-1):0] o_mwb_data; - // Master/DMA wishbone responses from the bus - input wire i_mwb_stall, i_mwb_ack; - input wire [(DW-1):0] i_mwb_data; - input wire i_mwb_err; - // The interrupt device interrupt lines - input wire [(DW-1):0] i_dev_ints; - // An interrupt to be set upon completion - output reg o_interrupt; - // Need to release the bus for a higher priority user - // This logic had lots of problems, so it is being - // removed. If you want to make sure the bus is available - // for a higher priority user, adjust the transfer length - // accordingly. - // - // input i_other_busmaster_requests_bus; - // - - wire s_cyc, s_stb, s_we; - wire [1:0] s_addr; - wire [31:0] s_data; -`define DELAY_ACCESS -`ifdef DELAY_ACCESS - reg r_s_cyc, r_s_stb, r_s_we; - reg [1:0] r_s_addr; - reg [31:0] r_s_data; - - always @(posedge i_clk) - r_s_cyc <= i_swb_cyc; - always @(posedge i_clk) - if (i_reset) - r_s_stb <= 1'b0; - else - r_s_stb <= i_swb_stb; - always @(posedge i_clk) - r_s_we <= i_swb_we; - always @(posedge i_clk) - r_s_addr<= i_swb_addr; - always @(posedge i_clk) - r_s_data<= i_swb_data; - - assign s_cyc = r_s_cyc; - assign s_stb = r_s_stb; - assign s_we = r_s_we; - assign s_addr= r_s_addr; - assign s_data= r_s_data; -`else - assign s_cyc = i_swb_cyc; - assign s_stb = i_swb_stb; - assign s_we = i_swb_we; - assign s_addr= i_swb_addr; - assign s_data= i_swb_data; -`endif + localparam [2:0] DMA_IDLE = 3'b000, + DMA_WAIT = 3'b001, + DMA_READ_REQ = 3'b010, + DMA_READ_ACK = 3'b011, + DMA_PRE_WRITE = 3'b100, + DMA_WRITE_REQ = 3'b101, + DMA_WRITE_ACK = 3'b110; + + wire s_cyc, s_stb, s_we; + wire [1:0] s_addr; + wire [SLV_WIDTH-1:0] s_data; reg [2:0] dma_state; reg cfg_err, cfg_len_nonzero; @@ -205,31 +176,82 @@ module wbdmac(i_clk, i_reset, // Single block operations: We'll read, then write, up to a single // memory block here. - reg [(DW-1):0] dma_mem [0:(((1< 1); end + // }}} + // nracks + // {{{ initial nracks = 0; always @(posedge i_clk) if (i_reset) nracks <= 0; - else if ((dma_state == `DMA_IDLE)||(dma_state == `DMA_WAIT)) + else if ((dma_state == DMA_IDLE)||(dma_state == DMA_WAIT)) nracks <= 0; else if ((o_mwb_stb)&&(!o_mwb_we)&&(!i_mwb_stall)) nracks <= nracks + 1'b1; + // }}} + // nread + // {{{ initial nread = 0; always @(posedge i_clk) if (i_reset) nread <= 0; - else if ((dma_state == `DMA_IDLE)||(dma_state == `DMA_WAIT)) + else if ((dma_state == DMA_IDLE)||(dma_state == DMA_WAIT)) nread <= 0; else if ((!o_mwb_we)&&(i_mwb_ack)) nread <= nread + 1'b1; + // }}} + // nwritten + // {{{ initial nwritten = 0; always @(posedge i_clk) if (i_reset) @@ -394,7 +442,10 @@ module wbdmac(i_clk, i_reset, nwritten <= 0; else if ((o_mwb_stb)&&(!i_mwb_stall)) nwritten <= nwritten + 1'b1; + // }}} + // nwacks + // {{{ initial nwacks = 0; always @(posedge i_clk) if (i_reset) @@ -403,25 +454,31 @@ module wbdmac(i_clk, i_reset, nwacks <= 0; else if (i_mwb_ack) nwacks <= nwacks + 1'b1; + // }}} + // cfg_err + // {{{ initial cfg_err = 1'b0; always @(posedge i_clk) if (i_reset) cfg_err <= 1'b0; - else if (dma_state == `DMA_IDLE) + else if (dma_state == DMA_IDLE) begin if ((s_stb)&&(s_we)&&(s_addr==2'b00)) cfg_err <= 1'b0; end else if (((i_mwb_err)&&(o_mwb_cyc))||(abort)) cfg_err <= 1'b1; + // }}} + // last_read_request + // {{{ initial last_read_request = 1'b0; always @(posedge i_clk) if (i_reset) last_read_request <= 1'b0; - else if ((dma_state == `DMA_WAIT)||(dma_state == `DMA_READ_REQ)) + else if ((dma_state == DMA_WAIT)||(dma_state == DMA_READ_REQ)) begin - if ((!i_mwb_stall)&&(dma_state == `DMA_READ_REQ)) + if ((!i_mwb_stall)&&(dma_state == DMA_READ_REQ)) begin last_read_request <= (nracks + 1 == { 1'b0, cfg_blocklen_sub_one}) @@ -432,22 +489,23 @@ module wbdmac(i_clk, i_reset, ||(bus_nracks == cfg_len-1); end else last_read_request <= 1'b0; + // }}} - - wire [(LGMEMLEN):0] next_nread; assign next_nread = nread + 1'b1; + // last_read_ack + // {{{ initial last_read_ack = 1'b0; always @(posedge i_clk) if (i_reset) last_read_ack <= 0; - else if (dma_state == `DMA_READ_REQ) + else if (dma_state == DMA_READ_REQ) begin if ((i_mwb_ack)&&((!o_mwb_stb)||(i_mwb_stall))) last_read_ack <= (next_nread == { 1'b0, cfg_blocklen_sub_one }); else last_read_ack <= (nread == { 1'b0, cfg_blocklen_sub_one }); - end else if (dma_state == `DMA_READ_ACK) + end else if (dma_state == DMA_READ_ACK) begin if ((i_mwb_ack)&&((!o_mwb_stb)||(i_mwb_stall))) last_read_ack <= (nread+2 == nracks); @@ -455,14 +513,17 @@ module wbdmac(i_clk, i_reset, last_read_ack <= (next_nread == nracks); end else last_read_ack <= 1'b0; + // }}} + // last_write_request + // {{{ initial last_write_request = 1'b0; always @(posedge i_clk) if (i_reset) last_write_request <= 1'b0; - else if (dma_state == `DMA_PRE_WRITE) + else if (dma_state == DMA_PRE_WRITE) last_write_request <= (nread <= 1); - else if (dma_state == `DMA_WRITE_REQ) + else if (dma_state == DMA_WRITE_REQ) begin if (i_mwb_stall) last_write_request <= (nwritten >= nread-1); @@ -470,12 +531,15 @@ module wbdmac(i_clk, i_reset, last_write_request <= (nwritten >= nread-2); end else last_write_request <= 1'b0; + // }}} + // last_write_ack + // {{{ initial last_write_ack = 1'b0; always @(posedge i_clk) if (i_reset) last_write_ack <= 1'b0; - else if((dma_state == `DMA_WRITE_REQ)||(dma_state == `DMA_WRITE_ACK)) + else if((dma_state == DMA_WRITE_REQ)||(dma_state == DMA_WRITE_ACK)) begin if ((i_mwb_ack)&&((!o_mwb_stb)||(i_mwb_stall))) last_write_ack <= (nwacks+2 == nwritten); @@ -483,64 +547,75 @@ module wbdmac(i_clk, i_reset, last_write_ack <= (nwacks+1 == nwritten); end else last_write_ack <= 1'b0; + // }}} + assign o_mwb_cyc = (dma_state == DMA_READ_REQ) + ||(dma_state == DMA_READ_ACK) + ||(dma_state == DMA_WRITE_REQ) + ||(dma_state == DMA_WRITE_ACK); - assign o_mwb_cyc = (dma_state == `DMA_READ_REQ) - ||(dma_state == `DMA_READ_ACK) - ||(dma_state == `DMA_WRITE_REQ) - ||(dma_state == `DMA_WRITE_ACK); + assign o_mwb_stb = (dma_state == DMA_READ_REQ) + ||(dma_state == DMA_WRITE_REQ); - assign o_mwb_stb = (dma_state == `DMA_READ_REQ) - ||(dma_state == `DMA_WRITE_REQ); + assign o_mwb_we = (dma_state == DMA_PRE_WRITE) + ||(dma_state == DMA_WRITE_REQ) + ||(dma_state == DMA_WRITE_ACK); - assign o_mwb_we = (dma_state == `DMA_PRE_WRITE) - ||(dma_state == `DMA_WRITE_REQ) - ||(dma_state == `DMA_WRITE_ACK); - - // - // This is tricky. In order for Vivado to consider dma_mem to be a + // rdaddr + // {{{ + // This is tricky. In order for Vivado to consider dma_mem to be a // proper memory, it must have a simple address fed into it. Hence // the read_address (rdaddr) register. The problem is that this // register must always be one greater than the address we actually // want to read from, unless we are idling. So ... the math is touchy. // - reg [(LGMEMLEN-1):0] rdaddr; initial rdaddr = 0; always @(posedge i_clk) if (i_reset) rdaddr <= 0; - else if((dma_state == `DMA_IDLE)||(dma_state == `DMA_WAIT) - ||(dma_state == `DMA_WRITE_ACK)) + else if((dma_state == DMA_IDLE)||(dma_state == DMA_WAIT) + ||(dma_state == DMA_WRITE_ACK)) rdaddr <= 0; - else if ((dma_state == `DMA_PRE_WRITE) - ||((dma_state==`DMA_WRITE_REQ)&&(!i_mwb_stall))) + else if ((dma_state == DMA_PRE_WRITE) + ||((dma_state == DMA_WRITE_REQ)&&(!i_mwb_stall))) rdaddr <= rdaddr + {{(LGMEMLEN-1){1'b0}},1'b1}; + // }}} + // o_mwb_data + // {{{ always @(posedge i_clk) - if (i_reset) - o_mwb_data <= 0; - else if ((dma_state != `DMA_WRITE_REQ)||(!i_mwb_stall)) + // if (i_reset) + // o_mwb_data <= 0; + // else + if ((dma_state != DMA_WRITE_REQ)||(!i_mwb_stall)) o_mwb_data <= dma_mem[rdaddr]; + always @(posedge i_clk) - if((dma_state == `DMA_READ_REQ)||(dma_state == `DMA_READ_ACK)) - dma_mem[nread[(LGMEMLEN-1):0]] <= i_mwb_data; + if((dma_state == DMA_READ_REQ)||(dma_state == DMA_READ_ACK)) + dma_mem[nread[(LGMEMLEN-1):0]] <= i_mwb_data; + // }}} + // o_swb_data + // {{{ always @(posedge i_clk) if (i_reset) o_swb_data <= 0; else casez(s_addr) - 2'b00: o_swb_data <= { (dma_state != `DMA_IDLE), cfg_err, + 2'b00: o_swb_data <= { (dma_state != DMA_IDLE), cfg_err, !cfg_incs, !cfg_incd, 1'b0, nread, cfg_on_dev_trigger, cfg_dev_trigger, cfg_blocklen_sub_one }; - 2'b01: o_swb_data <= { {(DW-AW){1'b0}}, cfg_len }; - 2'b10: o_swb_data <= { {(DW-2-AW){1'b0}}, cfg_raddr, 2'b00 }; - 2'b11: o_swb_data <= { {(DW-2-AW){1'b0}}, cfg_waddr, 2'b00 }; + 2'b01: o_swb_data <= { {(SLV_WIDTH-AW){1'b0}}, cfg_len }; + 2'b10: o_swb_data <= { {(SLV_WIDTH-$clog2(BUS_WIDTH/8)-AW){1'b0}}, cfg_raddr, {($clog2(BUS_WIDTH/8)){1'b0}} }; + 2'b11: o_swb_data <= { {(SLV_WIDTH-$clog2(BUS_WIDTH/8)-AW){1'b0}}, cfg_waddr, {($clog2(BUS_WIDTH/8)){1'b0}} }; endcase + // }}} + // trigger + // {{{ // This causes us to wait a minimum of two clocks before starting: One // to go into the wait state, and then one while in the wait state to // develop the trigger. @@ -549,10 +624,13 @@ module wbdmac(i_clk, i_reset, if (i_reset) trigger <= 1'b0; else - trigger <= (dma_state == `DMA_WAIT) + trigger <= (dma_state == DMA_WAIT) &&((!cfg_on_dev_trigger) ||(i_dev_ints[cfg_dev_trigger])); + // }}} + // o_swb_ack + // {{{ // Ack any access. We'll quietly ignore any access where we are busy, // but ack it anyway. In other words, before writing to the device, // double check that it isn't busy, and then write. @@ -564,34 +642,55 @@ module wbdmac(i_clk, i_reset, o_swb_ack <= 1'b0; else o_swb_ack <= (s_stb); + // }}} assign o_swb_stall = 1'b0; + // abort + // {{{ initial abort = 1'b0; always @(posedge i_clk) if (i_reset) abort <= 1'b0; - else if (dma_state == `DMA_IDLE) + else if (dma_state == DMA_IDLE) abort <= 1'b0; else abort <= ((s_stb)&&(s_we) &&(s_addr == 2'b00) &&(s_data == 32'hffed0000)); + // }}} + // user_halt + // {{{ initial user_halt = 1'b0; always @(posedge i_clk) - user_halt <= ((user_halt)&&(dma_state != `DMA_IDLE)) - ||((s_stb)&&(s_we)&&(dma_state != `DMA_IDLE) + if (i_reset) + user_halt <= 1'b0; + else + user_halt <= ((user_halt)&&(dma_state != DMA_IDLE)) + ||((s_stb)&&(s_we)&&(dma_state != DMA_IDLE) &&(s_addr == 2'b00) &&(s_data == 32'hafed0000)); - + // }}} // Make verilator happy + // {{{ // verilator lint_off UNUSED wire unused; assign unused = s_cyc; // verilator lint_on UNUSED + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // The formal properties for this module are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/wbpriarbiter.v b/rtl/cpu/wbpriarbiter.v index 7dfdc22..54533a1 100644 --- a/rtl/cpu/wbpriarbiter.v +++ b/rtl/cpu/wbpriarbiter.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbpriarbiter.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: This is a priority bus arbiter. It allows two separate wishbone @@ -25,9 +25,9 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015,2018-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or // modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at @@ -42,69 +42,80 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbpriarbiter(i_clk, - // Bus A - i_a_cyc, i_a_stb, i_a_we, i_a_adr, i_a_dat, i_a_sel, o_a_stall, o_a_ack, o_a_err, - // Bus B - i_b_cyc, i_b_stb, i_b_we, i_b_adr, i_b_dat, i_b_sel, o_b_stall, o_b_ack, o_b_err, - // Both buses - o_cyc, o_stb, o_we, o_adr, o_dat, o_sel, i_stall, i_ack, i_err); - parameter DW=32, AW=32; - // - // ZERO_ON_IDLE uses more logic than the alternative. It should be - // useful for reducing power, as these circuits tend to drive wires - // all the way across the design, but it may also slow down the master - // clock. I've used it as an option when using VERILATOR, 'cause - // zeroing things on idle can make them stand out all the more when - // staring at wires and dumps and such. - parameter [0:0] OPT_ZERO_ON_IDLE = 1'b0; - // - input wire i_clk; - // Bus A - input wire i_a_cyc, i_a_stb, i_a_we; - input wire [(AW-1):0] i_a_adr; - input wire [(DW-1):0] i_a_dat; - input wire [(DW/8-1):0] i_a_sel; - output wire o_a_stall, o_a_ack, o_a_err; - // Bus B - input wire i_b_cyc, i_b_stb, i_b_we; - input wire [(AW-1):0] i_b_adr; - input wire [(DW-1):0] i_b_dat; - input wire [(DW/8-1):0] i_b_sel; - output wire o_b_stall, o_b_ack, o_b_err; - // - output wire o_cyc, o_stb, o_we; - output wire [(AW-1):0] o_adr; - output wire [(DW-1):0] o_dat; - output wire [(DW/8-1):0] o_sel; - input wire i_stall, i_ack, i_err; +// }}} +module wbpriarbiter #( + // {{{ + parameter DW=32, AW=32, + // OPT_ZERO_ON_IDLE + // {{{ + // ZERO_ON_IDLE uses more logic than the alternative. It should + // be useful for reducing power, as these circuits tend to drive + // wires all the way across the design, but it may also slow + // down the master clock. I've used it as an option when using + // VER1LATOR, 'cause zeroing things on idle can make them stand + // out all the more when staring at wires and dumps and such. + parameter [0:0] OPT_ZERO_ON_IDLE = 1'b0 + // }}} + // }}} + ) ( + // {{{ + input wire i_clk, + // Bus A + // {{{ + input wire i_a_cyc, i_a_stb, i_a_we, + input wire [(AW-1):0] i_a_adr, + input wire [(DW-1):0] i_a_dat, + input wire [(DW/8-1):0] i_a_sel, + output wire o_a_stall, o_a_ack, o_a_err, + // }}} + // Bus B + // {{{ + input wire i_b_cyc, i_b_stb, i_b_we, + input wire [(AW-1):0] i_b_adr, + input wire [(DW-1):0] i_b_dat, + input wire [(DW/8-1):0] i_b_sel, + output wire o_b_stall, o_b_ack, o_b_err, + // }}} + // Outgoing combined bus + // {{{ + output wire o_cyc, o_stb, o_we, + output wire [(AW-1):0] o_adr, + output wire [(DW-1):0] o_dat, + output wire [(DW/8-1):0] o_sel, + input wire i_stall, i_ack, i_err + // }}} + // }}} + ); + reg r_a_owner; + + // r_a_owner + // {{{ // Go high immediately (new cycle) if ... // Previous cycle was low and *someone* is requesting a bus cycle // Go low immadiately if ... // We were just high and the owner no longer wants the bus // WISHBONE Spec recommends no logic between a FF and the o_cyc // This violates that spec. (Rec 3.15, p35) - reg r_a_owner; - initial r_a_owner = 1'b1; always @(posedge i_clk) - if (!i_b_cyc) - r_a_owner <= 1'b1; - // Allow B to set its CYC line w/o activating this interface - else if ((i_b_cyc)&&(i_b_stb)&&(!i_a_cyc)) - r_a_owner <= 1'b0; + if (!i_b_cyc) + r_a_owner <= 1'b1; + // Allow B to set its CYC line w/o activating this interface + else if ((i_b_cyc)&&(i_b_stb)&&(!i_a_cyc)) + r_a_owner <= 1'b0; + // }}} + // CYC, STB, and WE + // {{{ // Realistically, if neither master owns the bus, the output is a // don't care. Thus we trigger off whether or not 'A' owns the bus. // If 'B' owns it all we care is that 'A' does not. Likewise, if @@ -114,8 +125,13 @@ module wbpriarbiter(i_clk, assign o_cyc = (r_a_owner) ? i_a_cyc : i_b_cyc; assign o_we = (r_a_owner) ? i_a_we : i_b_we; assign o_stb = (r_a_owner) ? i_a_stb : i_b_stb; + // }}} + + // Everything else + // {{{ generate if (OPT_ZERO_ON_IDLE) - begin + begin : OPT_LOWPOWER + // {{{ assign o_adr = (o_stb)?((r_a_owner) ? i_a_adr : i_b_adr):0; assign o_dat = (o_stb)?((r_a_owner) ? i_a_dat : i_b_dat):0; assign o_sel = (o_stb)?((r_a_owner) ? i_a_sel : i_b_sel):0; @@ -125,7 +141,9 @@ module wbpriarbiter(i_clk, assign o_b_stall = (o_cyc)&&(!r_a_owner) ? i_stall : 1'b1; assign o_a_err = (o_cyc)&&( r_a_owner) ? i_err : 1'b0; assign o_b_err = (o_cyc)&&(!r_a_owner) ? i_err : 1'b0; - end else begin + // }}} + end else begin : OPT_LOWLOGIC + // {{{ assign o_adr = (r_a_owner) ? i_a_adr : i_b_adr; assign o_dat = (r_a_owner) ? i_a_dat : i_b_dat; assign o_sel = (r_a_owner) ? i_a_sel : i_b_sel; @@ -145,9 +163,20 @@ module wbpriarbiter(i_clk, // assign o_a_err = ( r_a_owner) ? i_err : 1'b0; assign o_b_err = (!r_a_owner) ? i_err : 1'b0; + // }}} end endgenerate - + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // Formal properties for this module are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/wbwatchdog.v b/rtl/cpu/wbwatchdog.v index 2210379..6db4998 100644 --- a/rtl/cpu/wbwatchdog.v +++ b/rtl/cpu/wbwatchdog.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbwatchdog.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: A Zip timer, redesigned to be a bus watchdog @@ -22,11 +22,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015,2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -39,92 +39,60 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbwatchdog(i_clk, i_reset, i_timeout, o_int); - parameter BW = 32; - input wire i_clk, i_reset; - // Inputs (these were at one time wishbone controlled ...) - input wire [(BW-1):0] i_timeout; - // Interrupt line - output reg o_int; +// }}} +module wbwatchdog #( + parameter BW = 32 + ) ( + // {{{ + input wire i_clk, i_reset, + // Inputs (these were at one time wishbone controlled ...) + input wire [(BW-1):0] i_timeout, + // Interrupt line + output reg o_int + // }}} + ); reg [(BW-1):0] r_value; + + // r_value + // {{{ initial r_value = {(BW){1'b1}}; always @(posedge i_clk) - if (i_reset) - r_value <= i_timeout[(BW-1):0]; - else if (!o_int) - r_value <= r_value + {(BW){1'b1}}; // r_value - 1; + if (i_reset) + r_value <= i_timeout[(BW-1):0]; + else if (!o_int) + r_value <= r_value + {(BW){1'b1}}; // r_value - 1; + // }}} // Set the interrupt on our last tick. + // {{{ initial o_int = 1'b0; always @(posedge i_clk) - if (i_reset) - o_int <= 1'b0; - else if (!o_int) - o_int <= (r_value == { {(BW-1){1'b0}}, 1'b1 }); - + if (i_reset) + o_int <= 1'b0; + else if (!o_int) + o_int <= (r_value == { {(BW-1){1'b0}}, 1'b1 }); + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL - reg f_past_valid; - - initial f_past_valid = 1'b0; - always @(posedge f_past_valid) - f_past_valid <= 1'b1; - - /////////////////////////////////////////////// - // - // - // Assumptions about our inputs - // - // - /////////////////////////////////////////////// - always @(*) - assume(i_timeout > 1); - always @(posedge i_clk) - if (f_past_valid) - assume(i_timeout == $past(i_timeout)); - - // - // - /////////////////////////////////////////////// - // - // - // Assertions about our internal state and our outputs - // - // - /////////////////////////////////////////////// - // - // - always @(posedge i_clk) - if ((f_past_valid)&&($past(o_int))&&(!$past(i_reset))) - assert(o_int); - - always @(*) - assert(o_int == (r_value == 0)); - - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset))&&(!$past(o_int))) - begin - assert(r_value == $past(r_value)-1'b1); - end - - always @(posedge i_clk) - if ((!f_past_valid)||($past(i_reset))) - begin - if (!f_past_valid) - assert(r_value == {(BW){1'b1}}); - else // if ($past(i_reset)) - assert(r_value == $past(i_timeout)); - assert(!o_int); - end +// Formal properties are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/zipaxi.v b/rtl/cpu/zipaxi.v new file mode 100644 index 0000000..1f5973b --- /dev/null +++ b/rtl/cpu/zipaxi.v @@ -0,0 +1,1450 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: zipaxi.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: A potential top level module holding the core of the Zip CPU +// together--this one with AXI4 instruction and data interfaces, +// and an AXI-lite debug interfaces. In general, the Zip CPU is designed +// to be as simple as possible. (actual implementation aside ...) The +// instruction set is about as RISC as you can get, with only 26 +// instruction types currently supported. (There are still 8-instruction +// Op-Codes reserved for floating point, and 5 which can be used for +// transactions not requiring registers.) Please see the accompanying +// spec.pdf file for a description of these instructions. +// +// This version is bus width agnostic for both instruction and data buses, +// although the debug bus must still be 32-bits. Instruction and data +// buses must be at least 32-bits wide. +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module zipaxi #( + // {{{ + parameter C_DBG_ADDR_WIDTH = 8, + localparam C_DBG_DATA_WIDTH = 32, + localparam DBGLSB = $clog2(C_DBG_DATA_WIDTH/8), + parameter ADDRESS_WIDTH = 32, + parameter C_AXI_DATA_WIDTH = 32, + parameter C_AXI_ID_WIDTH = 1, + parameter INSN_ID = 0, + parameter DATA_ID = 0, + localparam AXILSB = $clog2(C_AXI_DATA_WIDTH/8), + parameter OPT_LGICACHE = 0, + parameter OPT_LGDCACHE = 0, + parameter [0:0] OPT_PIPELINED = 1'b1, + parameter [ADDRESS_WIDTH-1:0] RESET_ADDRESS={(ADDRESS_WIDTH){1'b0}}, + parameter [0:0] START_HALTED = 1'b0, + parameter [0:0] OPT_WRAP = 1'b1, + parameter [0:0] SWAP_WSTRB = 1'b1, + parameter OPT_MPY = 3, + parameter [0:0] OPT_DIV = 1'b1, + parameter [0:0] OPT_SHIFTS = 1'b1, + parameter [0:0] OPT_LOCK = 1'b1, + parameter [0:0] OPT_FPU = 0, + parameter [0:0] OPT_EARLY_BRANCHING = 1, + parameter [0:0] OPT_CIS = 1'b1, + parameter [0:0] OPT_LOWPOWER = 1'b0, + parameter [0:0] OPT_DISTRIBUTED_REGS = 1'b1, + parameter [0:0] OPT_DBGPORT = START_HALTED, + parameter [0:0] OPT_TRACE_PORT = 1'b0, + parameter [0:0] OPT_PROFILER = 1'b0, + parameter [0:0] OPT_USERMODE = 1'b1, + parameter LGILINESZ= 3, + parameter OPT_LGDLINESZ = 3, + parameter RESET_DURATION = 10, + // localparam [0:0] WITH_LOCAL_BUS = 1'b0, + localparam AW=ADDRESS_WIDTH-2, +`ifdef VERILATOR + parameter [0:0] OPT_SIM = 1'b1, + parameter [0:0] OPT_CLKGATE = OPT_LOWPOWER +`else + parameter [0:0] OPT_SIM = 1'b0, + parameter [0:0] OPT_CLKGATE = 1'b0 +`endif +`ifdef FORMAL + , parameter F_LGDEPTH=8 +`endif + // }}} + ) ( + // {{{ + input wire S_AXI_ACLK, S_AXI_ARESETN, + i_interrupt, i_cpu_reset, + // Debug interface + // {{{ + // Debug interface -- inputs +// input wire i_halt, i_clear_cache, +// input wire [4:0] i_dbg_wreg, +// input wire i_dbg_we, +// input wire [31:0] i_dbg_data, +// input wire [4:0] i_dbg_rreg, + // Debug interface -- outputs +// output wire cpu_dbg_stall, +// output reg [31:0] o_dbg_reg, +// output reg [2:0] o_dbg_cc, +// output wire o_break, + input wire S_DBG_AWVALID, + output wire S_DBG_AWREADY, + input wire [C_DBG_ADDR_WIDTH-1:0] S_DBG_AWADDR, + input wire [2:0] S_DBG_AWPROT, + // + input wire S_DBG_WVALID, + output wire S_DBG_WREADY, + input wire [31:0] S_DBG_WDATA, + input wire [3:0] S_DBG_WSTRB, + // + output reg S_DBG_BVALID, + input wire S_DBG_BREADY, + output wire [1:0] S_DBG_BRESP, + // + input wire S_DBG_ARVALID, + output wire S_DBG_ARREADY, + input wire [7:0] S_DBG_ARADDR, + input wire [2:0] S_DBG_ARPROT, + // + output reg S_DBG_RVALID, + input wire S_DBG_RREADY, + output reg [31:0] S_DBG_RDATA, + output wire [1:0] S_DBG_RRESP, + // + // }}} + // Instruction bus (master) + // {{{ + output wire M_INSN_AWVALID, + input wire M_INSN_AWREADY, + output wire [C_AXI_ID_WIDTH-1:0] M_INSN_AWID, + output wire [ADDRESS_WIDTH-1:0] M_INSN_AWADDR, + output wire [7:0] M_INSN_AWLEN, + output wire [2:0] M_INSN_AWSIZE, + output wire [1:0] M_INSN_AWBURST, + output wire M_INSN_AWLOCK, + output wire [3:0] M_INSN_AWCACHE, + output wire [2:0] M_INSN_AWPROT, + output wire [3:0] M_INSN_AWQOS, + // + output wire M_INSN_WVALID, + input wire M_INSN_WREADY, + output wire [C_AXI_DATA_WIDTH-1:0] M_INSN_WDATA, + output wire [C_AXI_DATA_WIDTH/8-1:0] M_INSN_WSTRB, + output wire M_INSN_WLAST, + // + input wire M_INSN_BVALID, + output wire M_INSN_BREADY, + input wire [C_AXI_ID_WIDTH-1:0] M_INSN_BID, + input wire [1:0] M_INSN_BRESP, + // + output wire M_INSN_ARVALID, + input wire M_INSN_ARREADY, + output wire [C_AXI_ID_WIDTH-1:0] M_INSN_ARID, + output wire [ADDRESS_WIDTH-1:0] M_INSN_ARADDR, + output wire [7:0] M_INSN_ARLEN, + output wire [2:0] M_INSN_ARSIZE, + output wire [1:0] M_INSN_ARBURST, + output wire M_INSN_ARLOCK, + output wire [3:0] M_INSN_ARCACHE, + output wire [2:0] M_INSN_ARPROT, + output wire [3:0] M_INSN_ARQOS, + // + input wire M_INSN_RVALID, + output wire M_INSN_RREADY, + input wire [C_AXI_ID_WIDTH-1:0] M_INSN_RID, + input wire [C_AXI_DATA_WIDTH-1:0] M_INSN_RDATA, + input wire M_INSN_RLAST, + input wire [1:0] M_INSN_RRESP, + // }}} + // Data bus (master) + // {{{ + output wire M_DATA_AWVALID, + input wire M_DATA_AWREADY, + output wire [C_AXI_ID_WIDTH-1:0] M_DATA_AWID, + output wire [ADDRESS_WIDTH-1:0] M_DATA_AWADDR, + output wire [7:0] M_DATA_AWLEN, + output wire [2:0] M_DATA_AWSIZE, + output wire [1:0] M_DATA_AWBURST, + output wire M_DATA_AWLOCK, + output wire [3:0] M_DATA_AWCACHE, + output wire [2:0] M_DATA_AWPROT, + output wire [3:0] M_DATA_AWQOS, + // + output wire M_DATA_WVALID, + input wire M_DATA_WREADY, + output wire [C_AXI_DATA_WIDTH-1:0] M_DATA_WDATA, + output wire [C_AXI_DATA_WIDTH/8-1:0] M_DATA_WSTRB, + output wire M_DATA_WLAST, + // + input wire M_DATA_BVALID, + output wire M_DATA_BREADY, + input wire [C_AXI_ID_WIDTH-1:0] M_DATA_BID, + input wire [1:0] M_DATA_BRESP, + // + output wire M_DATA_ARVALID, + input wire M_DATA_ARREADY, + output wire [C_AXI_ID_WIDTH-1:0] M_DATA_ARID, + output wire [ADDRESS_WIDTH-1:0] M_DATA_ARADDR, + output wire [7:0] M_DATA_ARLEN, + output wire [2:0] M_DATA_ARSIZE, + output wire [1:0] M_DATA_ARBURST, + output wire M_DATA_ARLOCK, + output wire [3:0] M_DATA_ARCACHE, + output wire [2:0] M_DATA_ARPROT, + output wire [3:0] M_DATA_ARQOS, + // + input wire M_DATA_RVALID, + output wire M_DATA_RREADY, + input wire [C_AXI_ID_WIDTH-1:0] M_DATA_RID, + input wire [C_AXI_DATA_WIDTH-1:0] M_DATA_RDATA, + input wire M_DATA_RLAST, + input wire [1:0] M_DATA_RRESP, + // }}} + // Accounting outputs ... to help us count stalls and usage + output wire o_cmd_reset, + output wire o_halted, + output wire o_gie, + output wire o_op_stall, + output wire o_pf_stall, + output wire o_i_count, + // + output wire [31:0] o_cpu_debug, + // + output wire o_prof_stb, + output wire [ADDRESS_WIDTH-1:0] o_prof_addr, + output wire [31:0] o_prof_ticks + // }}} + ); + + // Declarations + // {{{ + localparam [0:0] DBG_ADDR_CTRL = 1'b0, + DBG_ADDR_CPU = 1'b1; + + localparam [0:0] OPT_PIPELINED_BUS_ACCESS = (OPT_PIPELINED)&&(OPT_LGDCACHE > 1); + localparam [0:0] OPT_MEMPIPE = OPT_PIPELINED_BUS_ACCESS; + localparam [0:0] OPT_DCACHE = (OPT_LGDCACHE > 4); + + localparam FETCH_LIMIT = (OPT_LGICACHE < 4) ? (1 << OPT_LGICACHE) : 16; + + // Debug bit allocations + // {{{ + // DBGCTRL + // 5 DBG Catch -- Catch exceptions/fautls w/ debugger + // 4 Clear cache + // 3 RESET_FLAG + // 2 STEP (W=1 steps, and returns to halted) + // 1 HALT(ED) + // 0 HALT + // DBGDATA + // read/writes internal registers + // + localparam HALT_BIT = 0, + STEP_BIT = 2, + RESET_BIT = 3, + CLEAR_CACHE_BIT = 4, + CATCH_BIT = 5; + // }}} + localparam [0:0] OPT_ALIGNMENT_ERR = 1'b0; + localparam [0:0] SWAP_ENDIANNESS = 1'b0; + + // AXI-lite signal handling + // {{{ + wire awskd_valid, wskd_valid, arskd_valid; + wire dbg_write_ready, dbg_read_ready; + wire [C_DBG_ADDR_WIDTH-DBGLSB-1:0] awskd_addr, arskd_addr; + wire [31:0] wskd_data; + wire [3:0] wskd_strb; + reg dbg_write_valid, dbg_read_valid; + wire w_dbg_write_valid; + reg [4:0] dbg_write_reg; + wire [4:0] dbg_read_reg; + reg [31:0] dbg_write_data; + wire [31:0] dbg_read_data; + wire cpu_dbg_stall, cpu_break, dbg_write_stall; + wire [2:0] cpu_dbg_cc; + // }}} + + wire reset_hold, halt_on_fault, dbg_catch; + wire cpu_clken, cpu_clock, clk_gate; + wire reset_request, release_request, halt_request, step_request, + clear_cache_request; + + + // CPU control registers + // {{{ + reg cmd_halt, cmd_reset, cmd_step, cmd_clear_cache; + wire [31:0] cpu_status; + wire dbg_cmd_write, dbg_cpu_write; + wire [31:0] dbg_cmd_data; + wire [3:0] dbg_cmd_strb; + // }}} + + // Fetch + // {{{ + wire pf_new_pc, clear_icache, pf_ready; + wire [AW+1:0] pf_request_address; + wire [31:0] pf_instruction; + wire [AW+1:0] pf_instruction_pc; + wire pf_valid, pf_illegal; + // }}} + // Memory + // {{{ + wire clear_dcache, mem_ce, bus_lock; + wire [2:0] mem_op; + wire [31:0] mem_cpu_addr; + wire [AW+1:0] mem_lock_pc; + wire [31:0] mem_wdata; + wire [4:0] mem_reg; + wire mem_busy, mem_rdbusy, mem_pipe_stalled, mem_valid, + mem_bus_err; + wire [4:0] mem_wreg; + wire [31:0] mem_result; + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Debug signal handling + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // + // Write signaling + // {{{ + skidbuffer #( + // {{{ + .OPT_OUTREG(0), + .OPT_LOWPOWER(OPT_LOWPOWER), + .DW(C_DBG_ADDR_WIDTH-DBGLSB) + // }}} + ) dbgawskd( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(!S_AXI_ARESETN), + .i_valid(S_DBG_AWVALID), + .o_ready(S_DBG_AWREADY), + .i_data(S_DBG_AWADDR[C_DBG_ADDR_WIDTH-1:DBGLSB]), + .o_valid(awskd_valid), .i_ready(dbg_write_ready), + .o_data(awskd_addr) + // }}} + ); + + skidbuffer #( + // {{{ + .OPT_OUTREG(0), + .OPT_LOWPOWER(OPT_LOWPOWER), + .DW(C_DBG_DATA_WIDTH+C_DBG_DATA_WIDTH/8) + // }}} + ) dbgwskd( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(!S_AXI_ARESETN), + .i_valid(S_DBG_WVALID), + .o_ready(S_DBG_WREADY), + .i_data({ S_DBG_WSTRB, S_DBG_WDATA }), + .o_valid(wskd_valid), .i_ready(dbg_write_ready), + .o_data({ wskd_strb, wskd_data }) + // }}} + ); + + assign dbg_write_ready = awskd_valid && wskd_valid + && ((wskd_strb==0) || awskd_addr[5] + || !dbg_write_stall) + && (!S_DBG_BVALID || S_DBG_BREADY); + + // dbg_write_valid + // {{{ + assign w_dbg_write_valid = dbg_write_ready && (|wskd_strb) + && awskd_addr[5] == DBG_ADDR_CPU; + initial dbg_write_valid = 0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + dbg_write_valid <= 1'b0; + else if (!dbg_write_stall) + dbg_write_valid <= w_dbg_write_valid; + // }}} + + // dbg_write_reg + // {{{ + always @(posedge S_AXI_ACLK) + if (!dbg_write_stall) + begin + dbg_write_reg <= awskd_addr[4:0]; + if (OPT_LOWPOWER && !w_dbg_write_valid) + dbg_write_reg <= 0; + end + // }}} + + // dbg_write_data + // {{{ + always @(posedge S_AXI_ACLK) + if (!dbg_write_stall) + begin + dbg_write_data <= wskd_data; + if (OPT_LOWPOWER && !w_dbg_write_valid) + dbg_write_data <= 0; + end + // }}} + + // S_DBG_BVALID + // {{{ + initial S_DBG_BVALID = 1'b0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + S_DBG_BVALID <= 1'b0; + else if (dbg_write_ready) + S_DBG_BVALID <= 1'b1; + else if (S_DBG_BREADY) + S_DBG_BVALID <= 1'b0; + // }}} + + // S_DBG_BRESP + // {{{ + assign S_DBG_BRESP = 2'b00; + // }}} + + // }}} + + // + // Read signaling + // {{{ + + skidbuffer #( + // {{{ + .OPT_OUTREG(0), + .OPT_LOWPOWER(OPT_LOWPOWER), + .DW(C_DBG_ADDR_WIDTH-DBGLSB) + // }}} + ) dbgarskd( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(!S_AXI_ARESETN), + .i_valid(S_DBG_ARVALID), + .o_ready(S_DBG_ARREADY), + .i_data(S_DBG_ARADDR[C_DBG_ADDR_WIDTH-1:DBGLSB]), + .o_valid(arskd_valid), .i_ready(dbg_read_ready), + .o_data(arskd_addr) + // }}} + ); + + assign dbg_read_ready = arskd_valid && !dbg_read_valid + && (!S_DBG_RVALID || S_DBG_RREADY); + + assign dbg_read_reg = (OPT_LOWPOWER && !dbg_read_ready) + ? 5'h0 : arskd_addr[4:0]; + + // dbg_read_valid + // {{{ + initial dbg_read_valid = 0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + dbg_read_valid <= 0; + else + dbg_read_valid <= dbg_read_ready + && arskd_addr[5] == DBG_ADDR_CPU; + // }}} + + // S_DBG_RVALID + // {{{ + initial S_DBG_RVALID = 0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + S_DBG_RVALID <= 0; + else if (!S_DBG_RVALID || S_DBG_RREADY) + S_DBG_RVALID <= (dbg_read_ready + && arskd_addr[5] == DBG_ADDR_CTRL) + || dbg_read_valid; + // }}} + + // S_DBG_RDATA + // {{{ + initial S_DBG_RDATA = 0; + always @(posedge S_AXI_ACLK) + if (OPT_LOWPOWER && !S_AXI_ARESETN) + S_DBG_RDATA <= 0; + else if (!S_DBG_RVALID || S_DBG_RREADY) + begin + // {{{ + if (dbg_read_valid) + S_DBG_RDATA <= dbg_read_data; + else + S_DBG_RDATA <= cpu_status; + + if (OPT_LOWPOWER && !dbg_read_valid && !dbg_read_ready) + S_DBG_RDATA <= 0; + // }}} + end + // }}} + + assign S_DBG_RRESP = 2'b00; + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Reset, halt, clear-cache, and step controls + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + assign dbg_cpu_write = dbg_write_ready && awskd_addr[5] == DBG_ADDR_CPU; + assign dbg_cmd_write = dbg_write_ready && awskd_addr[5] == DBG_ADDR_CTRL; + assign dbg_cmd_data = wskd_data; + assign dbg_cmd_strb = wskd_strb; + + assign reset_request = dbg_cmd_write && dbg_cmd_strb[RESET_BIT/8] + && dbg_cmd_data[RESET_BIT]; + assign release_request = dbg_cmd_write && dbg_cmd_strb[HALT_BIT/8] + && !dbg_cmd_data[HALT_BIT]; + assign halt_request = dbg_cmd_write && dbg_cmd_strb[HALT_BIT/8] + && dbg_cmd_data[HALT_BIT]; + assign step_request = dbg_cmd_write && dbg_cmd_strb[STEP_BIT/8] + && dbg_cmd_data[STEP_BIT]; + assign clear_cache_request = dbg_cmd_write + && dbg_cmd_strb[CLEAR_CACHE_BIT/8] + && dbg_cmd_data[CLEAR_CACHE_BIT]; + + // + // reset_hold: Always start us off with an initial reset + // {{{ + generate if (RESET_DURATION > 0) + begin : INITIAL_RESET_HOLD + // {{{ + reg [$clog2(RESET_DURATION)-1:0] reset_counter; + reg r_reset_hold; + + initial reset_counter = RESET_DURATION; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN || i_cpu_reset) + reset_counter <= RESET_DURATION; + else if (reset_counter > 0) + reset_counter <= reset_counter - 1; + + initial r_reset_hold = 1; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN || i_cpu_reset) + r_reset_hold <= 1; + else + r_reset_hold <= (reset_counter > 1); + + assign reset_hold = r_reset_hold; +`ifdef FORMAL + always @(*) + assert(reset_hold == (reset_counter != 0)); +`endif + // }}} + end else begin + + assign reset_hold = 0; + + end endgenerate + // }}} + + assign halt_on_fault = dbg_catch; + + // cmd_reset + // {{{ + // Always start us off with an initial reset + initial cmd_reset = 1'b1; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN || i_cpu_reset) + cmd_reset <= 1'b1; + else if (reset_hold) + cmd_reset <= 1'b1; + else if (cpu_break && !halt_on_fault) + cmd_reset <= 1'b1; + else + cmd_reset <= reset_request; + // }}} + + // cmd_halt + // {{{ + initial cmd_halt = START_HALTED; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + cmd_halt <= START_HALTED; + else if (i_cpu_reset && !dbg_write_ready && !dbg_write_stall) + cmd_halt <= START_HALTED; + else if (cmd_reset && START_HALTED) + cmd_halt <= START_HALTED; + else begin + // {{{ + // When shall we release from a halt? Only if we have come to + // a full and complete stop. Even then, we only release if we + // aren't being given a command to step the CPU. + // + if (!dbg_write_valid && !cpu_dbg_stall && dbg_cmd_write + && (release_request || step_request)) + cmd_halt <= 1'b0; + + // Reasons to halt + + // 1. Halt on any unhandled CPU exception. The cause of the + // exception must be cured before we can (re)start. + // If the CPU is configured to start immediately on power + // up, we leave it to reset on any exception instead. + if (cpu_break && halt_on_fault) + cmd_halt <= 1'b1; + + // 2. Halt on any user request to halt. (Only valid if the + // STEP bit isn't also set) + if (dbg_cmd_write && halt_request && !step_request) + cmd_halt <= 1'b1; + + // 3. Halt on any user request to write to a CPU register + if (dbg_cpu_write && dbg_cmd_strb != 0) + cmd_halt <= 1'b1; + + // 4. Halt following any step command + if (cmd_step) + cmd_halt <= 1'b1; + + // 4. Halt following any clear cache + if (cmd_clear_cache) + cmd_halt <= 1'b1; + + // 5. Halt on any clear cache bit--independent of any step bit + if (clear_cache_request) + cmd_halt <= 1'b1; + // }}} + end + // }}} + + // cmd_clear_cache + // {{{ + initial cmd_clear_cache = 1'b0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN || cmd_reset) + cmd_clear_cache <= 1'b0; + else if (dbg_cmd_write && clear_cache_request && halt_request) + cmd_clear_cache <= 1'b1; + else if (cmd_halt && !cpu_dbg_stall) + cmd_clear_cache <= 1'b0; + // }}} + + // cmd_step + // {{{ + initial cmd_step = 1'b0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN || i_cpu_reset) + cmd_step <= 1'b0; + else if (step_request) + cmd_step <= 1'b1; + else if (!cpu_dbg_stall) + cmd_step <= 1'b0; + // }}} + + // dbg_catch + // {{{ + generate if (!OPT_DBGPORT) + begin : NO_DBG_CATCH + assign dbg_catch = START_HALTED; + end else begin : GEN_DBG_CATCH + reg r_dbg_catch; + + initial r_dbg_catch = START_HALTED; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + r_dbg_catch <= START_HALTED; + else if (dbg_cmd_write && dbg_cmd_strb[CATCH_BIT/8]) + r_dbg_catch <= dbg_cmd_data[CATCH_BIT]; + + assign dbg_catch = r_dbg_catch; + end endgenerate + // }}} + + // cpu_status + // {{{ + // 0xffff_f000 -> (Unused / reserved) + // + // 0x0000_0800 -> cpu_break + // 0x0000_0400 -> Interrupt pending + // 0x0000_0200 -> User mode + // 0x0000_0100 -> Sleep (CPU is sleeping) + // + // 0x0000_00c0 -> (Unused/reserved) + // 0x0000_0020 -> dbg_catch + // 0x0000_0010 -> cmd_clear_cache + // + // 0x0000_0008 -> Reset + // 0x0000_0004 -> Step (auto clearing, write only) + // 0x0000_0002 -> Halt (status) + // 0x0000_0001 -> Halt (request) + assign cpu_status = { 16'h0, 4'h0, + cpu_break, i_interrupt, cpu_dbg_cc[1:0], + 2'h0, dbg_catch, 1'b0, + cmd_reset, 1'b0, !cpu_dbg_stall, cmd_halt + }; + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // The ZipCPU Core + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // +`ifdef FORMAL + // {{{ + (* anyseq *) reg f_cpu_halted, f_cpu_data, f_cpu_stall, + f_cpu_break; + (* anyseq *) reg [2:0] f_cpu_dbg_cc; + (* anyseq *) reg [2:0] f_cpu_dbg_read_data; + + assign cpu_dbg_stall = f_cpu_stall; + assign cpu_break = f_cpu_break; + assign cpu_dbg_cc = f_cpu_dbg_cc; + assign dbg_read_data = f_cpu_dbg_read_data; + + fdebug #( + // {{{ + .OPT_START_HALTED(START_HALTED), + .OPT_DISTRIBUTED_RAM(OPT_DISTRIBUTED_REGS) + // }}} + ) fdbg ( + // {{{ + .i_clk(S_AXI_ACLK), + .i_reset(!S_AXI_ARESETN), + .i_cpu_reset(cmd_reset || i_cpu_reset), + .i_halt(cmd_halt), + .i_halted(f_cpu_halted), + .i_clear_cache(cmd_clear_cache), + .i_dbg_we(dbg_write_valid), + .i_dbg_reg(dbg_write_reg), + .i_dbg_data(dbg_write_data), + .i_dbg_stall(cpu_dbg_stall), + .i_dbg_break(cpu_break), + .i_dbg_cc(cpu_dbg_cc) + // }}} + ); + // }}} +`else + zipcore #( + // {{{ + .RESET_ADDRESS({ {(32-ADDRESS_WIDTH){1'b0}}, RESET_ADDRESS }), + .ADDRESS_WIDTH(ADDRESS_WIDTH-2), + .OPT_PIPELINED(OPT_PIPELINED), + .OPT_EARLY_BRANCHING(OPT_EARLY_BRANCHING), + .OPT_DCACHE(OPT_DCACHE), + .OPT_MPY(OPT_MPY), + .OPT_DIV(OPT_DIV), + .OPT_SHIFTS(OPT_SHIFTS), + .IMPLEMENT_FPU(OPT_FPU), + .OPT_CIS(OPT_CIS), + .OPT_LOCK(OPT_LOCK), + .OPT_LOWPOWER(OPT_LOWPOWER), + .OPT_START_HALTED(START_HALTED), + .OPT_SIM(OPT_SIM), + .OPT_PIPELINED_BUS_ACCESS(OPT_MEMPIPE), + // localparam [0:0] OPT_MEMPIPE = OPT_PIPELINED_BUS_ACCESS; + .OPT_DBGPORT(OPT_DBGPORT), + .OPT_TRACE_PORT(OPT_TRACE_PORT), + .OPT_PROFILER(OPT_PROFILER), + // localparam [0:0] OPT_LOCK=(IMPLEMENT_LOCK)&&(OPT_PIPELINED); + // parameter [0:0] WITH_LOCAL_BUS = 1'b1; + .OPT_CLKGATE(OPT_CLKGATE), + .OPT_DISTRIBUTED_REGS(OPT_DISTRIBUTED_REGS), + .OPT_USERMODE(OPT_USERMODE) +`ifdef FORMAL + , .F_LGDEPTH(F_LGDEPTH) +`endif + // }}} + ) core ( + // {{{ + .i_clk(cpu_clock), .i_reset(cmd_reset), + .i_interrupt(i_interrupt), + .o_clken(cpu_clken), + // Debug interface + // {{{ + .i_halt(cmd_halt), .i_clear_cache(cmd_clear_cache), + .i_dbg_wreg(dbg_write_reg), .i_dbg_we(dbg_write_valid), + .i_dbg_data(dbg_write_data),.i_dbg_rreg(dbg_read_reg), + .o_dbg_stall(cpu_dbg_stall),.o_dbg_reg(dbg_read_data), + .o_dbg_cc(cpu_dbg_cc), .o_break(cpu_break), + // }}} + // Instruction fetch interface + // {{{ + .o_pf_new_pc(pf_new_pc), .o_clear_icache(clear_icache), + .o_pf_ready(pf_ready), + .o_pf_request_address(pf_request_address), + .i_pf_valid(pf_valid), + .i_pf_illegal(pf_illegal), + .i_pf_instruction(pf_instruction), + .i_pf_instruction_pc(pf_instruction_pc), + // }}} + // Memory unit interface + // {{{ + .o_clear_dcache(clear_dcache), .o_mem_ce(mem_ce), + .o_bus_lock(bus_lock), + .o_mem_op(mem_op), .o_mem_addr(mem_cpu_addr), + .o_mem_data(mem_wdata), + .o_mem_lock_pc(mem_lock_pc), + .o_mem_reg(mem_reg), + .i_mem_busy(mem_busy), + .i_mem_rdbusy(mem_rdbusy), + .i_mem_pipe_stalled(mem_pipe_stalled), + .i_mem_valid(mem_valid), + .i_bus_err(mem_bus_err), + .i_mem_wreg(mem_wreg), + .i_mem_result(mem_result), + // }}} + // Accounting/CPU usage interface + .o_op_stall(o_op_stall), .o_pf_stall(o_pf_stall), + .o_i_count(o_i_count), + // + .o_debug(o_cpu_debug), + // + .o_prof_stb( o_prof_stb), + .o_prof_addr( o_prof_addr), + .o_prof_ticks(o_prof_ticks) + // }}} + ); +`endif + assign o_cmd_reset = cmd_reset; + assign o_gie = cpu_dbg_cc[1]; + assign o_halted = !cpu_dbg_stall; + assign dbg_write_stall = dbg_write_valid&&(cpu_dbg_stall || !clk_gate); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Instruction Fetch + // {{{ + //////////////////////////////////////////////////////////////////////// + // + +`ifndef FORMAL + generate if (OPT_LGICACHE > 3) + begin : INSN_CACHE + + axiicache #( + // {{{ + .C_AXI_ID_WIDTH(C_AXI_ID_WIDTH), + .C_AXI_ADDR_WIDTH(ADDRESS_WIDTH), + .C_AXI_DATA_WIDTH(C_AXI_DATA_WIDTH), + .AXI_ID(INSN_ID), + .LGCACHESZ(OPT_LGICACHE), + .LGLINESZ(LGILINESZ), + .OPT_WRAP(OPT_WRAP), + .OPT_LOWPOWER(OPT_LOWPOWER), + // Instruction fetches don't need subword access, + // so SWAPWSTRB doesn't make any sense here. + // .SWAP_WSTRB(SWAP_WSTRB), + .SWAP_ENDIANNESS(SWAP_ENDIANNESS) + // }}} + ) pf ( + // {{{ + .S_AXI_ACLK(S_AXI_ACLK), .S_AXI_ARESETN(S_AXI_ARESETN), + // CPU signals + // {{{ + .i_cpu_reset(cmd_reset), + .i_new_pc(pf_new_pc), + .i_clear_cache(clear_icache), + .i_ready(pf_ready && clk_gate), + .i_pc(pf_request_address), + .o_insn(pf_instruction), + .o_pc(pf_instruction_pc), + .o_valid(pf_valid), + .o_illegal(pf_illegal), + // }}} + // AXI4 (full) bus signals + // {{{ + .M_AXI_ARVALID(M_INSN_ARVALID), + .M_AXI_ARREADY(M_INSN_ARREADY), + .M_AXI_ARID( M_INSN_ARID), + .M_AXI_ARADDR( M_INSN_ARADDR), + .M_AXI_ARLEN( M_INSN_ARLEN), + .M_AXI_ARSIZE( M_INSN_ARSIZE), + .M_AXI_ARBURST(M_INSN_ARBURST), + .M_AXI_ARLOCK( M_INSN_ARLOCK), + .M_AXI_ARCACHE(M_INSN_ARCACHE), + .M_AXI_ARPROT( M_INSN_ARPROT), + .M_AXI_ARQOS( M_INSN_ARQOS), + // + .M_AXI_RVALID(M_INSN_RVALID), + .M_AXI_RREADY(M_INSN_RREADY), + .M_AXI_RID( M_INSN_RID), + .M_AXI_RDATA( M_INSN_RDATA), + .M_AXI_RLAST( M_INSN_RLAST), + .M_AXI_RRESP( M_INSN_RRESP) + // }}} + // }}} + ); + + end else begin : AXILFETCH + + axilfetch #( + // {{{ + .C_AXI_ADDR_WIDTH(ADDRESS_WIDTH), + .C_AXI_DATA_WIDTH(C_AXI_DATA_WIDTH), + .FETCH_LIMIT(FETCH_LIMIT), + // .SWAP_WSTRB(SWAP_WSTRB), + .SWAP_ENDIANNESS(SWAP_ENDIANNESS) + // }}} + ) pf ( + // {{{ + .S_AXI_ACLK(S_AXI_ACLK), .S_AXI_ARESETN(S_AXI_ARESETN), + // CPU signals + // {{{ + .i_cpu_reset(cmd_reset), + .i_new_pc(pf_new_pc), + .i_clear_cache(clear_icache), + .i_ready(pf_ready && clk_gate), + .i_pc(pf_request_address), + .o_insn(pf_instruction), + .o_pc(pf_instruction_pc), + .o_valid(pf_valid), + .o_illegal(pf_illegal), + // }}} + // AXI-lite bus signals + // {{{ + .M_AXI_ARVALID(M_INSN_ARVALID), + .M_AXI_ARREADY(M_INSN_ARREADY), + .M_AXI_ARADDR(M_INSN_ARADDR), + .M_AXI_ARPROT(M_INSN_ARPROT), + // + .M_AXI_RVALID(M_INSN_RVALID), + .M_AXI_RREADY(M_INSN_RREADY), + .M_AXI_RDATA(M_INSN_RDATA), + .M_AXI_RRESP(M_INSN_RRESP) + // }}} + // }}} + ); + + assign M_INSN_ARID = INSN_ID; + // ARADDR + assign M_INSN_ARLEN = 0; + assign M_INSN_ARSIZE = AXILSB[2:0]; + assign M_INSN_ARBURST = 2'b01; + assign M_INSN_ARLOCK = 1'b0; + assign M_INSN_ARCACHE = 4'h3; + // PROT + assign M_INSN_ARQOS = 4'h0; + + // Verilator lint_off UNUSED + wire unused_insn_axi; + assign unused_insn_axi = &{ 1'b0, M_INSN_RID, M_INSN_RLAST }; + // Verilator lint_on UNUSED + end endgenerate +`endif + + // Assign values to the (unused) M_INSN_* write ports + // {{{ + assign M_INSN_AWVALID = 0; + assign M_INSN_AWVALID = 0; + assign M_INSN_AWID = INSN_ID; + assign M_INSN_AWADDR = 0; + assign M_INSN_AWLEN = 0; + assign M_INSN_AWSIZE = 0; + assign M_INSN_AWBURST = 0; + assign M_INSN_AWLOCK = 0; + assign M_INSN_AWCACHE = 0; + assign M_INSN_AWPROT = 0; + assign M_INSN_AWQOS = 0; + // + assign M_INSN_WVALID = 0; + assign M_INSN_WDATA = 0; + assign M_INSN_WSTRB = 0; + assign M_INSN_WLAST = 0; + // + assign M_INSN_BREADY = 1'b1; + // + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Memory Unit + // {{{ + //////////////////////////////////////////////////////////////////////// + // +`ifndef FORMAL + wire [C_AXI_DATA_WIDTH-1:0] i_bus_data, o_bus_data; + wire [C_AXI_DATA_WIDTH/8-1:0] o_bus_strb; + + generate if (SWAP_WSTRB && C_AXI_DATA_WIDTH > 32 && !OPT_DCACHE) + begin : SWAP_BUS_WORD_ORDER + genvar gk; + + for(gk=0; gk 0) + begin : PIPELINED_MEM + + axipipe #( + // {{{ + .C_AXI_ADDR_WIDTH(ADDRESS_WIDTH), + .C_AXI_DATA_WIDTH(C_AXI_DATA_WIDTH), + .C_AXI_ID_WIDTH(C_AXI_ID_WIDTH), + .AXI_ID(DATA_ID), + .OPT_LOCK(OPT_LOCK), + .OPT_ALIGNMENT_ERR(OPT_ALIGNMENT_ERR), + .SWAP_WSTRB(SWAP_WSTRB), + .OPT_LOWPOWER(OPT_LOWPOWER) + // .OPT_SIGN_EXTEND(OPT_SIGN_EXTEND) + // }}} + ) domem( + // {{{ + .S_AXI_ACLK(S_AXI_ACLK), .S_AXI_ARESETN(S_AXI_ARESETN), + .i_cpu_reset(cmd_reset), + // CPU interface + // {{{ + .i_stb(mem_ce), + .i_lock(bus_lock), + .i_op(mem_op), + .i_addr(mem_cpu_addr[AW+1:0]), + .i_restart_pc(mem_lock_pc), + .i_data(mem_wdata), + .i_oreg(mem_reg), + .o_busy(mem_busy), + .o_pipe_stalled(mem_pipe_stalled), + .o_rdbusy(mem_rdbusy), + .o_valid(mem_valid), + .o_err(mem_bus_err), + .o_wreg(mem_wreg), + .o_result(mem_result), + // }}} + // AXI + // Write interface + // {{{ + .M_AXI_AWVALID(M_DATA_AWVALID), + .M_AXI_AWREADY(M_DATA_AWREADY), + .M_AXI_AWID( M_DATA_AWID), + .M_AXI_AWADDR( M_DATA_AWADDR), + .M_AXI_AWLEN( M_DATA_AWLEN), + .M_AXI_AWSIZE( M_DATA_AWSIZE), + .M_AXI_AWBURST(M_DATA_AWBURST), + .M_AXI_AWLOCK( M_DATA_AWLOCK), + .M_AXI_AWCACHE(M_DATA_AWCACHE), + .M_AXI_AWPROT( M_DATA_AWPROT), + .M_AXI_AWQOS( M_DATA_AWQOS), + // + .M_AXI_WVALID(M_DATA_WVALID), + .M_AXI_WREADY(M_DATA_WREADY), + .M_AXI_WDATA(o_bus_data), + .M_AXI_WSTRB(o_bus_strb), + .M_AXI_WLAST(M_DATA_WLAST), + // + .M_AXI_BVALID(M_DATA_BVALID), + .M_AXI_BREADY(M_DATA_BREADY), + .M_AXI_BID( M_DATA_BID), + .M_AXI_BRESP( M_DATA_BRESP), + // }}} + // Read interface + // {{{ + .M_AXI_ARVALID(M_DATA_ARVALID), + .M_AXI_ARREADY(M_DATA_ARREADY), + .M_AXI_ARID( M_DATA_ARID), + .M_AXI_ARADDR( M_DATA_ARADDR), + .M_AXI_ARLEN( M_DATA_ARLEN), + .M_AXI_ARSIZE( M_DATA_ARSIZE), + .M_AXI_ARBURST(M_DATA_ARBURST), + .M_AXI_ARLOCK( M_DATA_ARLOCK), + .M_AXI_ARCACHE(M_DATA_ARCACHE), + .M_AXI_ARPROT( M_DATA_ARPROT), + .M_AXI_ARQOS( M_DATA_ARQOS), + // + .M_AXI_RVALID(M_DATA_RVALID), + .M_AXI_RREADY(M_DATA_RREADY), + .M_AXI_RID( M_DATA_RID), + .M_AXI_RDATA( i_bus_data), + .M_AXI_RLAST( M_DATA_RLAST), + .M_AXI_RRESP( M_DATA_RRESP) + // }}} + // }}} + ); + + // Make Verilator happy + // {{{ + // Verilator lint_off UNUSED + wire unused_pipe; + assign unused_pipe = &{ 1'b0, clear_dcache }; + // Verilator lint_on UNUSED + // }}} + end else begin : BARE_MEM + + axiops #( + // {{{ + .C_AXI_ADDR_WIDTH(ADDRESS_WIDTH), + .C_AXI_DATA_WIDTH(C_AXI_DATA_WIDTH), + .C_AXI_ID_WIDTH(C_AXI_ID_WIDTH), + .AXI_ID(DATA_ID), + .SWAP_ENDIANNESS(SWAP_ENDIANNESS), + .SWAP_WSTRB(SWAP_WSTRB), + // .OPT_SIGN_EXTEND(OPT_SIGN_EXTEND), + .OPT_LOCK(OPT_LOCK), + .OPT_ALIGNMENT_ERR(OPT_ALIGNMENT_ERR), + .OPT_LOWPOWER(OPT_LOWPOWER) + // }}} + ) domem( + // {{{ + .S_AXI_ACLK(S_AXI_ACLK), .S_AXI_ARESETN(S_AXI_ARESETN), + .i_cpu_reset(cmd_reset), + // CPU interface + // {{{ + .i_stb(mem_ce), + .i_lock(bus_lock), + .i_op(mem_op), + .i_addr(mem_cpu_addr[ADDRESS_WIDTH-1:0]), + .i_restart_pc(mem_lock_pc), + .i_data(mem_wdata), + .i_oreg(mem_reg), + .o_busy(mem_busy), + .o_rdbusy(mem_rdbusy), + .o_valid(mem_valid), + .o_err(mem_bus_err), + .o_wreg(mem_wreg), + .o_result(mem_result), + // }}} + // AXI4 (full) + // Write interface + // {{{ + .M_AXI_AWVALID(M_DATA_AWVALID), + .M_AXI_AWREADY(M_DATA_AWREADY), + .M_AXI_AWID( M_DATA_AWID), + .M_AXI_AWADDR( M_DATA_AWADDR), + .M_AXI_AWLEN( M_DATA_AWLEN), + .M_AXI_AWSIZE( M_DATA_AWSIZE), + .M_AXI_AWBURST(M_DATA_AWBURST), + .M_AXI_AWLOCK( M_DATA_AWLOCK), + .M_AXI_AWCACHE(M_DATA_AWCACHE), + .M_AXI_AWPROT( M_DATA_AWPROT), + .M_AXI_AWQOS( M_DATA_AWQOS), + // + .M_AXI_WVALID(M_DATA_WVALID), + .M_AXI_WREADY(M_DATA_WREADY), + .M_AXI_WDATA(o_bus_data), + .M_AXI_WSTRB(o_bus_strb), + .M_AXI_WLAST(M_DATA_WLAST), + // + .M_AXI_BVALID(M_DATA_BVALID), + .M_AXI_BREADY(M_DATA_BREADY), + .M_AXI_BID( M_DATA_BID), + .M_AXI_BRESP( M_DATA_BRESP), + // }}} + // Read interface + // {{{ + .M_AXI_ARVALID(M_DATA_ARVALID), + .M_AXI_ARREADY(M_DATA_ARREADY), + .M_AXI_ARID( M_DATA_ARID), + .M_AXI_ARADDR( M_DATA_ARADDR), + .M_AXI_ARLEN( M_DATA_ARLEN), + .M_AXI_ARSIZE( M_DATA_ARSIZE), + .M_AXI_ARBURST(M_DATA_ARBURST), + .M_AXI_ARLOCK( M_DATA_ARLOCK), + .M_AXI_ARCACHE(M_DATA_ARCACHE), + .M_AXI_ARPROT( M_DATA_ARPROT), + .M_AXI_ARQOS( M_DATA_ARQOS), + // + .M_AXI_RVALID(M_DATA_RVALID), + .M_AXI_RREADY(M_DATA_RREADY), + .M_AXI_RID( M_DATA_RID), + .M_AXI_RDATA( i_bus_data), + .M_AXI_RLAST( M_DATA_RLAST), + .M_AXI_RRESP( M_DATA_RRESP) + // }}} + // }}} + ); + + assign mem_pipe_stalled = mem_busy; + + // Make Verilator happy + // {{{ + // Verilator lint_off UNUSED + wire unused_bare; + assign unused_bare = &{ 1'b0, clear_dcache }; + // Verilator lint_on UNUSED + // }}} + end endgenerate +`endif + // }}} + //////////////////////////////////////////////////////////////////////// + // + // (Optional) Clock gate + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + generate if (OPT_CLKGATE) + begin : GATE_CPU_CLOCK + + reg gatep; + reg gaten /* verilator clock_enable */; + + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + gatep <= 1'b1; + else + gatep <= cpu_clken; + + always @(negedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + gaten <= 1'b1; + else + gaten <= gatep; + + assign cpu_clock = S_AXI_ACLK && gaten; + assign clk_gate = gatep; + + end else begin : NO_CLOCK_GATE + + assign cpu_clock = S_AXI_ACLK; + assign clk_gate = 1'b1; + + end endgenerate + // }}} + + // Make Verilator happy + // {{{ + // Verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, cpu_clken, cpu_dbg_cc[2], + S_DBG_AWADDR[DBGLSB-1:0], + S_DBG_ARADDR[DBGLSB-1:0], + S_DBG_ARPROT, S_DBG_AWPROT, + M_INSN_AWREADY, M_INSN_WREADY, + M_INSN_BVALID, M_INSN_BID, M_INSN_BRESP + }; + generate if (32 > ADDRESS_WIDTH) + begin + wire unused_addr; + assign unused_addr = &{ 1'b0, mem_cpu_addr[31:ADDRESS_WIDTH] }; + end endgenerate + // Verilator lint_on UNUSED + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL + wire [F_LGDEPTH-1:0] faxil_rd_outstanding, + faxil_wr_outstanding, faxil_awr_outstanding; + + //////////////////////////////////////////////////////////////////////// + // + // AXI-lite debug interface + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + + faxil_slave #( + // {{{ + .C_AXI_DATA_WIDTH(C_DBG_DATA_WIDTH), + .C_AXI_ADDR_WIDTH(C_DBG_ADDR_WIDTH), + .F_LGDEPTH(F_LGDEPTH), + .F_AXI_MAXWAIT(0), + .F_AXI_MAXDELAY(0) + // }}} + ) faxil ( + // {{{ + .i_clk(S_AXI_ACLK), .i_axi_reset_n(S_AXI_ARESETN), + // AXI-lite debug writes + // {{{ + .i_axi_awvalid(S_DBG_AWVALID), + .i_axi_awready(S_DBG_AWREADY), + .i_axi_awaddr(S_DBG_AWADDR), + .i_axi_awprot(S_DBG_AWPROT), + // + .i_axi_wvalid(S_DBG_WVALID), + .i_axi_wready(S_DBG_WREADY), + .i_axi_wdata(S_DBG_WDATA), + .i_axi_wstrb(S_DBG_WSTRB), + // + .i_axi_bvalid(S_DBG_BVALID), + .i_axi_bready(S_DBG_BREADY), + .i_axi_bresp(S_DBG_BRESP), + // }}} + // AXI-lite debug reads + // {{{ + .i_axi_arvalid(S_DBG_ARVALID), + .i_axi_arready(S_DBG_ARREADY), + .i_axi_araddr(S_DBG_ARADDR), + .i_axi_arprot(S_DBG_ARPROT), + // + .i_axi_rvalid(S_DBG_RVALID), + .i_axi_rready(S_DBG_RREADY), + .i_axi_rdata(S_DBG_RDATA), + .i_axi_rresp(S_DBG_RRESP), + // }}} + // Induction + // {{{ + .f_axi_rd_outstanding(faxil_rd_outstanding), + .f_axi_wr_outstanding(faxil_wr_outstanding), + .f_axi_awr_outstanding(faxil_awr_outstanding) + // }}} + // }}} + ); + + always @(*) + if (S_AXI_ARESETN) + begin + assert(faxil_rd_outstanding == (S_DBG_ARREADY ? 0:1) + +(dbg_read_valid ? 1:0) + (S_DBG_RVALID ? 1:0)); + + assert(!dbg_read_valid || !S_DBG_RVALID); + + assert(faxil_wr_outstanding == (S_DBG_WREADY ? 0:1) + +(S_DBG_BVALID ? 1:0)); + + assert(faxil_awr_outstanding == (S_DBG_AWREADY ? 0:1) + +(S_DBG_BVALID ? 1:0)); + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // CPU's debug interface + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // Captured above + + // }}} +`endif +// }}} +endmodule diff --git a/rtl/cpu/zipaxil.v b/rtl/cpu/zipaxil.v new file mode 100644 index 0000000..d41becc --- /dev/null +++ b/rtl/cpu/zipaxil.v @@ -0,0 +1,1167 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: zipaxil.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: A potential top level module holding the core of the Zip CPU +// together--this one with AXI-lite instruction, data, and debug +// interfaces. In general, the Zip CPU is designed to be as simple as +// possible. (actual implementation aside ...) The instruction set is +// about as RISC as you can get, with only 26 instruction types currently +// supported. (There are still 8-instruction Op-Codes reserved for +// floating point, and 5 which can be used for transactions not requiring +// registers.) Please see the accompanying spec.pdf file for a description +// of these instructions. +// +// This version is bus width agnostic for both instruction and data buses, +// although the debug bus must still be 32-bits. Instruction and data +// buses must be at least 32-bits wide. +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module zipaxil #( + // {{{ + parameter C_DBG_ADDR_WIDTH = 8, + parameter ADDRESS_WIDTH = 32, + parameter C_AXI_DATA_WIDTH = 32, + parameter OPT_LGICACHE = 0, + parameter OPT_LGDCACHE = 0, + parameter [0:0] OPT_PIPELINED = 1'b1, + parameter [ADDRESS_WIDTH-1:0] RESET_ADDRESS={(ADDRESS_WIDTH){1'b0}}, + parameter [0:0] START_HALTED = 1'b0, + parameter [0:0] SWAP_WSTRB = 1'b1, + parameter OPT_MPY = 3, + parameter [0:0] OPT_DIV = 1'b1, + parameter [0:0] OPT_SHIFTS = 1'b1, + parameter [0:0] OPT_LOCK = 1'b1, + parameter [0:0] OPT_FPU = 0, + parameter [0:0] OPT_EARLY_BRANCHING = 1, + parameter [0:0] OPT_CIS = 1'b1, + parameter [0:0] OPT_LOWPOWER = 1'b0, + parameter [0:0] OPT_DISTRIBUTED_REGS = 1'b1, + parameter [0:0] OPT_DBGPORT = START_HALTED, + parameter [0:0] OPT_TRACE_PORT = 1'b0, + parameter [0:0] OPT_PROFILER = 1'b0, + parameter [0:0] OPT_USERMODE = 1'b1, + parameter RESET_DURATION = 10, + // localparam [0:0] WITH_LOCAL_BUS = 1'b0, +`ifdef VERILATOR + parameter [0:0] OPT_SIM = 1'b1, + parameter [0:0] OPT_CLKGATE = OPT_LOWPOWER +`else + parameter [0:0] OPT_SIM = 1'b0, + parameter [0:0] OPT_CLKGATE = 1'b0 +`endif +`ifdef FORMAL + , parameter F_LGDEPTH=8 +`endif + // }}} + ) ( + // {{{ + input wire S_AXI_ACLK, S_AXI_ARESETN, + i_interrupt, i_cpu_reset, + // Debug interface + // {{{ + // Debug interface -- inputs +// input wire i_halt, i_clear_cache, +// input wire [4:0] i_dbg_wreg, +// input wire i_dbg_we, +// input wire [31:0] i_dbg_data, +// input wire [4:0] i_dbg_rreg, + // Debug interface -- outputs +// output wire cpu_dbg_stall, +// output reg [31:0] o_dbg_reg, +// output reg [2:0] o_dbg_cc, +// output wire o_break, + input wire S_DBG_AWVALID, + output wire S_DBG_AWREADY, + input wire [C_DBG_ADDR_WIDTH-1:0] S_DBG_AWADDR, + input wire [2:0] S_DBG_AWPROT, + // + input wire S_DBG_WVALID, + output wire S_DBG_WREADY, + input wire [31:0] S_DBG_WDATA, + input wire [3:0] S_DBG_WSTRB, + // + output reg S_DBG_BVALID, + input wire S_DBG_BREADY, + output wire [1:0] S_DBG_BRESP, + // + input wire S_DBG_ARVALID, + output wire S_DBG_ARREADY, + input wire [7:0] S_DBG_ARADDR, + input wire [2:0] S_DBG_ARPROT, + // + output reg S_DBG_RVALID, + input wire S_DBG_RREADY, + output reg [31:0] S_DBG_RDATA, + output wire [1:0] S_DBG_RRESP, + // + // }}} + // Instruction bus (master) + // {{{ + output wire M_INSN_AWVALID, + input wire M_INSN_AWREADY, + output wire [ADDRESS_WIDTH-1:0] M_INSN_AWADDR, + output wire [2:0] M_INSN_AWPROT, + // + output wire M_INSN_WVALID, + input wire M_INSN_WREADY, + output wire [C_AXI_DATA_WIDTH-1:0] M_INSN_WDATA, + output wire [C_AXI_DATA_WIDTH/8-1:0] M_INSN_WSTRB, + // + input wire M_INSN_BVALID, + output wire M_INSN_BREADY, + input wire [1:0] M_INSN_BRESP, + // + output wire M_INSN_ARVALID, + input wire M_INSN_ARREADY, + output wire [ADDRESS_WIDTH-1:0] M_INSN_ARADDR, + output wire [2:0] M_INSN_ARPROT, + // + input wire M_INSN_RVALID, + output wire M_INSN_RREADY, + input wire [C_AXI_DATA_WIDTH-1:0] M_INSN_RDATA, + input wire [1:0] M_INSN_RRESP, + // }}} + // Data bus (master) + // {{{ + output wire M_DATA_AWVALID, + input wire M_DATA_AWREADY, + output wire [ADDRESS_WIDTH-1:0] M_DATA_AWADDR, + output wire [2:0] M_DATA_AWPROT, + // + output wire M_DATA_WVALID, + input wire M_DATA_WREADY, + output wire [C_AXI_DATA_WIDTH-1:0] M_DATA_WDATA, + output wire [C_AXI_DATA_WIDTH/8-1:0] M_DATA_WSTRB, + // + input wire M_DATA_BVALID, + output wire M_DATA_BREADY, + input wire [1:0] M_DATA_BRESP, + // + output wire M_DATA_ARVALID, + input wire M_DATA_ARREADY, + output wire [ADDRESS_WIDTH-1:0] M_DATA_ARADDR, + output wire [2:0] M_DATA_ARPROT, + // + input wire M_DATA_RVALID, + output wire M_DATA_RREADY, + input wire [C_AXI_DATA_WIDTH-1:0] M_DATA_RDATA, + input wire [1:0] M_DATA_RRESP, + // }}} + // Accounting outputs ... to help us count stalls and usage + output wire o_cmd_reset, + output wire o_halted, + output wire o_gie, + output wire o_op_stall, + output wire o_pf_stall, + output wire o_i_count, + // + output wire [31:0] o_cpu_debug, + // + output wire o_prof_stb, + output wire [ADDRESS_WIDTH-1:0] o_prof_addr, + output wire [31:0] o_prof_ticks + // }}} + ); + + // Declarations + // {{{ + localparam [0:0] DBG_ADDR_CTRL = 1'b0, + DBG_ADDR_CPU = 1'b1; + + localparam C_DBG_DATA_WIDTH = 32; + localparam DBGLSB = $clog2(C_DBG_DATA_WIDTH/8); + localparam AW=ADDRESS_WIDTH-2; + localparam [0:0] OPT_PIPELINED_BUS_ACCESS = (OPT_PIPELINED)&&(OPT_LGDCACHE > 1); + localparam [0:0] OPT_MEMPIPE = OPT_PIPELINED_BUS_ACCESS; + localparam [0:0] OPT_DCACHE = (OPT_LGDCACHE > 4); + + localparam FETCH_LIMIT = (OPT_LGICACHE < 4) ? (1 << OPT_LGICACHE) : 16; + + // Debug bit allocations + // {{{ + // DBGCTRL + // 5 DBG Catch -- Catch exceptions/fautls w/ debugger + // 4 Clear cache + // 3 RESET_FLAG + // 2 STEP (W=1 steps, and returns to halted) + // 1 HALT(ED) + // 0 HALT + // DBGDATA + // read/writes internal registers + // + localparam HALT_BIT = 0, + STEP_BIT = 2, + RESET_BIT = 3, + CLEAR_CACHE_BIT = 4, + CATCH_BIT = 5; + // }}} + localparam [0:0] OPT_ALIGNMENT_ERR = 1'b0; + localparam [0:0] SWAP_ENDIANNESS = 1'b0; + + // AXI-lite signal handling + // {{{ + wire awskd_valid, wskd_valid, arskd_valid; + wire dbg_write_ready, dbg_read_ready; + wire [C_DBG_ADDR_WIDTH-DBGLSB-1:0] awskd_addr, arskd_addr; + wire [31:0] wskd_data; + wire [3:0] wskd_strb; + reg dbg_write_valid, dbg_read_valid; + wire w_dbg_write_valid; + reg [4:0] dbg_write_reg; + wire [4:0] dbg_read_reg; + reg [31:0] dbg_write_data; + wire [31:0] dbg_read_data; + wire cpu_dbg_stall, cpu_break, dbg_write_stall; + wire [2:0] cpu_dbg_cc; + // }}} + + wire reset_hold, halt_on_fault, dbg_catch; + wire cpu_clken, cpu_clock, clk_gate; + wire reset_request, release_request, halt_request, step_request, + clear_cache_request; + + + // CPU control registers + // {{{ + reg cmd_halt, cmd_reset, cmd_step, cmd_clear_cache; + wire [31:0] cpu_status; + wire dbg_cmd_write, dbg_cpu_write; + wire [31:0] dbg_cmd_data; + wire [3:0] dbg_cmd_strb; + // }}} + + // Fetch + // {{{ + wire pf_new_pc, clear_icache, pf_ready; + wire [AW+1:0] pf_request_address; + wire [31:0] pf_instruction; + wire [AW+1:0] pf_instruction_pc; + wire pf_valid, pf_illegal; + // }}} + // Memory + // {{{ + wire clear_dcache, mem_ce, bus_lock; + wire [2:0] mem_op; + wire [31:0] mem_cpu_addr; + wire [AW+1:0] mem_lock_pc; + wire [31:0] mem_wdata; + wire [4:0] mem_reg; + wire mem_busy, mem_rdbusy, mem_pipe_stalled, mem_valid, + mem_bus_err; + wire [4:0] mem_wreg; + wire [31:0] mem_result; + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Debug signal handling + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // + // Write signaling + // {{{ + skidbuffer #( + // {{{ + .OPT_OUTREG(0), + .OPT_LOWPOWER(OPT_LOWPOWER), + .DW(C_DBG_ADDR_WIDTH-DBGLSB) + // }}} + ) dbgawskd( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(!S_AXI_ARESETN), + .i_valid(S_DBG_AWVALID), + .o_ready(S_DBG_AWREADY), + .i_data(S_DBG_AWADDR[C_DBG_ADDR_WIDTH-1:DBGLSB]), + .o_valid(awskd_valid), .i_ready(dbg_write_ready), + .o_data(awskd_addr) + // }}} + ); + + skidbuffer #( + // {{{ + .OPT_OUTREG(0), + .OPT_LOWPOWER(OPT_LOWPOWER), + .DW(C_DBG_DATA_WIDTH+C_DBG_DATA_WIDTH/8) + // }}} + ) dbgwskd( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(!S_AXI_ARESETN), + .i_valid(S_DBG_WVALID), + .o_ready(S_DBG_WREADY), + .i_data({ S_DBG_WSTRB, S_DBG_WDATA }), + .o_valid(wskd_valid), .i_ready(dbg_write_ready), + .o_data({ wskd_strb, wskd_data }) + // }}} + ); + + assign dbg_write_ready = awskd_valid && wskd_valid + && ((wskd_strb==0) || awskd_addr[5] + || !dbg_write_stall) + && (!S_DBG_BVALID || S_DBG_BREADY); + + // dbg_write_valid + // {{{ + assign w_dbg_write_valid = dbg_write_ready && (|wskd_strb) + && awskd_addr[5] == DBG_ADDR_CPU; + initial dbg_write_valid = 0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + dbg_write_valid <= 1'b0; + else if (!dbg_write_stall) + dbg_write_valid <= w_dbg_write_valid; + // }}} + + // dbg_write_reg + // {{{ + always @(posedge S_AXI_ACLK) + if (!dbg_write_stall) + begin + dbg_write_reg <= awskd_addr[4:0]; + if (OPT_LOWPOWER && !w_dbg_write_valid) + dbg_write_reg <= 0; + end + // }}} + + // dbg_write_data + // {{{ + always @(posedge S_AXI_ACLK) + if (!dbg_write_stall) + begin + dbg_write_data <= wskd_data; + if (OPT_LOWPOWER && !w_dbg_write_valid) + dbg_write_data <= 0; + end + // }}} + + // S_DBG_BVALID + // {{{ + initial S_DBG_BVALID = 1'b0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + S_DBG_BVALID <= 1'b0; + else if (dbg_write_ready) + S_DBG_BVALID <= 1'b1; + else if (S_DBG_BREADY) + S_DBG_BVALID <= 1'b0; + // }}} + + // S_DBG_BRESP + // {{{ + assign S_DBG_BRESP = 2'b00; + // }}} + + // }}} + + // + // Read signaling + // {{{ + + skidbuffer #( + // {{{ + .OPT_OUTREG(0), + .OPT_LOWPOWER(OPT_LOWPOWER), + .DW(C_DBG_ADDR_WIDTH-DBGLSB) + // }}} + ) dbgarskd( + // {{{ + .i_clk(S_AXI_ACLK), .i_reset(!S_AXI_ARESETN), + .i_valid(S_DBG_ARVALID), + .o_ready(S_DBG_ARREADY), + .i_data(S_DBG_ARADDR[C_DBG_ADDR_WIDTH-1:DBGLSB]), + .o_valid(arskd_valid), .i_ready(dbg_read_ready), + .o_data(arskd_addr) + // }}} + ); + + assign dbg_read_ready = arskd_valid && !dbg_read_valid + && (!S_DBG_RVALID || S_DBG_RREADY); + + assign dbg_read_reg = (OPT_LOWPOWER && !dbg_read_ready) + ? 5'h0 : arskd_addr[4:0]; + + // dbg_read_valid + // {{{ + initial dbg_read_valid = 0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + dbg_read_valid <= 0; + else + dbg_read_valid <= dbg_read_ready + && arskd_addr[5] == DBG_ADDR_CPU; + // }}} + + // S_DBG_RVALID + // {{{ + initial S_DBG_RVALID = 0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + S_DBG_RVALID <= 0; + else if (!S_DBG_RVALID || S_DBG_RREADY) + S_DBG_RVALID <= (dbg_read_ready + && arskd_addr[5] == DBG_ADDR_CTRL) + || dbg_read_valid; + // }}} + + // S_DBG_RDATA + // {{{ + initial S_DBG_RDATA = 0; + always @(posedge S_AXI_ACLK) + if (OPT_LOWPOWER && !S_AXI_ARESETN) + S_DBG_RDATA <= 0; + else if (!S_DBG_RVALID || S_DBG_RREADY) + begin + // {{{ + if (dbg_read_valid) + S_DBG_RDATA <= dbg_read_data; + else + S_DBG_RDATA <= cpu_status; + + if (OPT_LOWPOWER && !dbg_read_valid && !dbg_read_ready) + S_DBG_RDATA <= 0; + // }}} + end + // }}} + + assign S_DBG_RRESP = 2'b00; + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Reset, halt, clear-cache, and step controls + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + assign dbg_cpu_write = dbg_write_ready && awskd_addr[5] == DBG_ADDR_CPU; + assign dbg_cmd_write = dbg_write_ready && awskd_addr[5] == DBG_ADDR_CTRL; + assign dbg_cmd_data = wskd_data; + assign dbg_cmd_strb = wskd_strb; + + assign reset_request = dbg_cmd_write && dbg_cmd_strb[RESET_BIT/8] + && dbg_cmd_data[RESET_BIT]; + assign release_request = dbg_cmd_write && dbg_cmd_strb[HALT_BIT/8] + && !dbg_cmd_data[HALT_BIT]; + assign halt_request = dbg_cmd_write && dbg_cmd_strb[HALT_BIT/8] + && dbg_cmd_data[HALT_BIT]; + assign step_request = dbg_cmd_write && dbg_cmd_strb[STEP_BIT/8] + && dbg_cmd_data[STEP_BIT]; + assign clear_cache_request = dbg_cmd_write + && dbg_cmd_strb[CLEAR_CACHE_BIT/8] + && dbg_cmd_data[CLEAR_CACHE_BIT]; + + // + // reset_hold: Always start us off with an initial reset + // {{{ + generate if (RESET_DURATION > 0) + begin : INITIAL_RESET_HOLD + // {{{ + reg [$clog2(RESET_DURATION)-1:0] reset_counter; + reg r_reset_hold; + + initial reset_counter = RESET_DURATION; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN || i_cpu_reset) + reset_counter <= RESET_DURATION; + else if (reset_counter > 0) + reset_counter <= reset_counter - 1; + + initial r_reset_hold = 1; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN || i_cpu_reset) + r_reset_hold <= 1; + else + r_reset_hold <= (reset_counter > 1); + + assign reset_hold = r_reset_hold; +`ifdef FORMAL + always @(*) + assert(reset_hold == (reset_counter != 0)); +`endif + // }}} + end else begin + + assign reset_hold = 0; + + end endgenerate + // }}} + + assign halt_on_fault = dbg_catch; + + // cmd_reset + // {{{ + // Always start us off with an initial reset + initial cmd_reset = 1'b1; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN || i_cpu_reset) + cmd_reset <= 1'b1; + else if (reset_hold) + cmd_reset <= 1'b1; + else if (cpu_break && !halt_on_fault) + cmd_reset <= 1'b1; + else + cmd_reset <= reset_request; + // }}} + + // cmd_halt + // {{{ + initial cmd_halt = START_HALTED; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + cmd_halt <= START_HALTED; + else if (i_cpu_reset && !dbg_write_ready && !dbg_write_stall) + cmd_halt <= START_HALTED; + else if (cmd_reset && START_HALTED) + cmd_halt <= START_HALTED; + else begin + // {{{ + // When shall we release from a halt? Only if we have come to + // a full and complete stop. Even then, we only release if we + // aren't being given a command to step the CPU. + // + if (!dbg_write_valid && !cpu_dbg_stall && dbg_cmd_write + && (release_request || step_request)) + cmd_halt <= 1'b0; + + // Reasons to halt + + // 1. Halt on any unhandled CPU exception. The cause of the + // exception must be cured before we can (re)start. + // If the CPU is configured to start immediately on power + // up, we leave it to reset on any exception instead. + if (cpu_break && halt_on_fault) + cmd_halt <= 1'b1; + + // 2. Halt on any user request to halt. (Only valid if the + // STEP bit isn't also set) + if (dbg_cmd_write && halt_request && !step_request) + cmd_halt <= 1'b1; + + // 3. Halt on any user request to write to a CPU register + if (dbg_cpu_write && dbg_cmd_strb != 0) + cmd_halt <= 1'b1; + + // 4. Halt following any step command + if (cmd_step) + cmd_halt <= 1'b1; + + // 4. Halt following any clear cache + if (cmd_clear_cache) + cmd_halt <= 1'b1; + + // 5. Halt on any clear cache bit--independent of any step bit + if (clear_cache_request) + cmd_halt <= 1'b1; + // }}} + end + // }}} + + // cmd_clear_cache + // {{{ + initial cmd_clear_cache = 1'b0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN || cmd_reset) + cmd_clear_cache <= 1'b0; + else if (dbg_cmd_write && clear_cache_request && halt_request) + cmd_clear_cache <= 1'b1; + else if (cmd_halt && !cpu_dbg_stall) + cmd_clear_cache <= 1'b0; + // }}} + + // cmd_step + // {{{ + initial cmd_step = 1'b0; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN || i_cpu_reset) + cmd_step <= 1'b0; + else if (step_request) + cmd_step <= 1'b1; + else if (!cpu_dbg_stall) + cmd_step <= 1'b0; + // }}} + + // dbg_catch + // {{{ + generate if (!OPT_DBGPORT) + begin : NO_DBG_CATCH + assign dbg_catch = START_HALTED; + end else begin : GEN_DBG_CATCH + reg r_dbg_catch; + + initial r_dbg_catch = START_HALTED; + always @(posedge S_AXI_ACLK) + if (!S_AXI_ARESETN) + r_dbg_catch <= START_HALTED; + else if (dbg_cmd_write && dbg_cmd_strb[CATCH_BIT/8]) + r_dbg_catch <= dbg_cmd_data[CATCH_BIT]; + + assign dbg_catch = r_dbg_catch; + end endgenerate + // }}} + + // cpu_status + // {{{ + // 0xffff_f000 -> (Unused / reserved) + // + // 0x0000_0800 -> cpu_break + // 0x0000_0400 -> Interrupt pending + // 0x0000_0200 -> User mode + // 0x0000_0100 -> Sleep (CPU is sleeping) + // + // 0x0000_00c0 -> (Unused/reserved) + // 0x0000_0020 -> dbg_catch + // 0x0000_0010 -> cmd_clear_cache + // + // 0x0000_0008 -> Reset + // 0x0000_0004 -> Step (auto clearing, write only) + // 0x0000_0002 -> Halt (status) + // 0x0000_0001 -> Halt (request) + assign cpu_status = { 16'h0, 4'h0, + cpu_break, i_interrupt, cpu_dbg_cc[1:0], + 2'h0, dbg_catch, 1'b0, + cmd_reset, 1'b0, !cpu_dbg_stall, cmd_halt + }; + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // The ZipCPU Core + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // +`ifdef FORMAL + // {{{ + (* anyseq *) reg f_cpu_halted, f_cpu_data, f_cpu_stall, + f_cpu_break; + (* anyseq *) reg [2:0] f_cpu_dbg_cc; + (* anyseq *) reg [2:0] f_cpu_dbg_read_data; + + assign cpu_dbg_stall = f_cpu_stall; + assign cpu_break = f_cpu_break; + assign cpu_dbg_cc = f_cpu_dbg_cc; + assign dbg_read_data = f_cpu_dbg_read_data; + + fdebug #( + // {{{ + .OPT_START_HALTED(START_HALTED), + .OPT_DISTRIBUTED_RAM(OPT_DISTRIBUTED_REGS) + // }}} + ) fdbg ( + // {{{ + .i_clk(S_AXI_ACLK), + .i_reset(!S_AXI_ARESETN), + .i_cpu_reset(cmd_reset || i_cpu_reset), + .i_halt(cmd_halt), + .i_halted(f_cpu_halted), + .i_clear_cache(cmd_clear_cache), + .i_dbg_we(dbg_write_valid), + .i_dbg_reg(dbg_write_reg), + .i_dbg_data(dbg_write_data), + .i_dbg_stall(cpu_dbg_stall), + .i_dbg_break(cpu_break), + .i_dbg_cc(cpu_dbg_cc) + // }}} + ); + // }}} +`else + zipcore #( + // {{{ + .RESET_ADDRESS({ {(32-ADDRESS_WIDTH){1'b0}}, RESET_ADDRESS }), + .ADDRESS_WIDTH(ADDRESS_WIDTH-2), + .OPT_PIPELINED(OPT_PIPELINED), + .OPT_EARLY_BRANCHING(OPT_EARLY_BRANCHING), + .OPT_DCACHE(OPT_DCACHE), + .OPT_MPY(OPT_MPY), + .OPT_DIV(OPT_DIV), + .OPT_SHIFTS(OPT_SHIFTS), + .IMPLEMENT_FPU(OPT_FPU), + .OPT_CIS(OPT_CIS), + .OPT_LOCK(OPT_LOCK), + .OPT_LOWPOWER(OPT_LOWPOWER), + .OPT_START_HALTED(START_HALTED), + .OPT_SIM(OPT_SIM), + .OPT_PIPELINED_BUS_ACCESS(OPT_MEMPIPE), + // localparam [0:0] OPT_MEMPIPE = OPT_PIPELINED_BUS_ACCESS; + .OPT_DBGPORT(OPT_DBGPORT), + .OPT_TRACE_PORT(OPT_TRACE_PORT), + .OPT_PROFILER(OPT_PROFILER), + .OPT_CLKGATE(OPT_CLKGATE), + .OPT_DISTRIBUTED_REGS(OPT_DISTRIBUTED_REGS), + .OPT_USERMODE(OPT_USERMODE) +`ifdef FORMAL + , .F_LGDEPTH(F_LGDEPTH) +`endif + // }}} + ) core ( + // {{{ + .i_clk(cpu_clock), .i_reset(cmd_reset), + .i_interrupt(i_interrupt), + .o_clken(cpu_clken), + // Debug interface + // {{{ + .i_halt(cmd_halt), .i_clear_cache(cmd_clear_cache), + .i_dbg_wreg(dbg_write_reg), .i_dbg_we(dbg_write_valid), + .i_dbg_data(dbg_write_data),.i_dbg_rreg(dbg_read_reg), + .o_dbg_stall(cpu_dbg_stall),.o_dbg_reg(dbg_read_data), + .o_dbg_cc(cpu_dbg_cc), .o_break(cpu_break), + // }}} + // Instruction fetch interface + // {{{ + .o_pf_new_pc(pf_new_pc), .o_clear_icache(clear_icache), + .o_pf_ready(pf_ready), + .o_pf_request_address(pf_request_address), + .i_pf_valid(pf_valid), + .i_pf_illegal(pf_illegal), + .i_pf_instruction(pf_instruction), + .i_pf_instruction_pc(pf_instruction_pc), + // }}} + // Memory unit interface + // {{{ + .o_clear_dcache(clear_dcache), .o_mem_ce(mem_ce), + .o_bus_lock(bus_lock), + .o_mem_op(mem_op), .o_mem_addr(mem_cpu_addr), + .o_mem_data(mem_wdata), + .o_mem_lock_pc(mem_lock_pc), + .o_mem_reg(mem_reg), + .i_mem_busy(mem_busy), + .i_mem_rdbusy(mem_rdbusy), + .i_mem_pipe_stalled(mem_pipe_stalled), + .i_mem_valid(mem_valid), + .i_bus_err(mem_bus_err), + .i_mem_wreg(mem_wreg), + .i_mem_result(mem_result), + // }}} + // Accounting/CPU usage interface + .o_op_stall(o_op_stall), .o_pf_stall(o_pf_stall), + .o_i_count(o_i_count), + // + .o_debug(o_cpu_debug), + // + .o_prof_stb( o_prof_stb), + .o_prof_addr( o_prof_addr), + .o_prof_ticks(o_prof_ticks) + // }}} + ); +`endif + assign o_cmd_reset = cmd_reset; + assign o_gie = cpu_dbg_cc[1]; + assign o_halted = !cpu_dbg_stall; + assign dbg_write_stall = dbg_write_valid&&(cpu_dbg_stall || !clk_gate); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Instruction Fetch + // {{{ + //////////////////////////////////////////////////////////////////////// + // + +`ifndef FORMAL + + axilfetch #( + // {{{ + .C_AXI_ADDR_WIDTH(ADDRESS_WIDTH), + .C_AXI_DATA_WIDTH(C_AXI_DATA_WIDTH), + .FETCH_LIMIT(FETCH_LIMIT), + .SWAP_ENDIANNESS(SWAP_ENDIANNESS) + // }}} + ) pf ( + // {{{ + .S_AXI_ACLK(S_AXI_ACLK), .S_AXI_ARESETN(S_AXI_ARESETN), + // CPU signals + // {{{ + .i_cpu_reset(cmd_reset), + .i_new_pc(pf_new_pc), + .i_clear_cache(clear_icache), + .i_ready(pf_ready && clk_gate), + .i_pc(pf_request_address), + .o_insn(pf_instruction), + .o_pc(pf_instruction_pc), + .o_valid(pf_valid), + .o_illegal(pf_illegal), + // }}} + // AXI-lite bus signals + // {{{ + .M_AXI_ARVALID(M_INSN_ARVALID), + .M_AXI_ARREADY(M_INSN_ARREADY), + .M_AXI_ARADDR(M_INSN_ARADDR), + .M_AXI_ARPROT(M_INSN_ARPROT), + // + .M_AXI_RVALID(M_INSN_RVALID), + .M_AXI_RREADY(M_INSN_RREADY), + .M_AXI_RDATA(M_INSN_RDATA), + .M_AXI_RRESP(M_INSN_RRESP) + // }}} + // }}} + ); + +`endif + + // Assign values to the (unused) M_INSN_* write ports + // {{{ + assign M_INSN_AWVALID = 0; + assign M_INSN_AWVALID = 0; + assign M_INSN_AWADDR = 0; + assign M_INSN_AWPROT = 0; + // + assign M_INSN_WVALID = 0; + assign M_INSN_WDATA = 0; + assign M_INSN_WSTRB = 0; + // + assign M_INSN_BREADY = 1'b1; + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Memory Unit + // {{{ + //////////////////////////////////////////////////////////////////////// + // +`ifndef FORMAL + wire [C_AXI_DATA_WIDTH-1:0] i_bus_data, o_bus_data; + wire [C_AXI_DATA_WIDTH/8-1:0] o_bus_strb; + + generate if (SWAP_WSTRB && C_AXI_DATA_WIDTH > 32 && !OPT_DCACHE) + begin : SWAP_BUS_WORD_ORDER + genvar gk; + + for(gk=0; gk ADDRESS_WIDTH) + begin + wire unused_addr; + assign unused_addr = &{ 1'b0, mem_cpu_addr[31:ADDRESS_WIDTH] }; + end endgenerate + // Verilator lint_on UNUSED + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL + wire [F_LGDEPTH-1:0] faxil_rd_outstanding, + faxil_wr_outstanding, faxil_awr_outstanding; + + //////////////////////////////////////////////////////////////////////// + // + // AXI-lite debug interface + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + + faxil_slave #( + // {{{ + .C_AXI_DATA_WIDTH(C_DBG_DATA_WIDTH), + .C_AXI_ADDR_WIDTH(C_DBG_ADDR_WIDTH), + .F_LGDEPTH(F_LGDEPTH), + .F_AXI_MAXWAIT(0), + .F_AXI_MAXDELAY(0) + // }}} + ) faxil ( + // {{{ + .i_clk(S_AXI_ACLK), .i_axi_reset_n(S_AXI_ARESETN), + // AXI-lite debug writes + // {{{ + .i_axi_awvalid(S_DBG_AWVALID), + .i_axi_awready(S_DBG_AWREADY), + .i_axi_awaddr(S_DBG_AWADDR), + .i_axi_awprot(S_DBG_AWPROT), + // + .i_axi_wvalid(S_DBG_WVALID), + .i_axi_wready(S_DBG_WREADY), + .i_axi_wdata(S_DBG_WDATA), + .i_axi_wstrb(S_DBG_WSTRB), + // + .i_axi_bvalid(S_DBG_BVALID), + .i_axi_bready(S_DBG_BREADY), + .i_axi_bresp(S_DBG_BRESP), + // }}} + // AXI-lite debug reads + // {{{ + .i_axi_arvalid(S_DBG_ARVALID), + .i_axi_arready(S_DBG_ARREADY), + .i_axi_araddr(S_DBG_ARADDR), + .i_axi_arprot(S_DBG_ARPROT), + // + .i_axi_rvalid(S_DBG_RVALID), + .i_axi_rready(S_DBG_RREADY), + .i_axi_rdata(S_DBG_RDATA), + .i_axi_rresp(S_DBG_RRESP), + // }}} + // Induction + // {{{ + .f_axi_rd_outstanding(faxil_rd_outstanding), + .f_axi_wr_outstanding(faxil_wr_outstanding), + .f_axi_awr_outstanding(faxil_awr_outstanding) + // }}} + // }}} + ); + + always @(*) + if (S_AXI_ARESETN) + begin + assert(faxil_rd_outstanding == (S_DBG_ARREADY ? 0:1) + +(dbg_read_valid ? 1:0) + (S_DBG_RVALID ? 1:0)); + + assert(!dbg_read_valid || !S_DBG_RVALID); + + assert(faxil_wr_outstanding == (S_DBG_WREADY ? 0:1) + +(S_DBG_BVALID ? 1:0)); + + assert(faxil_awr_outstanding == (S_DBG_AWREADY ? 0:1) + +(S_DBG_BVALID ? 1:0)); + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // CPU's debug interface + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // Captured above + + // }}} +`endif +// }}} +endmodule diff --git a/rtl/cpu/zipbones.v b/rtl/cpu/zipbones.v index 6e668cc..1061009 100644 --- a/rtl/cpu/zipbones.v +++ b/rtl/cpu/zipbones.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zipbones.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: In the spirit of keeping the Zip CPU small, this implements a @@ -12,11 +12,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -29,230 +29,553 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -`include "cpudefs.v" -// -`define RESET_BIT 6 -`define STEP_BIT 8 -`define HALT_BIT 10 -`define CLEAR_CACHE_BIT 11 -// -`ifdef OPT_TRADITIONAL_PFCACHE -`define LGICACHE_DEFAULT 8 +// }}} +module zipbones #( + // {{{ + parameter RESET_ADDRESS=32'h1000_0000, + ADDRESS_WIDTH=32, + parameter BUS_WIDTH=32, // Bus data width + // CPU options + // {{{ + parameter [0:0] OPT_PIPELINED=1, + parameter [0:0] OPT_EARLY_BRANCHING=OPT_PIPELINED, + // OPT_LGICACHE + // {{{ + parameter OPT_LGICACHE = 2, + // }}} + // OPT_LGDCACHE + // {{{ + // Set to zero for no data cache + parameter OPT_LGDCACHE = 0, + // }}} + parameter [0:0] START_HALTED=1, + parameter [0:0] OPT_DISTRIBUTED_REGS=1, + // OPT_MPY + // {{{ + parameter OPT_MPY = 3, + // }}} + // OPT_DIV + // {{{ + parameter [0:0] OPT_DIV=1, + // }}} + // OPT_SHIFTS + // {{{ + parameter [0:0] OPT_SHIFTS = 1, + // }}} + // OPT_FPU + // {{{ + parameter [0:0] OPT_FPU = 0, + // }}} + parameter [0:0] OPT_CIS=1, + parameter [0:0] OPT_LOCK=1, + parameter [0:0] OPT_USERMODE=1, + parameter [0:0] OPT_DBGPORT=START_HALTED, + parameter [0:0] OPT_TRACE_PORT=1, + parameter [0:0] OPT_PROFILER=0, + parameter [0:0] OPT_LOWPOWER=0, +`ifdef VERILATOR + parameter [0:0] OPT_SIM=1'b1, + parameter [0:0] OPT_CLKGATE = OPT_LOWPOWER, `else -`define LGICACHE_DEFAULT 0 + parameter [0:0] OPT_SIM=1'b0, + parameter [0:0] OPT_CLKGATE = 1'b0, `endif -// -`ifdef OPT_DCACHE -`define LGDCACHE_DEFAULT 10 + // }}} + parameter RESET_DURATION = 10, + // Short-cut names + // {{{ + // localparam AW=ADDRESS_WIDTH, + localparam DBG_WIDTH=32, // Debug bus data width + localparam // Derived parameters + // PHYSICAL_ADDRESS_WIDTH=ADDRESS_WIDTH, + PAW=ADDRESS_WIDTH-$clog2(BUS_WIDTH/8) +`ifdef OPT_MMU + // VIRTUAL_ADDRESS_WIDTH=30, `else -`define LGDCACHE_DEFAULT 0 + // VIRTUAL_ADDRESS_WIDTH=PAW, `endif -module zipbones(i_clk, i_reset, + // LGTLBSZ = 6, + // VAW=VIRTUAL_ADDRESS_WIDTH, + + // }}} + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, // Wishbone master interface from the CPU - o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, o_wb_sel, - i_wb_stall, i_wb_ack, i_wb_data, i_wb_err, + // {{{ + output wire o_wb_cyc, o_wb_stb, o_wb_we, + output wire [PAW-1:0] o_wb_addr, + output wire [BUS_WIDTH-1:0] o_wb_data, + output wire [BUS_WIDTH/8-1:0] o_wb_sel, + input wire i_wb_stall, i_wb_ack, + input wire [BUS_WIDTH-1:0] i_wb_data, + input wire i_wb_err, + // }}} // Incoming interrupts - i_ext_int, + input wire i_ext_int, // Our one outgoing interrupt - o_ext_int, + output wire o_ext_int, // Wishbone slave interface for debugging purposes - i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr, i_dbg_data, - o_dbg_stall, o_dbg_ack, o_dbg_data -`ifdef DEBUG_SCOPE - , o_cpu_debug -`endif - ); - parameter RESET_ADDRESS=32'h0100000, ADDRESS_WIDTH=30, - LGICACHE=`LGICACHE_DEFAULT, - LGDCACHE=`LGDCACHE_DEFAULT; - parameter [0:0] START_HALTED=0; - parameter EXTERNAL_INTERRUPTS=1, -`ifdef OPT_MULTIPLY - IMPLEMENT_MPY = `OPT_MULTIPLY; -`else - IMPLEMENT_MPY = 0; -`endif - parameter [0:0] -`ifdef OPT_DIVIDE - IMPLEMENT_DIVIDE=1, -`else - IMPLEMENT_DIVIDE=0, -`endif -`ifdef OPT_IMPLEMENT_FPU - IMPLEMENT_FPU=1, -`else - IMPLEMENT_FPU=0, -`endif - IMPLEMENT_LOCK=1; - localparam // Derived parameters - PHYSICAL_ADDRESS_WIDTH=ADDRESS_WIDTH, - PAW=ADDRESS_WIDTH, -`ifdef OPT_MMU - VIRTUAL_ADDRESS_WIDTH=30, -`else - VIRTUAL_ADDRESS_WIDTH=PAW, -`endif - LGTLBSZ = 6, - VAW=VIRTUAL_ADDRESS_WIDTH; - - localparam AW=ADDRESS_WIDTH; - input wire i_clk, i_reset; - // Wishbone master - output wire o_wb_cyc, o_wb_stb, o_wb_we; - output wire [(PAW-1):0] o_wb_addr; - output wire [31:0] o_wb_data; - output wire [3:0] o_wb_sel; - input wire i_wb_stall, i_wb_ack; - input wire [31:0] i_wb_data; - input wire i_wb_err; - // Incoming interrupts - input wire i_ext_int; - // Outgoing interrupt - output wire o_ext_int; - // Wishbone slave - input wire i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr; - input wire [31:0] i_dbg_data; - output wire o_dbg_ack; - output wire o_dbg_stall; - output wire [31:0] o_dbg_data; + // {{{ + input wire i_dbg_cyc, i_dbg_stb, i_dbg_we, + input wire [5:0] i_dbg_addr, + input wire [DBG_WIDTH-1:0] i_dbg_data, + input wire [DBG_WIDTH/8-1:0] i_dbg_sel, + output wire o_dbg_stall, + output wire o_dbg_ack, + output wire [DBG_WIDTH-1:0] o_dbg_data, + // }}} + output wire [31:0] o_cpu_debug, + // + output wire o_prof_stb, + output wire [ADDRESS_WIDTH-1:0] o_prof_addr, + output wire [31:0] o_prof_ticks + // }}} + ); + + // Declarations + // {{{ + localparam [0:0] DBG_ADDR_CTRL = 1'b0, + DBG_ADDR_CPU = 1'b1; + + // Debug bit allocations + // {{{ + // DBGCTRL + // 5 DBG Catch -- Catch exceptions/fautls w/ debugger + // 4 Clear cache + // 3 RESET_FLAG + // 2 STEP (W=1 steps, and returns to halted) + // 1 HALT(ED) + // 0 HALT + // DBGDATA + // read/writes internal registers // -`ifdef DEBUG_SCOPE - output wire [31:0] o_cpu_debug; -`endif - wire dbg_cyc, dbg_stb, dbg_we, dbg_addr, dbg_stall; - wire [31:0] dbg_idata, dbg_odata; - reg dbg_ack; + localparam HALT_BIT = 0, + STEP_BIT = 2, + RESET_BIT = 3, + CLEAR_CACHE_BIT = 4, + CATCH_BIT = 5; + // }}} + + wire cpu_clken; + wire dbg_cyc, dbg_stb, dbg_we, dbg_stall; + wire [5:0] dbg_addr; + wire [DBG_WIDTH-1:0] dbg_idata; + wire [DBG_WIDTH/8-1:0] dbg_sel; + reg [DBG_WIDTH-1:0] dbg_odata; + reg dbg_ack; + wire cpu_break, dbg_cmd_write, dbg_cpu_write; + wire reset_hold, halt_on_fault, dbg_catch; + wire reset_request, release_request, halt_request, + step_request, clear_cache_request; + reg cmd_reset, cmd_halt, cmd_step, cmd_clear_cache, + cmd_write; + reg [4:0] cmd_waddr; + reg [DBG_WIDTH-1:0] cmd_wdata; + wire [2:0] cpu_dbg_cc; + wire cpu_reset, cpu_halt, cpu_dbg_stall; + wire cpu_lcl_cyc, cpu_lcl_stb, + cpu_op_stall, cpu_pf_stall, cpu_i_count; + wire [DBG_WIDTH-1:0] cpu_dbg_data; + wire [DBG_WIDTH-1:0] cpu_status; + wire [DBG_WIDTH-1:0] dbg_cmd_data; + wire [DBG_WIDTH/8-1:0] dbg_cmd_strb; + reg dbg_pre_addr, dbg_pre_ack; + reg [DBG_WIDTH-1:0] dbg_cpu_status; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // The external debug interface + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // assign dbg_cyc = i_dbg_cyc; assign dbg_stb = i_dbg_stb; assign dbg_we = i_dbg_we; assign dbg_addr = i_dbg_addr; assign dbg_idata = i_dbg_data; + assign dbg_sel = i_dbg_sel; assign o_dbg_ack = dbg_ack; assign o_dbg_stall = dbg_stall; assign o_dbg_data = dbg_odata; + // }}} + //////////////////////////////////////////////////////////////////////// // // The external debug interface + // {{{ + //////////////////////////////////////////////////////////////////////// + // // // We offer only a limited interface here, requiring a pre-register // write to set the local address. This interface allows access to // the Zip System on a debug basis only, and not to the rest of the // wishbone bus. Further, to access these registers, the control - // register must first be accessed to both stop the CPU and to + // register must first be accessed to both stop the CPU and to // set the following address in question. Hence all accesses require // two accesses: write the address to the control register (and halt // the CPU if not halted), then read/write the data from the data // register. + + assign dbg_cpu_write = (dbg_stb && dbg_we) && (dbg_addr[5] == DBG_ADDR_CPU); + assign dbg_cmd_write = (dbg_stb)&&(dbg_we) + &&(dbg_addr[5] == DBG_ADDR_CTRL); + assign dbg_cmd_data = dbg_idata; + assign dbg_cmd_strb = dbg_sel; + + + assign reset_request = dbg_cmd_write && dbg_cmd_strb[RESET_BIT/8] + && dbg_cmd_data[RESET_BIT]; + assign release_request = dbg_cmd_write && dbg_cmd_strb[HALT_BIT/8] + && !dbg_cmd_data[HALT_BIT]; + assign halt_request = dbg_cmd_write && dbg_cmd_strb[HALT_BIT/8] + && dbg_cmd_data[HALT_BIT]; + assign step_request = dbg_cmd_write && dbg_cmd_strb[STEP_BIT/8] + && dbg_cmd_data[STEP_BIT]; + assign clear_cache_request = dbg_cmd_write + && dbg_cmd_strb[CLEAR_CACHE_BIT/8] + && dbg_cmd_data[CLEAR_CACHE_BIT]; + // - wire cpu_break, dbg_cmd_write; - reg cmd_reset, cmd_halt, cmd_step, cmd_clear_pf_cache; - reg [4:0] cmd_addr; - wire [3:0] cpu_dbg_cc; - assign dbg_cmd_write = (dbg_stb)&&(dbg_we)&&(!dbg_addr); - // + // reset_hold: Always start us off with an initial reset + // {{{ + generate if (RESET_DURATION > 0) + begin : INITIAL_RESET_HOLD + // {{{ + reg [$clog2(RESET_DURATION)-1:0] reset_counter; + reg r_reset_hold; + + initial reset_counter = RESET_DURATION; + always @(posedge i_clk) + if (i_reset) + reset_counter <= RESET_DURATION; + else if (reset_counter > 0) + reset_counter <= reset_counter - 1; + + initial r_reset_hold = 1; + always @(posedge i_clk) + if (i_reset) + r_reset_hold <= 1; + else + r_reset_hold <= (reset_counter > 1); + + assign reset_hold = r_reset_hold; +`ifdef FORMAL + always @(*) + assert(reset_hold == (reset_counter != 0)); +`endif + // }}} + end else begin + + assign reset_hold = 0; + + end endgenerate + // }}} + + assign halt_on_fault = dbg_catch; + + // cmd_reset + // {{{ // Always start us off with an initial reset - // initial cmd_reset = 1'b1; always @(posedge i_clk) - cmd_reset <= ((dbg_cmd_write)&&(dbg_idata[`RESET_BIT])); - // + if (i_reset) + cmd_reset <= 1'b1; + else if (reset_hold) + cmd_reset <= 1'b1; + else if (cpu_break && !halt_on_fault) + cmd_reset <= 1'b1; + else + cmd_reset <= reset_request; + // }}} + + // cmd_halt + // {{{ initial cmd_halt = START_HALTED; always @(posedge i_clk) if (i_reset) cmd_halt <= START_HALTED; - else if (cmd_reset) + else if (cmd_reset && START_HALTED) cmd_halt <= START_HALTED; - else if (dbg_cmd_write) - cmd_halt <= ((dbg_idata[`HALT_BIT])&&(!dbg_idata[`STEP_BIT])); - else if ((cmd_step)||(cpu_break)) - cmd_halt <= 1'b1; + else begin + // {{{ + // When shall we release from a halt? Only if we have come to + // a full and complete stop. Even then, we only release if we + // aren't being given a command to step the CPU. + // + if (!cmd_write && !cpu_dbg_stall && dbg_cmd_write + && (release_request || step_request)) + cmd_halt <= 1'b0; - initial cmd_clear_pf_cache = 1'b1; - always @(posedge i_clk) - cmd_clear_pf_cache <= (dbg_cmd_write)&&(dbg_idata[`CLEAR_CACHE_BIT]); - // - initial cmd_step = 1'b0; + // Reasons to halt + + // 1. Halt on any unhandled CPU exception. The cause of the + // exception must be cured before we can (re)start. + // If the CPU is configured to start immediately on power + // up, we leave it to reset on any exception instead. + if (cpu_break && halt_on_fault) + cmd_halt <= 1'b1; + + // 2. Halt on any user request to halt. (Only valid if the + // STEP bit isn't also set) + if (dbg_cmd_write && halt_request && !step_request) + cmd_halt <= 1'b1; + + // 3. Halt on any user request to write to a CPU register + if (dbg_cpu_write && dbg_cmd_strb != 0) + cmd_halt <= 1'b1; + + // 4. Halt following any step command + if (cmd_step) + cmd_halt <= 1'b1; + + // 4. Halt following any clear cache + if (cmd_clear_cache) + cmd_halt <= 1'b1; + + // 5. Halt on any clear cache bit--independent of any step bit + if (clear_cache_request) + cmd_halt <= 1'b1; + // }}} + end + // }}} + + // cmd_clear_cache + // {{{ + initial cmd_clear_cache = 1'b0; always @(posedge i_clk) - cmd_step <= (dbg_cmd_write)&&(dbg_idata[`STEP_BIT]); - // - initial cmd_addr = 5'h0; + if (i_reset || cpu_reset) + cmd_clear_cache <= 1'b0; + else if (dbg_cmd_write && clear_cache_request && halt_request) + cmd_clear_cache <= 1'b1; + else if (cmd_halt && !cpu_dbg_stall) + cmd_clear_cache <= 1'b0; + // }}} + + // cmd_step + // {{{ + initial cmd_step = 1'b0; always @(posedge i_clk) - if (dbg_cmd_write) - cmd_addr <= dbg_idata[4:0]; + if (i_reset) + cmd_step <= 1'b0; + else if (step_request) + cmd_step <= 1'b1; + else if (!cpu_dbg_stall) + cmd_step <= 1'b0; + // }}} - wire cpu_reset; - assign cpu_reset = (cmd_reset); + // dbg_catch + // {{{ + generate if (!OPT_DBGPORT) + begin : NO_DBG_CATCH + assign dbg_catch = START_HALTED; + end else begin : GEN_DBG_CATCH + reg r_dbg_catch; + + initial r_dbg_catch = START_HALTED; + always @(posedge i_clk) + if (i_reset) + r_dbg_catch <= START_HALTED; + else if (dbg_cmd_write && dbg_cmd_strb[CATCH_BIT/8]) + r_dbg_catch <= dbg_cmd_data[CATCH_BIT]; + + assign dbg_catch = r_dbg_catch; + end endgenerate + // }}} - wire cpu_halt, cpu_dbg_stall; + assign cpu_reset = (cmd_reset); assign cpu_halt = (cmd_halt); - wire [31:0] cmd_data; - // Values: - // 0x0003f -> cmd_addr mask - // 0x00040 -> reset - // 0x00080 -> PIC interrrupt pending - // 0x00100 -> cmd_step - // 0x00200 -> cmd_stall - // 0x00400 -> cmd_halt - // 0x00800 -> cmd_clear_pf_cache - // 0x01000 -> cc.sleep - // 0x02000 -> cc.gie - // 0x10000 -> External interrupt line is high - assign cmd_data = { 7'h00, 8'h00, i_ext_int, - cpu_dbg_cc, - 1'b0, cmd_halt, (!cpu_dbg_stall), 1'b0, - i_ext_int, cpu_reset, 1'b0, cmd_addr }; + // cpu_status + // {{{ + // 0xffff_f000 -> (Unused / reserved) + // + // 0x0000_0800 -> cpu_break + // 0x0000_0400 -> Interrupt pending + // 0x0000_0200 -> User mode + // 0x0000_0100 -> Sleep (CPU is sleeping) + // + // 0x0000_00c0 -> (Unused/reserved) + // 0x0000_0020 -> dbg_catch + // 0x0000_0010 -> cmd_clear_cache + // + // 0x0000_0008 -> Reset + // 0x0000_0004 -> Step (auto clearing, write only) + // 0x0000_0002 -> Halt (status) + // 0x0000_0001 -> Halt (request) + assign cpu_status = { 16'h0, 4'h0, + cpu_break, i_ext_int, cpu_dbg_cc[1:0], + 2'h0, dbg_catch, 1'b0, + cmd_reset, 1'b0, !cpu_dbg_stall, cmd_halt + }; + // }}} + + // cmd_write + // {{{ + initial cmd_write = 0; + always @(posedge i_clk) + if (i_reset || cpu_reset) + cmd_write <= 1'b0; + else if (!cmd_write || !cpu_dbg_stall) + cmd_write <= (dbg_cpu_write && dbg_cmd_strb != 0); + // }}} + + // cmd_waddr, cmd_wdata + // {{{ + always @(posedge i_clk) + if ((!cmd_write || (!cpu_dbg_stall)) && dbg_cpu_write) + begin + cmd_waddr <= dbg_addr[4:0]; + cmd_wdata <= dbg_idata; + end + // }}} + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// // // The CPU itself + // {{{ + //////////////////////////////////////////////////////////////////////// // - wire cpu_lcl_cyc, cpu_lcl_stb, - cpu_dbg_we, - cpu_op_stall, cpu_pf_stall, cpu_i_count; - wire [31:0] cpu_dbg_data; - assign cpu_dbg_we = ((dbg_stb)&&(dbg_we)&&(dbg_addr)); - zipcpu #(.RESET_ADDRESS(RESET_ADDRESS), - .ADDRESS_WIDTH(ADDRESS_WIDTH), - .LGICACHE(LGICACHE), - .OPT_LGDCACHE(LGDCACHE), - .WITH_LOCAL_BUS(0)) - thecpu(i_clk, cpu_reset, i_ext_int, - cpu_halt, cmd_clear_pf_cache, cmd_addr[4:0], cpu_dbg_we, - dbg_idata, cpu_dbg_stall, cpu_dbg_data, - cpu_dbg_cc, cpu_break, - o_wb_cyc, o_wb_stb, - cpu_lcl_cyc, cpu_lcl_stb, - o_wb_we, o_wb_addr, o_wb_data, o_wb_sel, - i_wb_stall, i_wb_ack, i_wb_data, - (i_wb_err)||(cpu_lcl_cyc), - cpu_op_stall, cpu_pf_stall, cpu_i_count -`ifdef DEBUG_SCOPE - , o_cpu_debug -`endif - ); + // + assign cpu_clken = cmd_write || dbg_stb; + + zipwb #( + // {{{ + .RESET_ADDRESS(RESET_ADDRESS), + .ADDRESS_WIDTH(ADDRESS_WIDTH-$clog2(BUS_WIDTH/8)), + .BUS_WIDTH(BUS_WIDTH), + .OPT_PIPELINED(OPT_PIPELINED), + .OPT_EARLY_BRANCHING(OPT_EARLY_BRANCHING), + .OPT_LGICACHE(OPT_LGICACHE), + .OPT_LGDCACHE(OPT_LGDCACHE), + .OPT_MPY(OPT_MPY), + .OPT_DIV(OPT_DIV), + .OPT_SHIFTS(OPT_SHIFTS), + .IMPLEMENT_FPU(OPT_FPU), + .OPT_CIS(OPT_CIS), + .OPT_LOCK(OPT_LOCK), + .OPT_LOWPOWER(OPT_LOWPOWER), + .OPT_START_HALTED(START_HALTED), + .OPT_SIM(OPT_SIM), + .OPT_DBGPORT(OPT_DBGPORT), + .OPT_TRACE_PORT(OPT_TRACE_PORT), + .OPT_PROFILER(OPT_PROFILER), + .OPT_CLKGATE(OPT_CLKGATE), + .OPT_DISTRIBUTED_REGS(OPT_DISTRIBUTED_REGS), + .OPT_USERMODE(OPT_USERMODE), + .WITH_LOCAL_BUS(0) + // }}} + ) thecpu( + // {{{ + .i_clk(i_clk), .i_reset(cpu_reset), .i_interrupt(i_ext_int), + .i_cpu_clken(cpu_clken), + // Debug interface + // {{{ + .i_halt(cpu_halt), .i_clear_cache(cmd_clear_cache), + .i_dbg_wreg(cmd_waddr), .i_dbg_we(cmd_write), + .i_dbg_data(cmd_wdata), + .i_dbg_rreg(dbg_addr[4:0]), + .o_dbg_stall(cpu_dbg_stall), + .o_dbg_reg(cpu_dbg_data), + .o_dbg_cc(cpu_dbg_cc), + .o_break(cpu_break), + // }}} + // Wishbone bus interface + // {{{ + .o_wb_gbl_cyc(o_wb_cyc), .o_wb_gbl_stb(o_wb_stb), + .o_wb_lcl_cyc(cpu_lcl_cyc), + .o_wb_lcl_stb(cpu_lcl_stb), + .o_wb_we(o_wb_we), .o_wb_addr(o_wb_addr), + .o_wb_data(o_wb_data), .o_wb_sel(o_wb_sel), + // Return values from the Wishbone bus + .i_wb_stall(i_wb_stall), .i_wb_ack(i_wb_ack), + .i_wb_data(i_wb_data), + .i_wb_err((i_wb_err)||(cpu_lcl_cyc)), + // }}} + .o_op_stall(cpu_op_stall), .o_pf_stall(cpu_pf_stall), + .o_i_count(cpu_i_count), + .o_debug(o_cpu_debug), + // + .o_prof_stb(o_prof_stb), + .o_prof_addr(o_prof_addr), + .o_prof_ticks(o_prof_ticks) + // }}} + ); + // }}} + //////////////////////////////////////////////////////////////////////// + // // Return debug response values - assign dbg_odata = (!dbg_addr)?cmd_data :cpu_dbg_data; + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + dbg_pre_addr <= dbg_addr[5]; + + always @(posedge i_clk) + dbg_cpu_status <= cpu_status; + + initial dbg_pre_ack = 1'b0; + always @(posedge i_clk) + if (i_reset || !i_dbg_cyc) + dbg_pre_ack <= 1'b0; + else + dbg_pre_ack <= dbg_stb && !dbg_stall; + initial dbg_ack = 1'b0; always @(posedge i_clk) - dbg_ack <= (dbg_stb)&&(!o_dbg_stall); - assign dbg_stall= (cpu_dbg_stall)&&(dbg_addr); + if (i_reset || !i_dbg_cyc) + dbg_ack <= 1'b0; + else + dbg_ack <= dbg_pre_ack; + + always @(posedge i_clk) + if (!OPT_LOWPOWER || dbg_pre_ack) + begin + if (dbg_pre_addr == DBG_ADDR_CTRL) + dbg_odata <= dbg_cpu_status; + else + dbg_odata <= cpu_dbg_data; + end + + assign dbg_stall = cpu_dbg_stall && dbg_addr[5] == DBG_ADDR_CPU + && (!dbg_we || cmd_write); + // }}} assign o_ext_int = (cmd_halt) && (!i_wb_stall); // Make Verilator happy + // {{{ // verilator lint_off UNUSED - wire [4:0] unused; - assign unused = { dbg_cyc, cpu_lcl_stb, cpu_op_stall, cpu_pf_stall, cpu_i_count }; + wire unused; + assign unused = &{ 1'b0, dbg_cyc, cpu_lcl_stb, cpu_op_stall, + cpu_dbg_cc[2], cpu_pf_stall, cpu_i_count }; // verilator lint_on UNUSED - + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL +// Formal properties for this core are maintained elsewhere +`endif +// }}} endmodule diff --git a/rtl/cpu/zipcore.v b/rtl/cpu/zipcore.v new file mode 100644 index 0000000..69f054f --- /dev/null +++ b/rtl/cpu/zipcore.v @@ -0,0 +1,5869 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: zipcore.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module zipcore #( + // {{{ + parameter ADDRESS_WIDTH=30, // 32-b word addr width + parameter [31:0] RESET_ADDRESS=32'h010_0000, + parameter OPT_MPY = 0, + parameter [0:0] OPT_SHIFTS = 1, + parameter [0:0] OPT_DIV = 1, + parameter [0:0] IMPLEMENT_FPU = 0, + parameter [0:0] OPT_EARLY_BRANCHING = 1, + parameter [0:0] OPT_CIS = 1'b1, + parameter [0:0] OPT_SIM = 1'b0, + parameter [0:0] OPT_DISTRIBUTED_REGS = 1'b1, + parameter [0:0] OPT_PIPELINED = 1'b1, + parameter [0:0] OPT_PIPELINED_BUS_ACCESS = (OPT_PIPELINED), + parameter [0:0] OPT_LOCK=1, + parameter [0:0] OPT_DCACHE = 1, + parameter [0:0] OPT_USERMODE = 1'b1, + parameter [0:0] OPT_LOWPOWER = 1'b0, + parameter [0:0] OPT_CLKGATE = 1'b1, + parameter [0:0] OPT_START_HALTED = 1'b1, + parameter [0:0] OPT_DBGPORT = 1'b1, + parameter [0:0] OPT_TRACE_PORT = 1'b0, + parameter [0:0] OPT_PROFILER = 1'b0, + + localparam AW=ADDRESS_WIDTH +`ifdef FORMAL + , parameter F_LGDEPTH=4 +`endif + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, i_interrupt, + output wire o_clken, + // Debug interface + // {{{ + input wire i_halt, i_clear_cache, + input wire [4:0] i_dbg_wreg, + input wire i_dbg_we, + input wire [31:0] i_dbg_data, + input wire [4:0] i_dbg_rreg, + // + output wire o_dbg_stall, + output wire [31:0] o_dbg_reg, + output reg [2:0] o_dbg_cc, + output wire o_break, + // }}} + // Instruction fetch interface + // {{{ + output wire o_pf_new_pc, + output wire o_clear_icache, + output wire o_pf_ready, + output wire [(AW+1):0] o_pf_request_address, + input wire i_pf_valid, i_pf_illegal, + input wire [31:0] i_pf_instruction, + input wire [(AW+1):0] i_pf_instruction_pc, + // }}} + // Memory unit interface + // {{{ + output wire o_clear_dcache, + output wire o_mem_ce, + output wire o_bus_lock, + output wire [2:0] o_mem_op, + output wire [31:0] o_mem_addr, + output wire [31:0] o_mem_data, + output wire [AW+1:0] o_mem_lock_pc, + output wire [4:0] o_mem_reg, + input wire i_mem_busy, i_mem_rdbusy, + i_mem_pipe_stalled, i_mem_valid, + input wire i_bus_err, + input wire [4:0] i_mem_wreg, + input wire [31:0] i_mem_result, + // }}} + // Accounting outputs ... to help us count stalls and usage + // {{{ + output wire o_op_stall, + output wire o_pf_stall, + output wire o_i_count, + // }}} + // Debug data for on-line/live tracing + // {{{ + output wire [31:0] o_debug, + //}}} + // (Optional) Profiler data + // {{{ + output wire o_prof_stb, + output wire [AW+1:0] o_prof_addr, + output wire [31:0] o_prof_ticks + // }}} + // }}} + ); + + // Local parameter definitions + // {{{ + // Verilator lint_off UNUSED + localparam [0:0] OPT_MEMPIPE = OPT_PIPELINED_BUS_ACCESS; + localparam [(AW-1):0] RESET_BUS_ADDRESS = RESET_ADDRESS[AW+1:2]; + localparam [3:0] CPU_CC_REG = 4'he, + CPU_PC_REG = 4'hf; + localparam [3:0] CPU_SUB_OP = 4'h0,// also a compare instruction + CPU_AND_OP = 4'h1,// also a test instruction + CPU_BREV_OP= 4'h8, + CPU_MOV_OP = 4'hd; + localparam CPU_CLRDCACHE_BIT=15, // Set to clr D-cache,auto clears + CPU_CLRICACHE_BIT=14, // Set to clr I-cache,auto clears + CPU_PHASE_BIT = 13, // Set on last half of a CIS + CPU_FPUERR_BIT = 12, // Set on floating point error + CPU_DIVERR_BIT = 11, // Set on divide error + CPU_BUSERR_BIT = 10, // Set on bus error + CPU_TRAP_BIT = 9, // User TRAP has taken place + CPU_ILL_BIT = 8, // Illegal instruction + CPU_BREAK_BIT = 7, + CPU_STEP_BIT = 6, // Will step one (or two CIS) ins + CPU_GIE_BIT = 5, + CPU_SLEEP_BIT = 4; + // Verilator lint_on UNUSED + // }}} + + // Register declarations + // {{{ + // The distributed RAM style comment is necessary on the + // SPARTAN6 with XST to prevent XST from oversimplifying the register + // set and in the process ruining everything else. It basically + // optimizes logic away, to where it no longer works. The logic + // as described herein will work, this just makes sure XST implements + // that logic. + // + (* ram_style = "distributed" *) + reg [31:0] regset [0:(OPT_USERMODE)? 31:15]; + + // Condition codes + // (BUS, TRAP,ILL,BREAKEN,STEP,GIE,SLEEP ), V, N, C, Z + reg [3:0] flags, iflags; + wire [15:0] w_uflags, w_iflags; + reg break_en, user_step, sleep, r_halted; + wire break_pending, trap, gie, ubreak, pending_interrupt, + stepped; + wire step; + wire ill_err_u; + reg ill_err_i; + reg ibus_err_flag; + wire ubus_err_flag; + wire idiv_err_flag, udiv_err_flag; + wire ifpu_err_flag, ufpu_err_flag; + wire ihalt_phase, uhalt_phase; + + // The master chip enable + wire master_ce, master_stall; + // + + // + // PIPELINE STAGE #1 :: Prefetch + // Variable declarations + // + // {{{ + reg [(AW+1):0] pf_pc; + reg new_pc; + wire clear_pipeline; + + reg dcd_stalled; + // wire pf_cyc, pf_stb, pf_we, pf_stall, pf_ack, pf_err; + // wire [(AW-1):0] pf_addr; + // wire pf_valid, pf_gie, pf_illegal; + wire pf_gie; +`ifdef FORMAL + wire [31:0] f_dcd_insn_word; + wire f_dcd_insn_gie; + wire f_dcd_insn_is_pipeable; + reg [31:0] f_op_insn_word; + reg [31:0] f_alu_insn_word; +`endif + + assign clear_pipeline = new_pc; + // }}} + + // + // PIPELINE STAGE #2 :: Instruction Decode + // Variable declarations + // + // + // {{{ + wire [3:0] dcd_opn; + wire dcd_ce, dcd_phase; + wire [4:0] dcd_A, dcd_B, dcd_R, dcd_preA, dcd_preB; + wire dcd_Acc, dcd_Bcc, dcd_Apc, dcd_Bpc, dcd_Rcc, dcd_Rpc; + wire [3:0] dcd_F; + wire dcd_wR, dcd_rA, dcd_rB, + dcd_ALU, dcd_M, dcd_DIV, dcd_FP, + dcd_wF, dcd_gie, dcd_break, dcd_lock, + dcd_pipe, dcd_ljmp; + wire dcd_valid; + wire [AW+1:0] dcd_pc /* verilator public_flat */; + wire [31:0] dcd_I; + wire dcd_zI; // true if dcd_I == 0 + wire dcd_A_stall, dcd_B_stall, dcd_F_stall; + + wire dcd_illegal; + wire dcd_early_branch, dcd_early_branch_stb; + wire [(AW+1):0] dcd_branch_pc; + + wire dcd_sim; + wire [22:0] dcd_sim_immv; + wire prelock_stall, last_lock_insn; + wire cc_invalid_for_dcd; + wire pending_sreg_write; + // }}} + + + // + // + // PIPELINE STAGE #3 :: Read Operands + // Variable declarations + // + // + // + // {{{ + // Now, let's read our operands + reg op_valid /* verilator public_flat */, + op_valid_mem, op_valid_alu; + reg op_valid_div, op_valid_fpu; + wire op_stall; + wire [3:0] op_opn; + wire [4:0] op_R; + reg op_Rcc; + wire [4:0] op_Aid, op_Bid; + wire op_rA, op_rB; + reg [31:0] r_op_Av, r_op_Bv; + wire [AW+1:0] op_pc; + wire [31:0] w_op_Av, w_op_Bv, op_Av, op_Bv; + reg [31:0] w_pcB_v, w_pcA_v; + reg [31:0] w_op_BnI; + wire op_wR; + reg op_wF; + wire op_gie; + wire [3:0] op_Fl; + reg [6:0] r_op_F; + wire [7:0] op_F; + wire op_ce, op_phase, op_pipe; + reg r_op_break; + wire w_op_valid; + wire op_lowpower_clear; + wire [8:0] w_cpu_info; + // Some pipeline control wires + reg op_illegal; + wire op_break; + wire op_lock; + + wire op_sim /* verilator public_flat */; + wire [22:0] op_sim_immv /* verilator public_flat */; + wire alu_sim /* verilator public_flat */; + wire [22:0] alu_sim_immv /* verilator public_flat */; + // }}} + + + // + // + // PIPELINE STAGE #4 :: ALU / Memory + // Variable declarations + // + // + // {{{ + wire [(AW+1):0] alu_pc; + reg [4:0] alu_reg; + reg r_alu_pc_valid, mem_pc_valid; + wire alu_pc_valid; + wire alu_phase; + wire alu_ce /* verilator public_flat */, alu_stall; + wire [31:0] alu_result; + wire [3:0] alu_flags; + wire alu_valid, alu_busy; + wire set_cond; + reg alu_wR, alu_wF; + wire alu_gie, alu_illegal; + + + wire mem_ce, mem_stalled; + + wire div_ce, div_error, div_busy, div_valid; + wire [31:0] div_result; + wire [3:0] div_flags; + + wire fpu_ce, fpu_error, fpu_busy, fpu_valid; + wire [31:0] fpu_result; + wire [3:0] fpu_flags; + reg adf_ce_unconditional; + + + reg dbgv, dbg_clear_pipe; + reg [31:0] dbg_val; + reg [31:0] debug_pc; + reg r_dbg_stall; + + assign div_ce = (op_valid_div)&&(adf_ce_unconditional)&&(set_cond); + assign fpu_ce = (IMPLEMENT_FPU)&&(op_valid_fpu)&&(adf_ce_unconditional)&&(set_cond); + + // }}} + + // + // + // PIPELINE STAGE #5 :: Write-back + // Variable declarations + // + // {{{ + wire wr_write_pc, wr_write_cc, + wr_write_scc, wr_write_ucc; + reg wr_reg_ce, wr_flags_ce; + reg [3:0] wr_flags; + reg [2:0] wr_index; + wire [4:0] wr_reg_id; + reg [31:0] wr_gpreg_vl, wr_spreg_vl; + wire w_switch_to_interrupt, w_release_from_interrupt; + reg [(AW+1):0] ipc; + wire [(AW+1):0] upc; + reg last_write_to_cc; + wire cc_write_hold; + reg r_clear_icache; + + reg pfpcset; + reg [2:0] pfpcsrc; + + wire w_clken; + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // MASTER: clock enable. + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + assign master_ce = (!i_halt || alu_phase) + &&(!cc_write_hold)&&(!o_break)&&(!sleep); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Calculate stall conditions + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + // PIPELINE STAGE #1 :: Prefetch + // These are calculated externally, within the prefetch module. + // + // PIPELINE STAGE #2 :: Instruction Decode + // {{{ + always @(*) + if (OPT_PIPELINED) + dcd_stalled = (dcd_valid && op_stall); + else + dcd_stalled = (!master_ce) + // Can't step forward when we need to be halted + ||(ill_err_i)||(ibus_err_flag)||(idiv_err_flag) + ||((dcd_valid || op_valid) && !dcd_early_branch) + ||(alu_busy)||(div_busy)||(fpu_busy)||(i_mem_busy); + // }}} + // + // PIPELINE STAGE #3 :: Read Operands + // {{{ + generate if (OPT_PIPELINED) + begin : GEN_OP_STALL + reg r_cc_invalid_for_dcd; + reg r_pending_sreg_write; + + // cc_invalid_for_dcd + // {{{ + initial r_cc_invalid_for_dcd = 1'b0; + always @(posedge i_clk) + if (clear_pipeline) + r_cc_invalid_for_dcd <= 1'b0; + else if ((alu_ce || mem_ce)&&(set_cond)&&(op_valid) &&((op_wF) + ||((op_wR)&&(op_R[4:0] == { op_gie, CPU_CC_REG })))) + // If an instruction in the pipeline will write to the + // CC register, then we can't be allowed to release + // any instruction depending upon the CC register + // (for other than conditional execution) into the + // pipeline. + r_cc_invalid_for_dcd <= 1'b1; + else if (r_cc_invalid_for_dcd) + // While the pipeline is busy, keep r_cc_invalid_for_dcd + // high. + r_cc_invalid_for_dcd <= ((alu_busy)||(i_mem_rdbusy)||(div_busy)||(fpu_busy)); + else + r_cc_invalid_for_dcd <= 1'b0; + + assign cc_invalid_for_dcd = r_cc_invalid_for_dcd; + // }}} + + // pending_sreg_write + // {{{ + // Used to determine if we need to stall for flags to be ready + initial r_pending_sreg_write = 1'b0; + always @(posedge i_clk) + if (clear_pipeline) + r_pending_sreg_write <= 1'b0; + else if (((adf_ce_unconditional)||(mem_ce)) + &&(set_cond)&&(!op_illegal) + &&(op_wR) + &&(op_R[3:1] == 3'h7) + &&(op_R[4:0] != { gie, 4'hf })) + r_pending_sreg_write <= 1'b1; + else if ((!i_mem_rdbusy)&&(!alu_busy)) + r_pending_sreg_write <= 1'b0; + + assign pending_sreg_write = r_pending_sreg_write; + // }}} + + assign op_stall = (op_valid)&&( + // {{{ + // Only stall if we're loaded w/valid insns and the + // next stage is accepting our instruction + (!adf_ce_unconditional)&&(!mem_ce) + ) + ||(dcd_valid)&&( + // If we are halted, then accepting anything + // into the Op stage might accept a register + // that then gets modified by the debugging + // interface so as to be invalid. + i_halt + // Stall if we need to wait for an operand A + // to be ready to read + || (dcd_A_stall) + // Likewise for B, also includes logic + // regarding immediate offset (register must + // be in register file if we need to add to + // an immediate) + ||(dcd_B_stall) + // Or if we need to wait on flags to work on the + // CC register + ||(dcd_F_stall) + ); + // }}} + assign op_ce = ((dcd_valid)||(dcd_illegal)||(dcd_early_branch))&&(!op_stall); + + end else begin : NO_OP_STALLS // !OPT_PIPELINED + // {{{ + assign op_stall = 1'b0; // (o_break)||(pending_interrupt); + assign op_ce = ((dcd_valid)||(dcd_early_branch))&&(!op_stall); + assign pending_sreg_write = 1'b0; + assign cc_invalid_for_dcd = 1'b0; + + // verilator coverage_off + // Verilator lint_off UNUSED + wire pipe_unused; + assign pipe_unused = &{ 1'b0, cc_invalid_for_dcd, + pending_sreg_write }; + // Verilator lint_on UNUSED + // verilator coverage_on + // }}} + end endgenerate + + // BUT ... op_ce is too complex for many of the data operations. So + // let's make their circuit enable code simpler. In particular, if + // op_ doesn't need to be preserved, we can change it all we want + // ... right? The clear_pipeline code, for example, really only needs + // to determine whether op_valid is true. + // assign op_change_data_ce = (!op_stall); + // }}} + // + // PIPELINE STAGE #4 :: ALU / Memory + // {{{ + // 1. Basic stall is if the previous stage is valid and the next is + // busy. + // 2. Also stall if the prior stage is valid and the master clock enable + // is de-selected + // 3. Stall if someone on the other end is writing the CC register, + // since we don't know if it'll put us to sleep or not. + // 4. Last case: Stall if we would otherwise move a break instruction + // through the ALU. Break instructions are not allowed through + // the ALU. + generate if (OPT_PIPELINED) + begin : GEN_ALU_STALL + // alu_stall, alu_ce + // {{{ + assign alu_stall = (((master_stall)||(i_mem_rdbusy))&&(op_valid_alu)) //Case 1&2 + ||(wr_reg_ce)&&(wr_write_cc); + // assign // alu_ce = (master_ce)&&(op_valid_alu)&&(!alu_stall) + // &&(!clear_pipeline)&&(!op_illegal) + // &&(!pending_sreg_write) + // &&(!alu_sreg_stall); + assign alu_ce = (adf_ce_unconditional)&&(op_valid_alu); + + // verilator coverage_off + // Verilator lint_off unused + wire unused_alu_stall = &{ 1'b0, alu_stall }; + // Verilator lint_on unused + // verilator coverage_on + // }}} + end else begin : NO_ALU_STALLS + // alu_stall, alu_ce + // {{{ + assign alu_stall = (master_stall); + //assign alu_ce = (master_ce)&&(op_valid_alu) + // &&(!clear_pipeline) + // &&(!alu_stall); + assign alu_ce = (adf_ce_unconditional)&&(op_valid_alu); + + // verilator coverage_off + // Verilator lint_off unused + wire unused_alu_stall = &{ 1'b0, alu_stall }; + // Verilator lint_on unused + // verilator coverage_on + // }}} + end endgenerate + // + + // mem_ce + // {{{ + // Note: if you change the conditions for mem_ce, you must also change + // alu_pc_valid. + // + assign mem_ce = (master_ce)&&(op_valid_mem)&&(!mem_stalled) + &&(!clear_pipeline); + // }}} + + // mem_stalled + // {{{ + generate if (OPT_PIPELINED_BUS_ACCESS) + begin + // {{{ + assign mem_stalled = (master_stall)||((op_valid_mem)&&( + (i_mem_pipe_stalled) + ||(i_bus_err)||(div_error) + ||(!op_pipe && i_mem_busy) + // Stall waiting for flags to be valid + // Or waiting for a write to the PC register + // Or CC register, since that can change the + // PC as well + ||((wr_reg_ce) + &&((wr_write_pc)||(wr_write_cc))))); + // }}} + end else if (OPT_PIPELINED) + begin + // {{{ + assign mem_stalled = (master_stall)||((op_valid_mem)&&( + (i_bus_err)||(div_error)||(i_mem_busy) + // Stall waiting for flags to be valid + // Or waiting for a write to the PC register + // Or CC register, since that can change the + // PC as well + ||((wr_reg_ce) + &&((wr_write_pc)||(wr_write_cc))))); + // }}} + end else begin + // {{{ + assign mem_stalled = master_stall || i_mem_busy; + // }}} + end endgenerate + // }}} + // }}} + + // + // Master stall condition + // {{{ + assign master_stall = (!master_ce)||(!op_valid)||(ill_err_i) + ||(step && stepped) + ||(ibus_err_flag)||(idiv_err_flag) + ||(pending_interrupt && !o_bus_lock)&&(!alu_phase) + ||(alu_busy)||(div_busy)||(fpu_busy)||(op_break) + ||((OPT_PIPELINED)&&( + prelock_stall + ||((i_mem_busy)&&(op_illegal)) + ||((i_mem_busy)&&(op_valid_div)) + ||(alu_illegal)||(o_break))); + // }}} + // + // (Everything but memory) stall condition + // {{{ + + // ALU, DIV, or FPU CE ... equivalent to the OR of all three of these + always @(*) + if (OPT_PIPELINED) + adf_ce_unconditional = + (!master_stall)&&(!op_valid_mem)&&(!i_mem_rdbusy) + &&(!i_mem_busy || !op_wR || op_R[4:1] != { gie, 3'h7}); + else + adf_ce_unconditional = (!master_stall)&&(op_valid)&&(!op_valid_mem); + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // + // PIPELINE STAGE #1 :: Instruction fetch + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + assign o_pf_ready = (!dcd_stalled && !dcd_phase); + + assign o_pf_new_pc = (new_pc)||((dcd_early_branch_stb)&&(!clear_pipeline)); + + assign o_pf_request_address = ((dcd_early_branch_stb)&&(!clear_pipeline)) + ? dcd_branch_pc:pf_pc; + assign pf_gie = gie; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // PIPELINE STAGE #2 :: Instruction Decode + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + assign dcd_ce =((OPT_PIPELINED)&&(!dcd_valid))||(!dcd_stalled); + wire [6:0] dcd_full_R, dcd_full_A, dcd_full_B; + + idecode #( + // {{{ + .ADDRESS_WIDTH(AW), + .OPT_MPY((OPT_MPY!=0)? 1'b1:1'b0), + .OPT_SHIFTS(OPT_SHIFTS), + .OPT_PIPELINED(OPT_PIPELINED), + .OPT_EARLY_BRANCHING(OPT_EARLY_BRANCHING), + .OPT_DIVIDE(OPT_DIV), + .OPT_FPU(IMPLEMENT_FPU), + .OPT_LOCK(OPT_LOCK), + .OPT_OPIPE(OPT_PIPELINED_BUS_ACCESS), + .OPT_USERMODE(OPT_USERMODE), + .OPT_SIM(OPT_SIM), + .OPT_CIS(OPT_CIS), + .OPT_LOWPOWER(OPT_LOWPOWER) + // }}} + ) instruction_decoder( + // {{{ + .i_clk(i_clk), + .i_reset(i_reset||clear_pipeline||o_clear_icache), + .i_ce(dcd_ce), + .i_stalled(dcd_stalled), + .i_instruction(i_pf_instruction), .i_gie(pf_gie), + .i_pc(i_pf_instruction_pc), .i_pf_valid(i_pf_valid), + .i_illegal(i_pf_illegal), + .o_valid(dcd_valid), .o_phase(dcd_phase), + .o_illegal(dcd_illegal), .o_pc(dcd_pc), + .o_dcdR(dcd_full_R), .o_dcdA(dcd_full_A), + .o_dcdB(dcd_full_B), + .o_preA(dcd_preA), .o_preB(dcd_preB), + .o_I(dcd_I), .o_zI(dcd_zI), .o_cond(dcd_F), + .o_wF(dcd_wF), .o_op(dcd_opn), + .o_ALU(dcd_ALU), .o_M(dcd_M), .o_DV(dcd_DIV), + .o_FP(dcd_FP), .o_break(dcd_break), .o_lock(dcd_lock), + .o_wR(dcd_wR), .o_rA(dcd_rA), .o_rB(dcd_rB), + .o_early_branch(dcd_early_branch), + .o_early_branch_stb(dcd_early_branch_stb), + .o_branch_pc(dcd_branch_pc), .o_ljmp(dcd_ljmp), + .o_pipe(dcd_pipe), + .o_sim(dcd_sim), .o_sim_immv(dcd_sim_immv) +`ifdef FORMAL + , .f_insn_word(f_dcd_insn_word), + .f_insn_gie(f_dcd_insn_gie), + .f_insn_is_pipeable(f_dcd_insn_is_pipeable) +`endif + // }}} + ); + + assign { dcd_Rcc, dcd_Rpc, dcd_R } = dcd_full_R; + assign { dcd_Acc, dcd_Apc, dcd_A } = dcd_full_A; + assign { dcd_Bcc, dcd_Bpc, dcd_B } = dcd_full_B; + assign dcd_gie = pf_gie; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // PIPELINE STAGE #3 :: Read Operands (Registers) + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // op_pipe + // {{{ + generate if (OPT_PIPELINED_BUS_ACCESS) + begin : GEN_OP_PIPE + reg r_op_pipe; + + // To be a pipeable operation, there must be + // two valid adjacent instructions + // Both must be memory instructions + // Both must be writes, or both must be reads + // Both operations must be to the same identical address, + // or at least a single (one) increment above that + // address + // + // However ... we need to know this before this clock, hence + // this is calculated in the instruction decoder. + initial r_op_pipe = 1'b0; + always @(posedge i_clk) + if ((OPT_LOWPOWER && i_reset)||(clear_pipeline)||(i_halt)) + r_op_pipe <= 1'b0; + else if (op_ce) + r_op_pipe <= (dcd_pipe)&&(op_valid_mem)&&(!OPT_LOWPOWER || !dcd_illegal); + else if ((wr_reg_ce)&&(wr_reg_id == op_Bid[4:0])) + r_op_pipe <= 1'b0; + else if (mem_ce) // Clear us any time an op_ is clocked in + r_op_pipe <= 1'b0; + + assign op_pipe = r_op_pipe; +`ifdef FORMAL + always @(*) + if (OPT_LOWPOWER && !op_valid_mem) + assert(!r_op_pipe); +`endif + end else begin + + assign op_pipe = 1'b0; + + end endgenerate + // }}} + + // Read from our register set + // {{{ + generate if (OPT_DISTRIBUTED_REGS) + begin : GEN_DISTRIBUTED_REGS + // {{{ + if (OPT_USERMODE) + begin : GEN_FULL_REGSET + + assign w_op_Av = regset[dcd_A]; + assign w_op_Bv = regset[dcd_B]; + + end else begin : GEN_NO_USERREGS + assign w_op_Av = regset[dcd_A[3:0]]; + assign w_op_Bv = regset[dcd_B[3:0]]; + end + + // verilator coverage_off + // verilator lint_off UNUSED + wire unused_prereg_addrs; + assign unused_prereg_addrs = &{ 1'b0, dcd_preA, dcd_preB }; + // verilator lint_on UNUSED + // verilator coverage_on + // }}} + end else begin : GEN_BLOCKRAM + // {{{ + reg [31:0] pre_rewrite_value, pre_op_Av, pre_op_Bv; + reg pre_rewrite_flag_A, pre_rewrite_flag_B; + + always @(posedge i_clk) + if (dcd_ce) + begin + pre_rewrite_flag_A <= (wr_reg_ce)&&(dcd_preA == wr_reg_id); + pre_rewrite_flag_B <= (wr_reg_ce)&&(dcd_preB == wr_reg_id); + pre_rewrite_value <= wr_gpreg_vl; + end else if (OPT_PIPELINED && dcd_valid) + begin + pre_rewrite_flag_A <= (wr_reg_ce)&&(dcd_A == wr_reg_id); + pre_rewrite_flag_B <= (wr_reg_ce)&&(dcd_B == wr_reg_id); + pre_rewrite_value <= wr_gpreg_vl; + end + + if (OPT_USERMODE) + begin : GEN_FULL_REGSET + + always @(posedge i_clk) + if (dcd_ce || (OPT_PIPELINED && dcd_valid)) + begin + pre_op_Av <= regset[dcd_ce ? dcd_preA : dcd_A]; + pre_op_Bv <= regset[dcd_ce ? dcd_preB : dcd_B]; + end + + end else begin : GEN_NO_USERREGS + always @(posedge i_clk) + if (dcd_ce || (OPT_PIPELINED && dcd_valid)) + begin + pre_op_Av <= regset[dcd_ce ? dcd_preA[3:0] : dcd_A[3:0]]; + pre_op_Bv <= regset[dcd_ce ? dcd_preB[3:0] : dcd_B[3:0]]; + end + end + + assign w_op_Av = (pre_rewrite_flag_A) ? pre_rewrite_value : pre_op_Av; + assign w_op_Bv = (pre_rewrite_flag_B) ? pre_rewrite_value : pre_op_Bv; + // }}} + end endgenerate + // }}} + + assign w_cpu_info = { + // {{{ + 1'b1, + (OPT_MPY >0)? 1'b1:1'b0, + (OPT_DIV >0)? 1'b1:1'b0, + (IMPLEMENT_FPU >0)? 1'b1:1'b0, + OPT_PIPELINED, + 1'b0, + (OPT_EARLY_BRANCHING > 0)? 1'b1:1'b0, + OPT_PIPELINED_BUS_ACCESS, + OPT_CIS + }; + // }}} + + always @(*) + begin + w_pcA_v = 0; + if ((!OPT_USERMODE)||(dcd_A[4] == dcd_gie)) + w_pcA_v[(AW+1):0] = { dcd_pc[AW+1:2], 2'b00 }; + else + w_pcA_v[(AW+1):0] = { upc[(AW+1):2], uhalt_phase, 1'b0 }; + end + + // Op register addresses + // {{{ + generate if (OPT_PIPELINED) + begin : OP_REG_ADVANEC + // {{{ + reg [4:0] r_op_R; + reg [4:0] r_op_Aid, r_op_Bid; + reg r_op_rA, r_op_rB; + + initial r_op_R = 0; + initial r_op_Aid = 0; + initial r_op_Bid = 0; + initial r_op_rA = 0; + initial r_op_rB = 0; + initial op_Rcc = 0; + always @(posedge i_clk) + begin + if (op_ce && (!OPT_LOWPOWER || w_op_valid)) + begin + r_op_R <= dcd_R; + r_op_Aid <= dcd_A; + if ((dcd_rB)&&(!dcd_early_branch)&&(!dcd_illegal)) + r_op_Bid <= dcd_B; + r_op_rA <= (dcd_rA)&&(!dcd_early_branch)&&(!dcd_illegal); + r_op_rB <= (dcd_rB)&&(!dcd_early_branch)&&(!dcd_illegal); + op_Rcc <= (dcd_Rcc)&&(dcd_wR)&&(dcd_R[4]==dcd_gie); + end + + if (op_lowpower_clear) + begin + r_op_rA <= 1'b0; + r_op_rB <= 1'b0; + end + end + + assign op_R = r_op_R; + assign op_Aid = r_op_Aid; + assign op_Bid = r_op_Bid; + assign op_rA = r_op_rA; + assign op_rB = r_op_rB; + // }}} + end else begin : OP_REG_COPY + // {{{ + assign op_R = dcd_R; + assign op_Aid = dcd_A; + assign op_Bid = dcd_B; + assign op_rA = dcd_rA; + assign op_rB = dcd_rB; + + always @(*) + op_Rcc = (dcd_Rcc)&&(dcd_wR)&&(dcd_R[4]==dcd_gie); + // }}} + end endgenerate + // }}} + + // r_op_Av -- The registered value of the op A register + // {{{ + reg [2:0] avsrc; + reg [2:0] bvsrc; + reg [1:0] bisrc; + + always @(*) + begin + avsrc = 3'b000; + if (!OPT_PIPELINED || op_ce) + begin + if (dcd_Apc) + avsrc = 3'b101; + else if (dcd_Acc) + avsrc = 3'b110; + else + avsrc = 3'b111; + end + + if (OPT_PIPELINED && wr_reg_ce) + begin + if (!op_ce && wr_reg_id == op_Aid && op_rA) + avsrc = 3'b100; + else if (op_ce && wr_reg_id == dcd_A) + avsrc = 3'b100; + end + end + +// 44313 + initial r_op_Av = 0; + always @(posedge i_clk) + begin + if (avsrc[2]) + case(avsrc[1:0]) + 2'b00: r_op_Av <= wr_gpreg_vl; + 2'b01: r_op_Av <= w_pcA_v; + 2'b10: r_op_Av <= { w_cpu_info, w_op_Av[22:16], (dcd_A[4])?w_uflags:w_iflags }; + 2'b11: r_op_Av <= w_op_Av; + endcase + + if (op_lowpower_clear) + r_op_Av <= 0; + end + // }}} + + // w_op_B -- The registered value of the op B register + // {{{ + always @(*) + begin + w_pcB_v = 0; + if ((!OPT_USERMODE)||(dcd_B[4] == dcd_gie)) + w_pcB_v[(AW+1):0] = { dcd_pc[AW+1:2], 2'b00 }; + else + w_pcB_v[(AW+1):0] = { upc[(AW+1):2], uhalt_phase, 1'b0 }; + end + + always @(*) + if (!dcd_rB) + bisrc = 0; + else if ((OPT_PIPELINED)&&(wr_reg_ce)&&(wr_reg_id == dcd_B)) + bisrc = 1; + else if (dcd_Bcc) + bisrc = 2; + else + bisrc = 3; + + always @(*) + case(bisrc[1:0]) + 2'b00: w_op_BnI = 0; + 2'b01: w_op_BnI = wr_gpreg_vl; + 2'b10: w_op_BnI = { w_cpu_info, w_op_Bv[22:16], + (dcd_B[4]) ? w_uflags : w_iflags }; + 2'b11: w_op_BnI = w_op_Bv; + endcase + + always @(*) + begin + bvsrc = 0; + if ((!OPT_PIPELINED)||(op_ce)) + begin + if ((dcd_Bpc)&&(dcd_rB)) + bvsrc = 3'b100; + else + bvsrc = 3'b101; + end else if ((OPT_PIPELINED)&&(op_rB) + &&(wr_reg_ce)&&(op_Bid == wr_reg_id)) + bvsrc = 3'b110; + end + + initial r_op_Bv = 0; + always @(posedge i_clk) + begin + if (bvsrc[2]) casez(bvsrc[1:0]) + 2'b00: r_op_Bv <= w_pcB_v + { dcd_I[29:0], 2'b00 }; + 2'b01: r_op_Bv <= w_op_BnI + dcd_I; + 2'b1?: r_op_Bv <= wr_gpreg_vl; + endcase + + if (op_lowpower_clear) + r_op_Bv <= 0; + end + // }}} + + // op_F + // {{{ + // The logic here has become more complex than it should be, no thanks + // to Xilinx's Vivado trying to help. The conditions are supposed to + // be two sets of four bits: the top bits specify what bits matter, the + // bottom specify what those top bits must equal. However, two of + // conditions check whether bits are on, and those are the only two + // conditions checking those bits. Therefore, Vivado complains that + // these two bits are redundant. Hence the convoluted expression + // below, arriving at what we finally want in the (now wire net) + // op_F. + initial r_op_F = 7'h00; + always @(posedge i_clk) + begin + if ((!OPT_PIPELINED)||(op_ce)) + // Cannot do op_change_data_ce here since op_F depends + // upon being either correct for a valid op, or correct + // for the last valid op + begin // Set the flag condition codes, bit order is [3:0]=VNCZ + case(dcd_F[2:0]) + 3'h0: r_op_F <= 7'h00; // Always + 3'h1: r_op_F <= 7'h11; // Z + 3'h2: r_op_F <= 7'h44; // LT + 3'h3: r_op_F <= 7'h22; // C + 3'h4: r_op_F <= 7'h08; // V + 3'h5: r_op_F <= 7'h10; // NE + 3'h6: r_op_F <= 7'h40; // GE (!N) + 3'h7: r_op_F <= 7'h20; // NC + endcase + end + + if (op_lowpower_clear) + r_op_F <= 7'h00; + end // Bit order is { (flags_not_used), VNCZ mask, VNCZ value } + assign op_F = { r_op_F[3], r_op_F[6:0] }; + // }}} + + // op_valid + // {{{ + assign w_op_valid = (!clear_pipeline)&&(dcd_valid) + &&(!dcd_ljmp)&&(!dcd_early_branch); + + initial op_valid = 1'b0; + initial op_valid_alu = 1'b0; + initial op_valid_mem = 1'b0; + initial op_valid_div = 1'b0; + initial op_valid_fpu = 1'b0; + always @(posedge i_clk) + if ((i_reset)||(clear_pipeline)) + begin + // {{{ + op_valid <= 1'b0; + op_valid_alu <= 1'b0; + op_valid_mem <= 1'b0; + op_valid_div <= 1'b0; + op_valid_fpu <= 1'b0; + // }}} + end else if (op_ce) + begin + // {{{ + // Do we have a valid instruction? + // The decoder may vote to stall one of its + // instructions based upon something we currently + // have in our queue. This instruction must then + // move forward, and get a stall cycle inserted. + // Hence, the test on dcd_stalled here. If we must + // wait until our operands are valid, then we aren't + // valid yet until then. + if (OPT_PIPELINED || !op_valid) + begin + op_valid <= (w_op_valid)||(dcd_early_branch); + op_valid_alu <= (w_op_valid)&&((dcd_ALU)||(dcd_illegal)); + op_valid_mem <= (dcd_M)&&(!dcd_illegal) + &&(w_op_valid); + op_valid_div <= (OPT_DIV)&&(dcd_DIV)&&(!dcd_illegal)&&(w_op_valid); + op_valid_fpu <= (IMPLEMENT_FPU)&&(dcd_FP)&&(!dcd_illegal)&&(w_op_valid); + end else if ((adf_ce_unconditional)||(mem_ce)) + begin + op_valid <= 1'b0; + op_valid_alu <= 1'b0; + op_valid_mem <= 1'b0; + op_valid_div <= 1'b0; + op_valid_fpu <= 1'b0; + end + // }}} + end else if ((adf_ce_unconditional)||(mem_ce)) + begin + // {{{ + // If nothing advances into the OP stage, yet what was in + // the OP stage moves forward, then we need to invalidate what + // used to be here. + op_valid <= 1'b0; + op_valid_alu <= 1'b0; + op_valid_mem <= 1'b0; + op_valid_div <= 1'b0; + op_valid_fpu <= 1'b0; + // }}} + end + // }}} + + // op_lowpower_clear + // {{{ + generate if (!OPT_LOWPOWER || !OPT_PIPELINED) + begin : NO_OP_LOWPOWER_CLEAR + + assign op_lowpower_clear = 1'b0; + + end else begin : GEN_OP_LOWPOWER_CLEAR + reg r_op_lowpower_clear; + + always @(*) + if (i_reset || clear_pipeline) + r_op_lowpower_clear = 1'b1; + else if (op_ce && !w_op_valid) + r_op_lowpower_clear = 1'b1; + else if ((!op_ce || !w_op_valid)&&(adf_ce_unconditional || mem_ce)) + r_op_lowpower_clear = 1'b1; + else + r_op_lowpower_clear = 1'b0; + + assign op_lowpower_clear = r_op_lowpower_clear; + end endgenerate + // }}} + + // op_break + // {{{ + // Here's part of our debug interface. When we recognize a break + // instruction, we set the op_break flag. That'll prevent this + // instruction from entering the ALU, and cause an interrupt before + // this instruction. Thus, returning to this code will cause the + // break to repeat and continue upon return. To get out of this + // condition, replace the break instruction with what it is supposed + // to be, step through it, and then replace it back. In this fashion, + // a debugger can step through code. + // assign w_op_break = (dcd_break)&&(r_dcd_I[15:0] == 16'h0001); + + initial r_op_break = 1'b0; + always @(posedge i_clk) + if (clear_pipeline) + r_op_break <= 1'b0; + else if ((OPT_PIPELINED)&&(op_ce)) + r_op_break <= (dcd_valid)&&(dcd_break)&&(!dcd_illegal); + else if ((!OPT_PIPELINED)&&(dcd_valid)) + r_op_break <= (dcd_break)&&(!dcd_illegal)&&(!step || !stepped); + + assign op_break = r_op_break; +`ifdef FORMAL + always @(*) + if (op_break) + assert(op_valid || clear_pipeline); +`endif + // }}} + + // op_lock + // {{{ + generate if (!OPT_LOCK) + begin : NO_OPLOCK + + assign op_lock = 1'b0; + + // verilator coverage_off + // Verilator lint_off UNUSED + wire dcd_lock_unused; + assign dcd_lock_unused = &{ 1'b0, dcd_lock }; + // Verilator lint_on UNUSED + // verilator coverage_on + + end else // if (!OPT_LOCK) + begin : GEN_OPLOCK + reg r_op_lock; + + initial r_op_lock = 1'b0; + always @(posedge i_clk) + if (clear_pipeline) + r_op_lock <= 1'b0; + else if (op_ce) + r_op_lock <= (dcd_valid)&&(dcd_lock) + &&(!dcd_illegal); + assign op_lock = r_op_lock; + + end endgenerate + // }}} + + // op_illegal + // {{{ + initial op_illegal = 1'b0; + always @(posedge i_clk) + if ((i_reset)||(clear_pipeline)) + op_illegal <= 1'b0; + else if (OPT_PIPELINED) + begin + if (op_ce) + op_illegal <= (dcd_valid)&&(!dcd_ljmp) + &&(!dcd_early_branch)&&(dcd_illegal); + end else if (!OPT_PIPELINED) + begin + if (dcd_valid) + op_illegal <= (!dcd_ljmp)&&(!dcd_early_branch)&&(dcd_illegal); + end + // }}} + + // op_wF + // {{{ + always @(posedge i_clk) + begin + if ((!OPT_PIPELINED)||(op_ce)) + op_wF <= (dcd_wF)&&((!dcd_Rcc)||(!dcd_wR)) + &&(!dcd_early_branch); + + if (op_lowpower_clear) + op_wF <= 1'b0; + end + // }}} + + // op_wR + // {{{ + generate if ((OPT_PIPELINED)||(OPT_EARLY_BRANCHING)) + begin + reg r_op_wR; + + initial r_op_wR = 1'b0; + always @(posedge i_clk) + begin + if (op_ce) + r_op_wR <= (dcd_wR)&&(!dcd_early_branch); + if (op_lowpower_clear) + r_op_wR <= 1'b0; + end + + assign op_wR = r_op_wR; + end else begin + + assign op_wR = dcd_wR; + + end endgenerate + // }}} + + // op_sim, op_sim_immv + // {{{ + generate if (OPT_SIM) + begin : OP_SIM + // + // Only execute this if OPT_SIM is true--that is, if we + // are running from a simulated environment. + // + reg r_op_sim; + reg [22:0] r_op_sim_immv; + + always @(posedge i_clk) + if (op_ce) + begin + r_op_sim <= dcd_sim && (!OPT_LOWPOWER || w_op_valid); + r_op_sim_immv <= dcd_sim_immv; + if (OPT_LOWPOWER && (!dcd_sim || !w_op_valid)) + r_op_sim_immv <= 0; + end else if (adf_ce_unconditional) + begin + r_op_sim <= 1'b0; + if (OPT_LOWPOWER) + r_op_sim_immv <= 0; + end + + assign op_sim = r_op_sim; + assign op_sim_immv = r_op_sim_immv; + + end else begin : NO_OP_SIM + + assign op_sim = 0; + assign op_sim_immv = 0; + + // verilator coverage_off + // Verilator lint_off UNUSED + wire op_sim_unused; + assign op_sim_unused = &{ 1'b0, dcd_sim, dcd_sim_immv }; + // Verilator lint_on UNUSED + // verilator coverage_on + end endgenerate + // }}} + + // op_pc + // {{{ + generate if ((OPT_PIPELINED)||(OPT_EARLY_BRANCHING)) + begin : SET_OP_PC + + reg [AW+1:0] r_op_pc; + + initial r_op_pc[0] = 1'b0; + always @(posedge i_clk) + if (op_ce) + begin + if (dcd_early_branch) + // Need to retire an early branch as a NOOP, + // to make sure our PC is properly updated + r_op_pc <= dcd_branch_pc; + else if (!OPT_LOWPOWER || w_op_valid) + r_op_pc <= dcd_pc; + end + + assign op_pc = r_op_pc; + + end else begin : SET_OP_PC + + assign op_pc = dcd_pc; + + end endgenerate + // }}} + + // op_opn -- the opcode of the current operation + // {{{ + generate if (!OPT_PIPELINED) + begin : COPY_DCD_OPN + + assign op_opn = dcd_opn; + + end else begin : FWD_OPERATION + + reg [3:0] r_op_opn; + + always @(posedge i_clk) + if (op_ce && (!OPT_LOWPOWER || w_op_valid || dcd_early_branch)) + begin + // Which ALU operation? Early branches are + // unimplemented moves + r_op_opn <= ((dcd_early_branch)||(dcd_illegal)) + ? CPU_MOV_OP : dcd_opn; + // opM <= dcd_M; // Is this a memory operation? + // What register will these results be written into? + end + + assign op_opn = r_op_opn; + + end endgenerate + // }}} + assign op_gie = gie; + + assign op_Fl = (op_gie)?(w_uflags[3:0]):(w_iflags[3:0]); + + // op_phase + // {{{ + generate if (OPT_CIS) + begin : OPT_CIS_OP_PHASE + + reg r_op_phase; + + initial r_op_phase = 1'b0; + always @(posedge i_clk) + if ((i_reset)||(clear_pipeline)) + r_op_phase <= 1'b0; + else if (op_ce) + r_op_phase <= (dcd_phase)&&((!dcd_wR)||(!dcd_Rpc)); + + assign op_phase = r_op_phase; + end else begin : OPT_NOCIS_OP_PHASE + assign op_phase = 1'b0; + + // verilator lint_off UNUSED + wire OPT_CIS_dcdRpc; + assign OPT_CIS_dcdRpc = dcd_Rpc; + // verilator lint_on UNUSED + end endgenerate + // }}} + + // op_Av -- the combinatorial A value input to the ALU/MEM/DIV stage + // {{{ + // This is tricky. First, the PC and Flags registers aren't kept in + // register set but in special registers of their own. So step one + // is to select the right register. Step to is to replace that + // register with the results of an ALU or memory operation, if such + // results are now available. Otherwise, we'd need to insert a wait + // state of some type. + // + // The alternative approach would be to define some sort of + // op_stall wire, which would stall any upstream stage. + // We'll create a flag here to start our coordination. Once we + // define this flag to something other than just plain zero, then + // the stalls will already be in place. + generate if (OPT_PIPELINED) + begin + + assign op_Av = ((wr_reg_ce)&&(wr_reg_id == op_Aid)) + ? wr_gpreg_vl : r_op_Av; + + end else begin + + assign op_Av = r_op_Av; + + end endgenerate + // }}} + + // dcd_A_stall + // {{{ + // Stall if we have decoded an instruction that will read register A + // AND ... something that may write a register is running + // AND (series of conditions here ...) + // The operation might set flags, and we wish to read the + // CC register + // OR ... (No other conditions) + generate if (OPT_PIPELINED) + begin + + assign dcd_A_stall = (dcd_rA) // &&(dcd_valid) is checked for elsewhere + &&((op_valid)||(i_mem_rdbusy) + ||(div_busy)||(fpu_busy)) + &&(((op_wF)||(cc_invalid_for_dcd))&&(dcd_Acc)) + ||((dcd_rA)&&(dcd_Acc)&&(cc_invalid_for_dcd)); + end else begin + + // There are no pipeline hazards, if we aren't pipelined + assign dcd_A_stall = 1'b0; + + end endgenerate + // }}} + + // op_Bv + // {{{ + assign op_Bv = ((OPT_PIPELINED)&&(wr_reg_ce) + &&(wr_reg_id == op_Bid)&&(op_rB)) + ? wr_gpreg_vl: r_op_Bv; + // }}} + + // dcd_B_stall, dcd_F_stall + // {{{ + generate if (OPT_PIPELINED) + begin : DCD_BF_STALLS + // Stall if we have decoded an instruction that will read register B + // AND ... something that may write a (unknown) register is running + // AND (series of conditions here ...) + // The operation might set flags, and we wish to read the + // CC register + // OR the operation might set register B, and we still need + // a clock to add the offset to it + assign dcd_B_stall = (dcd_rB) // &&(dcd_valid) is checked for elsewhere + // {{{ + // If the op stage isn't valid, yet something + // is running, then it must have been valid. + // We'll use the last values from that stage + // (op_wR, op_wF, op_R) in our logic below. + &&((op_valid)||(i_mem_rdbusy) + ||(div_busy)||(fpu_busy)||(alu_busy)) + &&( + // Okay, what happens if the result register + // from instruction 1 becomes the input for + // instruction two, *and* there's an immediate + // offset in instruction two? In that case, we + // need an extra clock between the two + // instructions to calculate the base plus + // offset. + // + // What if instruction 1 (or before) is in a + // memory pipeline? We may no longer know what + // the register was! We will then need to + // blindly wait. We'll temper this only waiting + // if we're not piping this new instruction. + // If we were piping, the pipe logic in the + // decode circuit has told us that the hazard + // is clear, so we're okay then. + // + ((!dcd_zI)&&( + ((op_R == dcd_B)&&(op_wR)) + ||((i_mem_rdbusy)&&(!dcd_pipe)) + ||(((alu_busy || + div_busy || i_mem_rdbusy)) + &&(alu_reg == dcd_B)) + ||((wr_reg_ce)&&(wr_reg_id[3:1] == 3'h7)) + )) + // Stall following any instruction that will + // set the flags, if we're going to need the + // flags (CC) register for op_B. + ||(((op_wF)||(cc_invalid_for_dcd))&&(dcd_Bcc)) + // Stall on any ongoing memory operation that + // will write to op_B -- captured above + // ||((i_mem_busy)&&(!mem_we)&&(mem_last_reg==dcd_B)&&(!dcd_zI)) + ) + ||((dcd_rB)&&(dcd_Bcc)&&(cc_invalid_for_dcd)); + // }}} + assign dcd_F_stall = ((!dcd_F[3]) + // {{{ + ||((dcd_rA)&&(dcd_A[3:1]==3'h7) + &&(dcd_A[4:0] != { gie, 4'hf})) + ||((dcd_rB)&&(dcd_B[3:1]==3'h7)) + &&(dcd_B[4:0] != { gie, 4'hf})) + &&(((op_valid)&&(op_wR) + &&(op_R[3:1]==3'h7) + &&(op_R[4:0]!={gie, 4'hf})) + ||(pending_sreg_write)); + // &&(dcd_valid) is checked for elsewhere + // }}} + end else begin : NO_PIPELINE_NO_STALL + // {{{ + // No stalls without pipelining, 'cause how can you have a + // pipeline hazard without the pipeline? + assign dcd_B_stall = 1'b0; + assign dcd_F_stall = 1'b0; + // }}} + end endgenerate + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // PIPELINE STAGE #4 :: Apply Instruction + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // The ALU itself + cpuops #( + // {{{ + .OPT_MPY(OPT_MPY), + .OPT_SHIFTS(OPT_SHIFTS), + .OPT_LOWPOWER(OPT_LOWPOWER) + // }}} + ) doalu( + // {{{ + i_clk, ((i_reset)||(clear_pipeline)), + alu_ce, op_opn, op_Av, op_Bv, + alu_result, alu_flags, alu_valid, alu_busy); + // }}} + + // Divide + // {{{ + generate if (OPT_DIV != 0) + begin : DIVIDE +`ifdef FORMAL +`define DIVIDE_MODULE abs_div +`else +`define DIVIDE_MODULE div +`endif + `DIVIDE_MODULE #( + .OPT_LOWPOWER(OPT_LOWPOWER) + ) thedivide( + // {{{ + .i_clk(i_clk), .i_reset(i_reset || clear_pipeline), + .i_wr(div_ce), .i_signed(op_opn[0]), + .i_numerator(op_Av), .i_denominator(op_Bv), + .o_busy(div_busy), .o_valid(div_valid), + .o_err(div_error), .o_quotient(div_result), + .o_flags(div_flags) + // }}} + ); + + end else begin + // {{{ + assign div_error = 1'b0; // Can't be high unless div_valid + assign div_busy = 1'b0; + assign div_valid = 1'b0; + assign div_result= 32'h00; + assign div_flags = 4'h0; + + // Make verilator happy here + // verilator coverage_off + // verilator lint_off UNUSED + wire unused_divide; + assign unused_divide = &{ 1'b0, div_ce }; + // verilator lint_on UNUSED + // verilator coverage_on + // }}} + end endgenerate + // }}} + + // (Non-existent) FPU + // {{{ + generate if (IMPLEMENT_FPU != 0) + begin : FPU + // + // sfpu thefpu(i_clk, i_reset, fpu_ce, op_opn[2:0], + // op_Av, op_Bv, fpu_busy, fpu_valid, fpu_err, fpu_result, + // fpu_flags); + // +`ifdef FORMAL + abs_div thefpu(i_clk, ((i_reset)||(clear_pipeline)), + fpu_ce, op_opn[2:0], + op_Av, op_Bv, fpu_busy, fpu_valid, fpu_error, fpu_result, + fpu_flags); + +`else + assign fpu_error = 1'b0; // Must only be true if fpu_valid + assign fpu_busy = 1'b0; + assign fpu_valid = 1'b0; + assign fpu_result= 32'h00; + assign fpu_flags = 4'h0; +`endif + end else begin + assign fpu_error = 1'b0; + assign fpu_busy = 1'b0; + assign fpu_valid = 1'b0; + assign fpu_result= 32'h00; + assign fpu_flags = 4'h0; + end endgenerate + // }}} + + // Condition handling + // {{{ + assign set_cond = ((op_F[7:4]&op_Fl[3:0])==op_F[3:0]); + initial alu_wF = 1'b0; + initial alu_wR = 1'b0; + generate if (OPT_PIPELINED) + begin + always @(posedge i_clk) + if (i_reset) + begin + alu_wR <= 1'b0; + alu_wF <= 1'b0; + end else if (alu_ce) + begin + // alu_reg <= op_R; + alu_wR <= (op_wR)&&(set_cond)&&(!op_illegal); + alu_wF <= (op_wF)&&(set_cond)&&(!op_illegal); + end else if (!alu_busy) begin + // These are strobe signals, so clear them if not + // set for any particular clock + alu_wR <= (r_halted)&&(OPT_DBGPORT && i_dbg_we && !o_dbg_stall); + alu_wF <= 1'b0; + end + end else begin + + always @(posedge i_clk) + begin + alu_wR <= (op_wR)&&(set_cond)&&(!op_illegal); + alu_wF <= (op_wF)&&(set_cond)&&(!op_illegal); + end + + end endgenerate + // }}} + + // alu_phase + // {{{ + // Instruction phase (which half of the instruction we are on) tracking + generate if (OPT_CIS) + begin : GEN_ALU_PHASE + + reg r_alu_phase; + + initial r_alu_phase = 1'b0; + always @(posedge i_clk) + if ((i_reset)||(clear_pipeline)) + r_alu_phase <= 1'b0; + else if (((adf_ce_unconditional)||(mem_ce))&&(op_valid)) + r_alu_phase <= op_phase; + else if ((adf_ce_unconditional)||(mem_ce)) + r_alu_phase <= 1'b0; + + assign alu_phase = r_alu_phase; + end else begin + + assign alu_phase = 1'b0; + end endgenerate + // }}} + + // alu_reg + // {{{ + generate if (OPT_PIPELINED) + begin + + always @(posedge i_clk) + if (alu_ce || div_ce || o_mem_ce || fpu_ce) + alu_reg <= op_R; + else if (OPT_DBGPORT && i_dbg_we && !o_dbg_stall) + alu_reg <= i_dbg_wreg; + + end else begin + + always @(posedge i_clk) + if (OPT_DBGPORT && i_dbg_we && !o_dbg_stall) + alu_reg <= i_dbg_wreg; + else + alu_reg <= op_R; + end endgenerate + // }}} + + // wr_index + // {{{ + initial wr_index = 0; + always @(posedge i_clk) + begin + if ((OPT_PIPELINED && (mem_ce || adf_ce_unconditional)) + ||(!OPT_PIPELINED && op_valid)) + begin + wr_index <= 0; + /* + if (op_valid_mem) + wr_index <= 3'b001; + if (op_valid_alu) + wr_index <= 3'b010; + if (op_valid_div) + wr_index <= 3'b011; + if (op_valid_fpu) + wr_index <= 3'b100; + */ + + wr_index[0] <= (op_valid_mem | op_valid_div); + wr_index[1] <= (op_valid_alu | op_valid_div); + wr_index[2] <= (op_valid_fpu); + end + + if (OPT_DBGPORT && i_dbg_we && !o_dbg_stall) + wr_index <= 3'b000; + + if (!IMPLEMENT_FPU) + wr_index[2] <= 1'b0; + end + // }}} + + // + // DEBUG Register write access starts here + // + // {{{ + initial dbgv = 1'b0; + always @(posedge i_clk) + if (i_reset || !r_halted) + dbgv <= 0; + else + dbgv <= OPT_DBGPORT && i_dbg_we && !o_dbg_stall; + + always @(posedge i_clk) + if (!OPT_LOWPOWER || (OPT_DBGPORT && i_dbg_we)) + dbg_val <= i_dbg_data; + + // dbg_clear_pipe + // {{{ + always @(posedge i_clk) + if (i_reset || clear_pipeline || !r_halted) + dbg_clear_pipe <= 0; + else if (OPT_DBGPORT && i_dbg_we && !o_dbg_stall) + begin + dbg_clear_pipe <= 1'b0; + + if (!OPT_PIPELINED) + dbg_clear_pipe <= 1'b1; + if ((i_dbg_wreg == op_Bid)&&(op_rB)) + dbg_clear_pipe <= 1'b1; + if (i_dbg_wreg[3:1] == 3'h7) + dbg_clear_pipe <= 1'b1; + end else if ((!OPT_PIPELINED)&&(i_clear_cache && !o_dbg_stall)) + dbg_clear_pipe <= 1'b1; + else + dbg_clear_pipe <= 1'b0; + // }}} + + assign alu_gie = gie; + // }}} + + // r_alu_pc + // {{{ + generate if (OPT_PIPELINED) + begin : GEN_ALU_PC + reg [(AW+1):0] r_alu_pc; + initial r_alu_pc = 0; + always @(posedge i_clk) + if ((adf_ce_unconditional) + ||((master_ce)&&(op_valid_mem) + &&(!clear_pipeline)&&(!mem_stalled))) + r_alu_pc <= op_pc; + assign alu_pc = r_alu_pc; + + end else begin + + assign alu_pc = op_pc; + + end endgenerate + // }}} + + // r_alu_illegal + // {{{ + generate if (OPT_PIPELINED) + begin : SET_ALU_ILLEGAL + reg r_alu_illegal; + + initial r_alu_illegal = 0; + always @(posedge i_clk) + if (clear_pipeline) + r_alu_illegal <= 1'b0; + else if (adf_ce_unconditional) + r_alu_illegal <= op_illegal; + else + r_alu_illegal <= 1'b0; + + assign alu_illegal = (r_alu_illegal); + end else begin : SET_ALU_ILLEGAL + assign alu_illegal = op_illegal; + end endgenerate + // }}} + + // r_alu_pc_valid, mem_pc_valid + // {{{ + initial r_alu_pc_valid = 1'b0; + initial mem_pc_valid = 1'b0; + always @(posedge i_clk) + if (clear_pipeline) + r_alu_pc_valid <= 1'b0; + else if ((adf_ce_unconditional)&&(!op_phase)) + r_alu_pc_valid <= 1'b1; + else if (((!alu_busy)&&(!div_busy)&&(!fpu_busy))||(clear_pipeline)) + r_alu_pc_valid <= 1'b0; + + assign alu_pc_valid = (r_alu_pc_valid) + &&((!alu_busy)&&(!div_busy)&&(!fpu_busy)); + + always @(posedge i_clk) + if (i_reset) + mem_pc_valid <= 1'b0; + else + mem_pc_valid <= (mem_ce); + // }}} + + // Bus lock logic + // {{{ + generate if (OPT_LOCK) + begin : BUSLOCK + reg r_prelock_stall; + reg [1:0] r_bus_lock; + reg [AW+1:0] r_lock_pc; + + // r_prelock_stall + // {{{ + initial r_prelock_stall = 1'b0; + always @(posedge i_clk) + if (!OPT_PIPELINED || clear_pipeline) + r_prelock_stall <= 1'b0; + else if (op_valid && op_lock && r_bus_lock == 2'b00 + && adf_ce_unconditional) + r_prelock_stall <= 1'b1; + else if (op_valid && dcd_valid + && (i_pf_valid || dcd_early_branch)) + r_prelock_stall <= 1'b0; + // }}} + + // r_lock_pc + // {{{ + always @(posedge i_clk) + if (op_valid && op_ce && op_lock) + r_lock_pc <= op_pc-4; +`ifdef FORMAL + always @(posedge i_clk) + cover(op_valid && op_ce && op_lock); +`endif + // }}} + + // r_bus_lock + // {{{ + // Count 3 cycles. The lock will only hold solid for three + // cycles after the LOCK instruction is received. Count those + // cycles here. + initial r_bus_lock = 2'b00; + always @(posedge i_clk) + if (clear_pipeline) + r_bus_lock <= 2'b00; + else if (op_valid && (adf_ce_unconditional||mem_ce)) + begin + if (r_bus_lock != 2'b00) + r_bus_lock <= r_bus_lock - 1; + else if (op_lock) + r_bus_lock <= 2'b11; + end + // }}} + + assign prelock_stall = OPT_PIPELINED && r_prelock_stall; + assign o_bus_lock = |r_bus_lock; + assign o_mem_lock_pc = r_lock_pc; + assign last_lock_insn = (r_bus_lock <= 1); +`ifdef FORMAL + // {{{ + if (OPT_PIPELINED) + begin + (* anyconst *) reg r_nojump_lock; + + always @(*) + if (r_nojump_lock && (r_prelock_stall || r_bus_lock)) + begin + assume(!dcd_early_branch); + assume(!dcd_ljmp); + end + + always @(*) + if (!clear_pipeline) case(r_bus_lock) + 2'b11: if (!prelock_stall && r_nojump_lock) + begin + assert(op_valid && dcd_valid && i_pf_valid); + end + 2'b10: begin + assert(!prelock_stall); + if (r_nojump_lock) + begin + assert(dcd_valid); + assert(op_valid + dcd_valid + i_pf_valid >= 2'b10); + end end + 2'b01: begin + assert(!prelock_stall); + if (r_nojump_lock && !dcd_illegal) + begin + assert(op_valid || dcd_valid + || i_pf_valid); + end end + 2'b00: assert(!prelock_stall); + endcase + end else begin + + always @(*) + assert(!prelock_stall); + end + // }}} +`endif + end else begin + // {{{ + assign prelock_stall = 1'b0; + assign o_bus_lock = 1'b0; + assign o_mem_lock_pc = 0; + assign last_lock_insn= 1; + // }}} + end endgenerate + // }}} + + // Memory interface + // {{{ + // This logic is now managed outside the ZipCore + // + assign o_mem_ce = mem_ce && set_cond; + assign o_mem_op = op_opn[2:0]; + assign o_mem_data = op_Av; + assign o_mem_addr = op_Bv; + assign o_mem_reg = op_R; + + // }}} + + // Sim instructions, alu_sim, alu_sim_immv + // {{{ + generate if (OPT_SIM) + begin : ALU_SIM + reg r_alu_sim; + reg [22:0] r_alu_sim_immv; + wire [4:0] regid; + + assign regid = { (OPT_USERMODE && gie), op_sim_immv[3:0]}; + + if (OPT_USERMODE) + begin + // {{{ + initial r_alu_sim = 1'b0; + always @(posedge i_clk) + begin + if (!i_reset && !clear_pipeline + && adf_ce_unconditional && set_cond + && op_sim && op_valid_alu + &&(!wr_reg_ce || !wr_write_pc + || wr_reg_id[4] != alu_gie)) + begin + // Execute simulation only instructions + // {{{ + if ((op_sim_immv[19:10] == 10'h0)&&(op_sim_immv[8])) + begin // [N/S]EXIT + // {{{ + $finish; + + // if (op_sim_immv[19:4] == 16'h0031) + // Exit(User reg), code cpu_wr_gpreg + // Verilog offers no support for this. + // Veri1ator might, but it isn't + // standard. + // if (op_sim_immv[19:4] == 16'h0030) + // Exit(Normal reg), code cpu_wr_gpreg + // $finish; + // if (op_sim_immv[19:8] == 12'h001) + // Exit(Immediate), code cpu_wr_gpreg + // $finish; + // }}} + end + + if (op_sim_immv[19:0] == 20'h2ff) + begin + // DUMP all registers + // {{{ + if (!op_gie) + begin + $write("sR0 : %08x ", regset[0]); + $write("sR1 : %08x ", regset[1]); + $write("sR2 : %08x ", regset[2]); + $write("sR3 : %08x\n",regset[3]); + + $write("sR4 : %08x ", regset[4]); + $write("sR5 : %08x ", regset[5]); + $write("sR6 : %08x ", regset[6]); + $write("sR7 : %08x\n",regset[7]); + + $write("sR8 : %08x ", regset[8]); + $write("sR9 : %08x ", regset[9]); + $write("sR10: %08x ", regset[10]); + $write("sR11: %08x\n",regset[11]); + + $write("sR12: %08x ", regset[12]); + $write("sSP : %08x ", regset[13]); + $write("sCC : %08x ", w_iflags); + $write("sPC : %08x\n", (!op_gie) ? op_pc : ipc); + $write("\n", (!op_gie) ? op_pc : ipc); + end + + $write("uR0 : %08x ", regset[16]); + $write("uR1 : %08x ", regset[17]); + $write("uR2 : %08x ", regset[18]); + $write("uR3 : %08x\n",regset[19]); + + $write("uR4 : %08x ", regset[20]); + $write("uR5 : %08x ", regset[21]); + $write("uR6 : %08x ", regset[22]); + $write("uR7 : %08x\n",regset[23]); + + $write("uR8 : %08x ", regset[24]); + $write("uR9 : %08x ", regset[25]); + $write("uR10: %08x ", regset[26]); + $write("uR11: %08x\n",regset[27]); + + $write("uR12: %08x ", regset[28]); + $write("uSP : %08x ", regset[29]); + $write("uCC : %08x ", w_uflags); + $write("uPC : %08x\n", (op_gie) ? op_pc : upc); + // }}} + end + + if (op_sim_immv[19:4] == 16'h0020) + begin + // Dump a register + // {{{ + $write("@%08x ", op_pc); + $write("%c", (op_gie) ? "s":"u"); + $write("R[%2d] = 0x", op_sim_immv[3:0]); + // Dump a register + if (wr_reg_ce && wr_reg_id == regid) + $display("%08x", wr_gpreg_vl); + else + $display("%08x", regset[regid]); + // }}} + end + if (op_sim_immv[19:4] == 16'h0021) + begin + // Dump a user register + // {{{ + $write("@%08x u", op_pc); + $write("R[%2d] = 0x", op_sim_immv[3:0]); + if (wr_reg_ce && wr_reg_id == { 1'b1, op_sim_immv[3:0] }) + $display("%08x\n", wr_gpreg_vl); + else + $display("%08x\n", regset[{ 1'b1, + op_sim_immv[3:0]}]); + // }}} + end + if (op_sim_immv[19:4] == 16'h0023) + begin + // SOUT(user register) + // {{{ + if (wr_reg_ce && wr_reg_id == { 1'b1, op_sim_immv[3:0] }) + $write("%c", wr_gpreg_vl[7:0]); + else + $write("%c", regset[{ 1'b1, op_sim_immv[3:0]}][7:0]); + // }}} + end + if (op_sim_immv[19:4] == 16'h0022) + begin + // SOUT(register) + // {{{ + if (wr_reg_ce && wr_reg_id == regid) + $write("%c", wr_gpreg_vl[7:0]); + else + $write("%c", regset[regid][7:0]); + // }}} + end + + if (op_sim_immv[19:8] == 12'h004) + begin + // SOUT(Immediate) + // {{{ + $write("%c", op_sim_immv[7:0]); + // }}} + end + + // ELSE unrecognized SIM instruction + + // Set alu_sim either way + r_alu_sim <= 1'b1; + // }}} + end else + r_alu_sim <= 1'b0; + + if (adf_ce_unconditional) + r_alu_sim_immv <= op_sim_immv; + end + // }}} + end else begin + // {{{ + initial r_alu_sim = 1'b0; + always @(posedge i_clk) + begin + if (!i_reset && !clear_pipeline + && adf_ce_unconditional && set_cond + && op_sim && op_valid_alu + &&(!wr_reg_ce || !wr_write_pc + || wr_reg_id[4] != alu_gie)) + begin + // Execute simulation only instructions + // {{{ + if ((op_sim_immv[19:10] == 10'h0)&&(op_sim_immv[8])) + begin // [N/S]EXIT + // {{{ + $finish; + + // if (op_sim_immv[19:4] == 16'h0031) + // Exit(User reg), code cpu_wr_gpreg + // Verilog offers no support for this. + // Veri1ator might, but it isn't + // standard. + // if (op_sim_immv[19:4] == 16'h0030) + // Exit(Normal reg), code cpu_wr_gpreg + // $finish; + // if (op_sim_immv[19:8] == 12'h001) + // Exit(Immediate), code cpu_wr_gpreg + // $finish; + // }}} + end + + if (op_sim_immv[19:0] == 20'h2ff) + begin + // DUMP all registers + // {{{ + if (!op_gie) + begin + $write(" R0 : %08x ", regset[0]); + $write(" R1 : %08x ", regset[1]); + $write(" R2 : %08x ", regset[2]); + $write(" R3 : %08x\n",regset[3]); + + $write(" R4 : %08x ", regset[4]); + $write(" R5 : %08x ", regset[5]); + $write(" R6 : %08x ", regset[6]); + $write(" R7 : %08x\n",regset[7]); + + $write(" R8 : %08x ", regset[8]); + $write(" R9 : %08x ", regset[9]); + $write(" R10: %08x ", regset[10]); + $write(" R11: %08x\n",regset[11]); + + $write(" R12: %08x ", regset[12]); + $write(" SP : %08x ", regset[13]); + $write(" CC : %08x ", w_iflags); + $write(" PC : %08x\n", op_pc); + end + + // }}} + end + + if (op_sim_immv[19:5] == 15'h0010) + begin + // Dump a register + // {{{ + $write("@%08x ", op_pc); + $write(" R[%2d] = 0x", op_sim_immv[3:0]); + // Dump a register + if (wr_reg_ce&&wr_reg_id[3:0] == regid[3:0]) + $display("%08x", wr_gpreg_vl); + else + $display("%08x", regset[regid[3:0]]); + // }}} + end + if (op_sim_immv[19:5] == 15'h0011) + begin + // SOUT(user register) + // {{{ + if (wr_reg_ce && wr_reg_id[3:0] == op_sim_immv[3:0]) + $write("%c", wr_gpreg_vl[7:0]); + else + $write("%c", regset[op_sim_immv[3:0]][7:0]); + // }}} + end + + if (op_sim_immv[19:8] == 12'h004) + begin + // SOUT(Immediate) + // {{{ + $write("%c", op_sim_immv[7:0]); + // }}} + end + + // ELSE unrecognized SIM instruction + + // Set alu_sim either way + r_alu_sim <= 1'b1; + // }}} + end else + r_alu_sim <= 1'b0; + + if (adf_ce_unconditional) + r_alu_sim_immv <= op_sim_immv; + end + + // Verilator lint_off UNUSED + wire unused_simmv; + assign unused_simmv = &{ 1'b0, regid[4] }; + // Verilator lint_on UNUSED + // }}} + end + + assign alu_sim = r_alu_sim; + assign alu_sim_immv = r_alu_sim_immv; + + end else begin : NO_ALU_SIM + + assign alu_sim = 0; + assign alu_sim_immv = 0; + + end endgenerate + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // + // PIPELINE STAGE #5 :: Write-back results + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // + // This stage is not allowed to stall. If results are ready to be + // written back, they are written back at all cost. Sleepy CPU's + // won't prevent write back, nor debug modes, halting the CPU, nor + // anything else. Indeed, the (master_ce) bit is only as relevant + // as knowinig something is available for writeback. + + // (was) wr_discard, wr_reg_ce + // {{{ + // + // Write back to our generic register set ... + // When shall we write back? On one of two conditions + // Note that the flags needed to be checked before issuing the + // bus instruction, so they don't need to be checked here. + // Further, alu_wR includes (set_cond), so we don't need to + // check for that here either. It also includes alu_illegal, so + // again--doesn't need to be checked again here. + +/* + // 12 inputs. This implementation is made worse by wr_index. + always @(*) + case(wr_index) + 3'b000: wr_reg_ce = dbgv; + 3'b001: wr_reg_ce = i_mem_valid; + //3'b010: wr_reg_ce = (!clear_pipeline)&&(alu_wR && alu_valid); + 3'b011: wr_reg_ce = (!clear_pipeline)&&(div_valid)&&(!div_error); + 3'b1??: wr_reg_ce = (!clear_pipeline)&&(fpu_valid)&&(!fpu_error); + default: wr_reg_ce = (!clear_pipeline)&&(alu_wR && alu_valid); + endcase +*/ + // 7-LUT -- without FPU or wr_index (which wasn't needed) + always @(*) + begin + wr_reg_ce = dbgv || i_mem_valid; + if ((alu_wR && alu_valid) + ||(div_valid && !div_error) + ||(fpu_valid && !fpu_error)) + wr_reg_ce = wr_reg_ce || !clear_pipeline; + end +`ifdef FORMAL + always @(*) + if (!i_reset) + casez(wr_index) + 3'b000: assert(!i_mem_valid && (!alu_wR || !alu_valid) + && (!div_valid || div_error) + && (!fpu_valid || fpu_error)); + 3'b001: assert(!dbgv && (!alu_wR || !alu_valid) + && (!div_valid || div_error) + && (!fpu_valid || fpu_error)); + 3'b010: assert(!dbgv && !i_mem_valid + // && (!alu_wR || !alu_valid) + && (!div_valid || div_error) + && (!fpu_valid || fpu_error)); + 3'b011: assert(!dbgv && !i_mem_valid + && (!alu_wR || !alu_valid) + // && (!div_valid || div_error) + && (!fpu_valid || fpu_error)); + 3'b100: assert(!dbgv && !i_mem_valid + && (!alu_wR || !alu_valid) + && (!div_valid || div_error)); + // && (!fpu_valid || fpu_error); + default: assert(0); + endcase +`endif + // }}} + + // wr_reg_id, wr_write-cc, wr_write_scc, wr_write_ucc, wr_write_pc + // {{{ + // Which register shall be written? + // COULD SIMPLIFY THIS: by adding three bits to these registers, + // One or PC, one for CC, and one for GIE match + // Note that the alu_reg is the register to write on a divide or + // FPU operation. + generate if (OPT_USERMODE) + begin + assign wr_reg_id = (i_mem_valid) ? i_mem_wreg : alu_reg; + end else begin + assign wr_reg_id[3:0] = (i_mem_valid) + ? i_mem_wreg[3:0] : alu_reg[3:0]; + + assign wr_reg_id[4] = 1'b0; + end endgenerate + + // Are we writing to the CC register? + // one 5-LUT ea + assign wr_write_cc = (wr_reg_id[3:0] == CPU_CC_REG); + assign wr_write_scc = (wr_reg_id[4:0] == {1'b0, CPU_CC_REG}); + assign wr_write_ucc = (wr_reg_id[4:0] == {1'b1, CPU_CC_REG}); + // Are we writing to the PC? + assign wr_write_pc = (wr_reg_id[3:0] == CPU_PC_REG); + // }}} + + // wr_?preg_vl: Select from among sources the value to be writtena + // {{{ + // One 6-LUT per bit, or 32 6-LUTs w/o FPU + always @(*) + casez(wr_index) + 3'b000: wr_gpreg_vl = dbg_val; + 3'b001: wr_gpreg_vl = i_mem_result; + // 3'b010: wr_gpreg_vl = alu_result; + 3'b011: wr_gpreg_vl = div_result; + 3'b1??: wr_gpreg_vl = fpu_result; + default: wr_gpreg_vl = alu_result; + endcase + + // One 6-LUT per bit, or 32 6-LUTs + always @(*) + case(wr_index[1:0]) + 2'b00: wr_spreg_vl = dbg_val; + 2'b01: wr_spreg_vl = i_mem_result; + // 3'b010: wr_gpreg_vl = alu_result; + default: wr_spreg_vl = alu_result; + endcase + // }}} + + // Update the register set + // {{{ + generate if (OPT_USERMODE) + begin : SET_REGISTERS + + always @(posedge i_clk) + if (wr_reg_ce) + regset[wr_reg_id] <= wr_gpreg_vl; + + end else begin : SET_SREGISTERS + + always @(posedge i_clk) + if (wr_reg_ce) + regset[wr_reg_id[3:0]] <= wr_gpreg_vl; + + end endgenerate + // }}} + + // + // Write back to the condition codes/flags register ... + + // wr_flags_ce : should condition codes be written to? + // {{{ + // When shall we write to our flags register? alu_wF already + // includes the set condition ... + // assign wr_flags_ce = (alu_wF)&&((alu_valid) + // ||(div_valid)||(fpu_valid)) + // &&(!clear_pipeline)&&(!alu_illegal); +/* + // 10 inputs + always @(*) + begin + wr_flags_ce = 0; + if (alu_wF && !clear_pipeline) // Includes !alu_illegal in wF + case(wr_index) + // 3'b000: wr_flags_ce = 0; // Debug + // 3'b001: wr_flags_ce = 0; // Memory + 3'b010: wr_flags_ce = alu_valid; // ALU + 3'b011: wr_flags_ce = div_valid && !div_error; // DIV + 3'b1??: wr_flags_ce = fpu_valid && !fpu_error; // FPU + default: wr_flags_ce = 0; + endcase + end +*/ + // 7-LUT -- since we don't need wr_index + always @(*) + begin + wr_flags_ce = alu_valid || (div_valid && !div_error) + || (fpu_valid && !fpu_error); + if (!alu_wF || clear_pipeline) + wr_flags_ce = 1'b0; + end +`ifdef FORMAL + always @(*) + if (!i_reset) + begin + casez(wr_index) + 3'b000: assert(wr_flags_ce == 1'b0); + 3'b001: assert(wr_flags_ce == 1'b0); + 3'b010: assert(wr_flags_ce == (alu_wF && !clear_pipeline && alu_valid)); + 3'b011: assert(wr_flags_ce == (alu_wF && !clear_pipeline && div_valid && !div_error)); + 3'b100: assert(IMPLEMENT_FPU && wr_flags_ce == (alu_wF && !clear_pipeline && fpu_valid && !fpu_error)); + default: assert(0); + endcase + + if (alu_illegal) + assert(!div_valid && !fpu_valid + && (!alu_wF || !alu_valid)); + end +`endif + // }}} + + // wr_flags: what should the new condition codes be? + // {{{ + always @(*) + begin + wr_flags = 0; + casez(wr_index) + 3'b010: wr_flags = alu_flags; + 3'b011: wr_flags = div_flags; + 3'b1??: wr_flags = fpu_flags; + default: wr_flags = 0; + endcase + end + // }}} + + // w_uflags, w_iflags : Define the current CC registers + // {{{ + assign w_uflags = { 2'b00, uhalt_phase, ufpu_err_flag, + udiv_err_flag, ubus_err_flag, trap, ill_err_u, + ubreak, !gie && user_step, 1'b1, sleep, + (wr_flags_ce && alu_gie) ? wr_flags : flags }; + assign w_iflags = { 2'b00, ihalt_phase, ifpu_err_flag, + idiv_err_flag, ibus_err_flag, trap, ill_err_i, + break_en, 1'b0, 1'b0, sleep, + (wr_flags_ce && !alu_gie) ? wr_flags : iflags }; + // }}} + + // flags: The user condition codes, Z, C, N, and V + // {{{ + // What value to write? + always @(posedge i_clk) + // If explicitly writing the register itself + if (wr_reg_ce && wr_write_ucc) + flags <= wr_gpreg_vl[3:0]; + // Otherwise if we're setting the flags from an ALU operation + else if (wr_flags_ce && alu_gie) + flags <= wr_flags; + // }}} + + // iflags: The supervisor condition codes, Z, C, N, and V + // {{{ + always @(posedge i_clk) + if (wr_reg_ce && wr_write_scc) + iflags <= wr_gpreg_vl[3:0]; + else if (wr_flags_ce && !alu_gie) + iflags <= wr_flags; + // }}} + + // break_en + // {{{ + // The 'break' enable bit. This bit can only be set from supervisor + // mode. It controls what the CPU does upon encountering a break + // instruction. + // + // The goal, upon encountering a break is that the CPU should stop and + // not execute the break instruction, choosing instead to enter into + // either interrupt mode or halt first. + // if ((break_en) AND (break_instruction)) // user mode or not + // HALT CPU + // else if (break_instruction) // only in user mode + // set an interrupt flag, set the user break bit, + // go to supervisor mode, allow supervisor to step the CPU. + initial break_en = 1'b0; + always @(posedge i_clk) + if (i_reset) + break_en <= 1'b0; + else if ((wr_reg_ce)&&(wr_write_scc)) + break_en <= wr_spreg_vl[CPU_BREAK_BIT]; + // }}} + + // break_pending + // {{{ + generate if (OPT_PIPELINED) + begin : GEN_PENDING_BREAK + reg r_break_pending; + + initial r_break_pending = 1'b0; + always @(posedge i_clk) + if ((clear_pipeline)||(!op_valid)) + r_break_pending <= 1'b0; + else if ((op_break)&&(!r_break_pending)) + r_break_pending <= (!alu_busy)&&(!div_busy) + &&(!fpu_busy)&&(!i_mem_busy) + &&(!wr_reg_ce) && (!step || !stepped); + // else + // r_break_pending <= 1'b0; + + assign break_pending = r_break_pending; + end else begin + + assign break_pending = op_break; + +`ifdef FORMAL + always @(*) + if (!gie && user_step && stepped) + assert(!op_break); +`endif + end endgenerate + // }}} + + // o_break + // {{{ + assign o_break = ((break_en)||(!op_gie))&&(break_pending) + &&(!clear_pipeline) + ||(ill_err_i) + ||((!alu_gie)&&(i_bus_err)) + ||((!alu_gie)&&(div_error)) + ||((!alu_gie)&&(fpu_error)) + ||((!alu_gie)&&(alu_illegal)&&(!clear_pipeline)); + // }}} + + // sleep + // {{{ + // The sleep register. Setting the sleep register causes the CPU to + // sleep until the next interrupt. Setting the sleep register within + // interrupt mode causes the processor to halt until a reset. This is + // a panic/fault halt. The trick is that you cannot be allowed to + // set the sleep bit and switch to supervisor mode in the same + // instruction: users are not allowed to halt the CPU. + initial sleep = 1'b0; + generate if (OPT_USERMODE) + begin : GEN_SLEEP + // {{{ + always @(posedge i_clk) + if ((i_reset)||(w_switch_to_interrupt)) + sleep <= 1'b0; + else if ((wr_reg_ce)&&(wr_write_cc)&&(!alu_gie)) + // In supervisor mode, we have no protections. + // The supervisor can set the sleep bit however + // he wants. Well ... not quite. Switching to + // user mode and sleep mode shouold only be + // possible if the interrupt flag isn't set. + // Thus: if (i_interrupt) + // &&(wr_spreg_vl[GIE]) + // don't set the sleep bit + // otherwise however it would o.w. be set + sleep <= (wr_spreg_vl[CPU_SLEEP_BIT]) + &&((!i_interrupt) + ||(!wr_spreg_vl[CPU_GIE_BIT])); + else if ((wr_reg_ce)&&(wr_write_cc)&&(wr_spreg_vl[CPU_GIE_BIT])) + // In user mode, however, you can only set the + // sleep mode while remaining in user mode. + // You can't switch to sleep mode *and* + // supervisor mode at the same time, lest you + // halt the CPU. + sleep <= wr_spreg_vl[CPU_SLEEP_BIT]; + // }}} + end else begin : GEN_NO_USERMODE_SLEEP + // {{{ + reg r_sleep_is_halt; + initial r_sleep_is_halt = 1'b0; + always @(posedge i_clk) + if (i_reset) + r_sleep_is_halt <= 1'b0; + else if ((wr_reg_ce)&&(wr_write_cc) + &&(wr_spreg_vl[CPU_SLEEP_BIT]) + &&(!wr_spreg_vl[CPU_GIE_BIT])) + r_sleep_is_halt <= 1'b1; + + // Trying to switch to user mode, either via a WAIT or an RTU + // instruction will cause the CPU to sleep until an interrupt, + // in the NO-USERMODE build. + always @(posedge i_clk) + if ((i_reset)||((i_interrupt)&&(!r_sleep_is_halt))) + sleep <= 1'b0; + else if ((wr_reg_ce)&&(wr_write_cc) + &&(wr_spreg_vl[CPU_GIE_BIT])) + sleep <= 1'b1; + // }}} + end endgenerate + // }}} + + // step : debug single-step control + // {{{ + initial user_step = 1'b0; + always @(posedge i_clk) + if (i_reset || !OPT_USERMODE) + user_step <= 1'b0; + else if ((wr_reg_ce)&&(!alu_gie)&&(wr_write_ucc)) + user_step <= wr_spreg_vl[CPU_STEP_BIT]; + + assign step = user_step && gie; + // }}} + + // o_clken + // {{{ + generate if (!OPT_CLKGATE) + begin : NO_CLOCK_GATE + + assign w_clken = 1'b1; + assign o_clken = 1'b1; + + end else begin : GEN_CLOCK_GATE + reg r_clken; + + // Actual clock gating signal + // + // = r_clken || (i_interrupt&&!i_halt) || i_dbg_we + // + initial r_clken = !OPT_START_HALTED; + always @(posedge i_clk) + if (i_reset) + r_clken <= !OPT_START_HALTED; + else if (i_halt && r_halted && (!OPT_DBGPORT || !i_dbg_we)) + r_clken <= i_mem_busy || !i_halt || o_mem_ce; + else if (!i_halt&& (!sleep || i_interrupt || pending_interrupt)) + r_clken <= 1'b1; + else // if (sleep || i_halt) + begin + r_clken <= 1'b0; + + // If we are in the middle of a lock operation, then + // don't shut the clock off + if (o_bus_lock) + r_clken <= 1'b1; + + // If we are in between two compressed instructions + // from the same word, then don't disable the clock + if (alu_phase) + r_clken <= 1'b1; + + // Don't shut the clock off if we are still busy with + // any previous operation(s) + if (i_mem_busy || o_mem_ce + || alu_busy || div_busy || fpu_busy + || wr_reg_ce ||(OPT_DBGPORT && i_dbg_we) + || i_bus_err) + r_clken <= 1'b1; + + if (i_halt && !r_halted) + r_clken <= 1'b1; + + // Should we wait for a valid PF result before halting? + // if (!i_pf_valid) r_clken <= 1'b1; + // if (!op_valid && !dcd_illegal) r_clken <= 1'b1; + // if (!dcd_valid && !i_pf_illegal) r_clken <= 1'b1; + end + + assign w_clken = r_clken; + + // Wake up on interrupts, debug write requests, or the raising + // of the halt flag if we're not sleeping. + assign o_clken = r_clken || (OPT_DBGPORT && i_dbg_we) + || i_clear_cache + || (!i_halt && (i_interrupt || !sleep)); + end endgenerate + // }}} + + // gie, switch_to_interrupt, release_from_interrupt + // {{{ + // The GIE register. Only interrupts can disable the interrupt register + generate if (OPT_USERMODE) + begin : GEN_PENDING_INTERRUPT + // {{{ + reg r_pending_interrupt; + reg r_user_stepped; + + initial r_user_stepped = 1'b0; + always @(posedge i_clk) + if (i_reset || !gie || !user_step) + r_user_stepped <= 1'b0; + // else if(w_switch_to_interrupt) + // r_user_stepped <= 1'b0; + else if (op_valid && !op_phase && !op_lock && last_lock_insn + && (adf_ce_unconditional || mem_ce)) + r_user_stepped <= 1'b1; + + initial r_pending_interrupt = 1'b0; + always @(posedge i_clk) + if (i_reset) + r_pending_interrupt <= 1'b0; + else if (!gie || w_switch_to_interrupt) + r_pending_interrupt <= 1'b0; + else if (clear_pipeline && (!user_step || !stepped)) + r_pending_interrupt <= 1'b0; + else begin + if (i_interrupt) + r_pending_interrupt <= 1'b1; + + if (break_pending) + r_pending_interrupt <= 1'b1; + + if (adf_ce_unconditional && op_illegal) + r_pending_interrupt <= 1'b1; + + if (((!alu_busy && !i_mem_busy && !div_busy && !fpu_busy) + || wr_reg_ce) && user_step && stepped) + r_pending_interrupt <= 1'b1; + end + + assign pending_interrupt = r_pending_interrupt && !i_halt; + + + assign w_switch_to_interrupt = (gie)&&( + // On interrupt (obviously) + ((pending_interrupt) + &&(!alu_phase)&&(!o_bus_lock)&&(!i_mem_busy)) + // + // On division by zero. If the divide isn't + // implemented, div_valid and div_error will be short + // circuited and that logic will be bypassed + ||(div_error) + // + // Same thing on a floating point error. Note that + // fpu_error must *never* be set unless fpu_valid is + // also set as well, else this will fail. + ||(fpu_error) + // + // + ||(i_bus_err) + // + // If we write to the CC register + ||((wr_reg_ce)&&(!wr_spreg_vl[CPU_GIE_BIT]) + &&(wr_reg_id[4])&&(wr_write_cc)) + ); + assign w_release_from_interrupt = (!gie)&&(!i_interrupt) + // Then if we write the sCC register + &&(((wr_reg_ce)&&(wr_spreg_vl[CPU_GIE_BIT]) + &&(wr_write_scc)) + ); + +`ifdef FORMAL + always @(posedge i_clk) + if (r_pending_interrupt && gie && !clear_pipeline) + assert(i_interrupt || user_step || alu_illegal + || ill_err_u || break_pending); + + always @(posedge i_clk) + if (f_past_valid && $past(user_step && stepped && !o_bus_lock + && !o_dbg_stall)) + assert(!gie || r_pending_interrupt); +`endif + assign stepped = r_user_stepped; + // }}} + end else begin : NO_PENDING_INTS + // {{{ + assign w_switch_to_interrupt = 1'b0; + assign w_release_from_interrupt = 1'b0; + assign pending_interrupt = 1'b0; + assign stepped = 1'b0; + + // Verilator lint_off UNUSED + wire unused_int_signals; + assign unused_int_signals = &{ 1'b0, last_lock_insn }; + // Verilator lint_on UNUSED + // }}} + end endgenerate + + generate if (OPT_USERMODE) + begin : SET_GIE + + reg r_gie; + + initial r_gie = 1'b0; + always @(posedge i_clk) + if (i_reset) + r_gie <= 1'b0; + else if (w_switch_to_interrupt) + r_gie <= 1'b0; + else if (w_release_from_interrupt) + r_gie <= 1'b1; + + assign gie = r_gie; + end else begin : ZERO_GIE + + assign gie = 1'b0; + + end endgenerate + // }}} + + // trap, ubreak + // {{{ + generate if (OPT_USERMODE) + begin : SET_TRAP_N_UBREAK + // {{{ + + reg r_trap; + reg r_ubreak; + + // A trap is generated when the user writes to the CC + // register to clear the GIE bit. This is how the supervisor + // can tell that supervisor mode was entered by a request from + // usermode. + initial r_trap = 1'b0; + always @(posedge i_clk) + if ((i_reset)||(w_release_from_interrupt)) + r_trap <= 1'b0; + else if ((alu_gie)&&(wr_reg_ce)&&(!wr_spreg_vl[CPU_GIE_BIT]) + &&(wr_write_ucc)) // &&(wr_reg_id[4]) implied + r_trap <= 1'b1; + else if ((wr_reg_ce)&&(wr_write_ucc)&&(!alu_gie)) + r_trap <= (r_trap)&&(wr_spreg_vl[CPU_TRAP_BIT]); + + // A user break is an indication of an exception. Something + // went wrong. When entering supervisor mode, if this bit + // is set the supervisor then knows something went wrong in + // userspace that needs to be looked into. + initial r_ubreak = 1'b0; + always @(posedge i_clk) + if ((i_reset)||(w_release_from_interrupt)) + r_ubreak <= 1'b0; + else if ((op_gie)&&(break_pending)&&(w_switch_to_interrupt)) + r_ubreak <= 1'b1; + else if (((!alu_gie)||(dbgv))&&(wr_reg_ce)&&(wr_write_ucc)) + r_ubreak <= (ubreak)&&(wr_spreg_vl[CPU_BREAK_BIT]); + + assign trap = r_trap; + assign ubreak = r_ubreak; + // }}} + end else begin + + assign trap = 1'b0; + assign ubreak = 1'b0; + + end endgenerate + // }}} + + // ill_err_i, ill_err_u: Illegal instruction flags + // {{{ + initial ill_err_i = 1'b0; + always @(posedge i_clk) + if (i_reset) + ill_err_i <= 1'b0; + // Only the debug interface can clear this bit + else if ((dbgv)&&(wr_write_scc)) + ill_err_i <= (ill_err_i)&&(wr_spreg_vl[CPU_ILL_BIT]); + else if ((alu_illegal)&&(!alu_gie)&&(!clear_pipeline)) + ill_err_i <= 1'b1; + + generate if (OPT_USERMODE) + begin : SET_USER_ILLEGAL_INSN + + reg r_ill_err_u; + + initial r_ill_err_u = 1'b0; + always @(posedge i_clk) + // The bit is automatically cleared on release from interrupt + // or reset + if ((i_reset)||(w_release_from_interrupt)) + r_ill_err_u <= 1'b0; + // If the supervisor (or debugger) writes to this + // register, clearing the bit, then clear it + else if (((!alu_gie)||(dbgv))&&(wr_reg_ce)&&(wr_write_ucc)) + r_ill_err_u <=((ill_err_u)&&(wr_spreg_vl[CPU_ILL_BIT])); + else if ((alu_illegal)&&(alu_gie)&&(!clear_pipeline)) + r_ill_err_u <= 1'b1; + + assign ill_err_u = r_ill_err_u; + + end else begin : NO_USER_ILL + + assign ill_err_u = 1'b0; + + end endgenerate + // }}} + + // ibus_err_flag, ubus_err_flag : Bus error flags + // {{{ + // Supervisor/interrupt bus error flag -- this will crash the CPU if + // ever set. + initial ibus_err_flag = 1'b0; + always @(posedge i_clk) + if (i_reset) + ibus_err_flag <= 1'b0; + else if ((dbgv)&&(wr_write_scc)) + ibus_err_flag <= (ibus_err_flag)&&(wr_spreg_vl[CPU_BUSERR_BIT]); + else if ((i_bus_err)&&(!alu_gie)) + ibus_err_flag <= 1'b1; + + // User bus error flag -- if ever set, it will cause an interrupt to + // supervisor mode. + generate if (OPT_USERMODE) + begin : SET_USER_BUSERR + + reg r_ubus_err_flag; + + initial r_ubus_err_flag = 1'b0; + always @(posedge i_clk) + if ((i_reset)||(w_release_from_interrupt)) + r_ubus_err_flag <= 1'b0; + else if (((!alu_gie)||(dbgv))&&(wr_reg_ce)&&(wr_write_ucc)) + r_ubus_err_flag <= (ubus_err_flag)&&(wr_spreg_vl[CPU_BUSERR_BIT]); + else if ((i_bus_err)&&(alu_gie)) + r_ubus_err_flag <= 1'b1; + + assign ubus_err_flag = r_ubus_err_flag; + end else begin : NO_USER_BUSERR + + assign ubus_err_flag = 1'b0; + + end endgenerate + // }}} + + // idiv_err_flag, udiv_err_flag : Divide by zero error flags + // {{{ + generate if (OPT_DIV != 0) + begin : DIVERR + // {{{ + reg r_idiv_err_flag; + + // Supervisor/interrupt divide (by zero) error flag -- this will + // crash the CPU if ever set. This bit is thus available for us + // to be able to tell if/why the CPU crashed. + initial r_idiv_err_flag = 1'b0; + always @(posedge i_clk) + if (i_reset) + r_idiv_err_flag <= 1'b0; + else if ((dbgv)&&(wr_write_scc)) + r_idiv_err_flag <= (r_idiv_err_flag)&&(wr_spreg_vl[CPU_DIVERR_BIT]); + else if ((div_error)&&(!alu_gie)) + r_idiv_err_flag <= 1'b1; + + assign idiv_err_flag = r_idiv_err_flag; + + if (OPT_USERMODE) + begin : USER_DIVERR + + reg r_udiv_err_flag; + + // User divide (by zero) error flag -- if ever set, it will + // cause a sudden switch interrupt to supervisor mode. + initial r_udiv_err_flag = 1'b0; + always @(posedge i_clk) + if ((i_reset)||(w_release_from_interrupt)) + r_udiv_err_flag <= 1'b0; + else if (((!alu_gie)||(dbgv))&&(wr_reg_ce) + &&(wr_write_ucc)) + r_udiv_err_flag <= (r_udiv_err_flag)&&(wr_spreg_vl[CPU_DIVERR_BIT]); + else if ((div_error)&&(alu_gie)) + r_udiv_err_flag <= 1'b1; + + assign udiv_err_flag = r_udiv_err_flag; + end else begin : NO_USER_DIVERR + assign udiv_err_flag = 1'b0; + end + // }}} + end else begin + // {{{ + assign idiv_err_flag = 1'b0; + assign udiv_err_flag = 1'b0; + // }}} + end endgenerate + // }}} + + // ifpu_err_flag, ufpu_err_flag : Floating point error flag(s) + // {{{ + generate if (IMPLEMENT_FPU !=0) + begin : FPUERR + // {{{ + // Supervisor/interrupt floating point error flag -- this will + // crash the CPU if ever set. + reg r_ifpu_err_flag, r_ufpu_err_flag; + initial r_ifpu_err_flag = 1'b0; + always @(posedge i_clk) + if (i_reset) + r_ifpu_err_flag <= 1'b0; + else if ((dbgv)&&(wr_write_scc)) + r_ifpu_err_flag <= (r_ifpu_err_flag)&&(wr_spreg_vl[CPU_FPUERR_BIT]); + else if ((fpu_error)&&(fpu_valid)&&(!alu_gie)) + r_ifpu_err_flag <= 1'b1; + // User floating point error flag -- if ever set, it will cause + // a sudden switch interrupt to supervisor mode. + initial r_ufpu_err_flag = 1'b0; + always @(posedge i_clk) + if ((i_reset)&&(w_release_from_interrupt)) + r_ufpu_err_flag <= 1'b0; + else if (((!alu_gie)||(dbgv))&&(wr_reg_ce) + &&(wr_write_ucc)) + r_ufpu_err_flag <= (r_ufpu_err_flag)&&(wr_spreg_vl[CPU_FPUERR_BIT]); + else if ((fpu_error)&&(alu_gie)&&(fpu_valid)) + r_ufpu_err_flag <= 1'b1; + + assign ifpu_err_flag = r_ifpu_err_flag; + assign ufpu_err_flag = r_ufpu_err_flag; + // }}} + end else begin + // {{{ + assign ifpu_err_flag = 1'b0; + assign ufpu_err_flag = 1'b0; + // }}} + end endgenerate + // }}} + + // ihalt_phase, uhalt_phase : If an instruction was broken when halting + // {{{ + generate if (OPT_CIS) + begin : GEN_IHALT_PHASE + reg r_ihalt_phase; + + initial r_ihalt_phase = 0; + always @(posedge i_clk) + if (i_reset) + r_ihalt_phase <= 1'b0; + else if ((!alu_gie)&&(alu_pc_valid)&&(!clear_pipeline)) + r_ihalt_phase <= alu_phase; + + assign ihalt_phase = r_ihalt_phase; + end else begin : GEN_IHALT_PHASE + + assign ihalt_phase = 1'b0; + + end endgenerate + + generate if ((!OPT_CIS) || (!OPT_USERMODE)) + begin : GEN_UHALT_PHASE + + assign uhalt_phase = 1'b0; + + end else begin : GEN_UHALT_PHASE + + reg r_uhalt_phase; + + initial r_uhalt_phase = 1'b0; + always @(posedge i_clk) + if ((i_reset)||(w_release_from_interrupt)) + r_uhalt_phase <= 1'b0; + else if ((alu_gie)&&(alu_pc_valid)) + r_uhalt_phase <= alu_phase; + else if ((!alu_gie)&&(wr_reg_ce)&&(wr_write_pc) + &&(wr_reg_id[4])) + r_uhalt_phase <= wr_spreg_vl[1]; + + assign uhalt_phase = r_uhalt_phase; + + end endgenerate + // }}} + + // ipc, upc: Program counters + // {{{ + // + // Write backs to the PC register, and general increments of it + // We support two: upc and ipc. If the instruction is normal, + // we increment upc, if interrupt level we increment ipc. If + // the instruction writes the PC, we write whichever PC is appropriate. + // + // Do we need to all our partial results from the pipeline? + // What happens when the pipeline has gie and !gie instructions within + // it? Do we clear both? What if a gie instruction tries to clear + // a non-gie instruction? + + // upc + generate if (OPT_USERMODE) + begin : SET_USER_PC + // {{{ + reg [(AW+1):0] r_upc; + + always @(posedge i_clk) + if ((wr_reg_ce)&&(wr_reg_id[4])&&(wr_write_pc)) + r_upc <= { wr_spreg_vl[(AW+1):2], 2'b00 }; + else if ((alu_gie)&& + (((alu_pc_valid)&&(!clear_pipeline)&&(!alu_illegal)) + ||(mem_pc_valid))) + r_upc <= alu_pc; + + assign upc = r_upc; + // }}} + end else begin + // {{{ + assign upc = {(AW+2){1'b0}}; + // }}} + end endgenerate + + // ipc + // {{{ + initial ipc = { RESET_BUS_ADDRESS, 2'b00 }; + always @(posedge i_clk) + if (i_reset) + ipc <= { RESET_BUS_ADDRESS, 2'b00 }; + else if ((wr_reg_ce)&&(!wr_reg_id[4])&&(wr_write_pc)) + ipc <= { wr_spreg_vl[(AW+1):2], 2'b00 }; + else if ((!alu_gie)&&(!alu_phase)&& + (((alu_pc_valid)&&(!clear_pipeline)&&(!alu_illegal)) + ||(mem_pc_valid))) + ipc <= alu_pc; + // }}} + // }}} + + // pf_pc : the program counter used by the pre-fetch + // {{{ + + // pfpcset, pfpcsrc + // {{{ + always @(*) + begin + pfpcset = 0; + pfpcsrc = 0; + if (i_reset) + begin + pfpcsrc = 0; + pfpcset = 1; + end else if ((dbgv)&&(wr_reg_ce)&&(wr_reg_id[4] == gie && wr_write_pc)) + begin + pfpcsrc = 1; // spreg + pfpcset = 1; + end else if ((w_switch_to_interrupt) + ||((!gie)&&((o_clear_icache)||(dbg_clear_pipe)))) + begin + pfpcsrc = 2; // ipc + pfpcset = 1; + end else if ((w_release_from_interrupt)||((gie)&&((o_clear_icache)||(dbg_clear_pipe)))) + begin + pfpcsrc = 3; // upc + pfpcset = 1; + end else if ((wr_reg_ce)&&(wr_reg_id[4] == gie && wr_write_pc)) + begin + pfpcsrc = 1; // spreg + pfpcset = 1; + end else if ((dcd_early_branch_stb)&&(!clear_pipeline)) + begin + pfpcsrc = 4; // branch + pfpcset = 1; + end else if ((new_pc)||(o_pf_ready&& i_pf_valid)) + begin + pfpcsrc = 5; // PC increment + pfpcset = 1; + end + end + // }}} + + initial pf_pc = { RESET_BUS_ADDRESS, 2'b00 }; + always @(posedge i_clk) + if (pfpcset) + case(pfpcsrc) + 3'b000: pf_pc <= { RESET_BUS_ADDRESS, 2'b00 }; + 3'b001: pf_pc <= { wr_spreg_vl[(AW+1):2], 2'b00 }; + 3'b010: pf_pc <= { ipc[(AW+1):2], 2'b00 }; + 3'b011: pf_pc <= { upc[(AW+1):2], 2'b00 }; + 3'b100: pf_pc <= { dcd_branch_pc[AW+1:2] + 1'b1, 2'b00 }; + 3'b101: pf_pc <= { pf_pc[(AW+1):2] + 1'b1, 2'b00 }; + default: pf_pc <= { RESET_BUS_ADDRESS, 2'b00 }; + endcase +/* + if (i_reset) + pf_pc <= { RESET_BUS_ADDRESS, 2'b00 }; + else if ((dbg_clear_pipe)&&(wr_reg_ce)&&(wr_write_pc)) + pf_pc <= { wr_spreg_vl[(AW+1):2], 2'b00 }; + else if ((w_switch_to_interrupt) + ||((!gie)&&((o_clear_icache)||(dbg_clear_pipe)))) + pf_pc <= { ipc[(AW+1):2], 2'b00 }; + else if ((w_release_from_interrupt)||((gie)&&((o_clear_icache)||(dbg_clear_pipe)))) + pf_pc <= { upc[(AW+1):2], 2'b00 }; + else if ((wr_reg_ce)&&(wr_reg_id[4] == gie)&&(wr_write_pc)) + pf_pc <= { wr_spreg_vl[(AW+1):2], 2'b00 }; + else if ((dcd_early_branch_stb)&&(!clear_pipeline)) + pf_pc <= { dcd_branch_pc[AW+1:2] + 1'b1, 2'b00 }; + else if ((new_pc)||((!pf_stalled)&&(i_pf_valid))) + pf_pc <= { pf_pc[(AW+1):2] + 1'b1, 2'b00 }; +*/ + // }}} + + initial last_write_to_cc = 1'b0; + always @(posedge i_clk) + if (i_reset) + last_write_to_cc <= 1'b0; + else + last_write_to_cc <= (wr_reg_ce)&&(wr_write_cc); + assign cc_write_hold = (wr_reg_ce && wr_write_cc)||(last_write_to_cc); + + // o_clear_icache + // {{{ + // If we aren't pipelined, or equivalently if we have no cache, these + // instructions will get quietly (or not so quietly) ignored by the + // optimizer. + initial r_clear_icache = 1'b1; + always @(posedge i_clk) + if (i_reset) + r_clear_icache <= 1'b0; + else if (i_clear_cache && !o_dbg_stall) + r_clear_icache <= 1'b1; + else if ((wr_reg_ce)&&(wr_write_scc)) + r_clear_icache <= wr_spreg_vl[CPU_CLRICACHE_BIT]; + else + r_clear_icache <= 1'b0; + + assign o_clear_icache = r_clear_icache; + // }}} + + // o_clear_dcache + // {{{ + generate if (OPT_DCACHE) + begin : CLEAR_DCACHE + reg r_clear_dcache; + + initial r_clear_dcache = 1'b1; + always @(posedge i_clk) + if (i_reset) + r_clear_dcache <= 1'b0; + else if (i_clear_cache && !o_dbg_stall) + r_clear_dcache <= 1'b1; + else if ((wr_reg_ce)&&(wr_write_scc)) + r_clear_dcache <= wr_spreg_vl[CPU_CLRDCACHE_BIT]; + else + r_clear_dcache <= 1'b0; + + assign o_clear_dcache = r_clear_dcache; + end else begin : NOCLEAR_DCACHE + + assign o_clear_dcache = 1'b0; + + end endgenerate + // }}} + + // new_pc : does the Prefetch need to clear the pipeline and start over? + // {{{ + initial new_pc = 1'b1; + always @(posedge i_clk) + if ((i_reset)||(o_clear_icache)||(dbg_clear_pipe)) + new_pc <= 1'b1; + else if (w_switch_to_interrupt) + new_pc <= 1'b1; + else if (w_release_from_interrupt) + new_pc <= 1'b1; + // else if ((wr_reg_ce)&&(wr_reg_id[4] == gie)&&(wr_write_pc)) + // Can't check for *this* PC here, since a user PC might be + // loaded in the pipeline and hence rewritten. Thus, while + // I hate to do it, we'll need to clear the pipeline on any + // PC write + else if ((wr_reg_ce)&&(alu_gie == wr_reg_id[4])&&(wr_write_pc)) + new_pc <= 1'b1; + else + new_pc <= 1'b0; + // }}} + + // + // The debug write-back interface + // {{{ + + // debug_pc + // {{{ + generate if (OPT_USERMODE) + begin : DBGPC_FULL + // {{{ + always @(*) + begin + debug_pc = 0; + if (i_dbg_rreg[4]) + // User mode + debug_pc[(AW+1):0] + = { upc[(AW+1):2], uhalt_phase, 1'b0 }; + else + // Supervisor mode + debug_pc[(AW+1):0] + = { ipc[(AW+1):2], ihalt_phase, 1'b0 }; + end + // }}} + end else begin : DBGPC_NO_USER + // {{{ + always @(*) + begin + debug_pc = 0; + debug_pc[(AW+1):0] = { ipc[AW+1:2], ihalt_phase, 1'b0 }; + end + // }}} + end endgenerate + // }}} + + // o_dbg_reg + // {{{ + generate if (!OPT_DBGPORT) + begin + + assign o_dbg_reg = 0; + + // verilator lint_off UNUSED + wire unused_dbgport; + assign unused_dbgport = &{ 1'b0, i_dbg_rreg, debug_pc }; + // verilator lint_on UNUSED + + end else if (OPT_USERMODE) + begin : SETDBG + // {{{ + reg [31:0] pre_dbg_reg, r_dbg_reg; + + if (OPT_DISTRIBUTED_REGS) + begin : GEN_DISTRIBUTED_RAM_DBG + // {{{ + always @(*) + pre_dbg_reg = regset[i_dbg_rreg]; + + always @(posedge i_clk) + begin + r_dbg_reg <= pre_dbg_reg; + if (i_dbg_rreg[3:0] == CPU_PC_REG) + r_dbg_reg <= debug_pc; + else if (i_dbg_rreg[3:0] == CPU_CC_REG) + begin + r_dbg_reg[15:0] <= (i_dbg_rreg[4]) + ? w_uflags : w_iflags; + r_dbg_reg[31:23] <= w_cpu_info; + r_dbg_reg[CPU_GIE_BIT] <= gie; + end + end + + assign o_dbg_reg = r_dbg_reg; + // }}} + end else begin : GEN_BKRAM_DBG + // {{{ + reg dbg_reg_sel; + reg [31:0] pre_dbg_special; + + always @(posedge i_clk) + dbg_reg_sel <= (i_dbg_rreg[3:1] == 3'h7); + + always @(posedge i_clk) + pre_dbg_reg <= regset[i_dbg_rreg]; + + always @(posedge i_clk) + if (i_dbg_rreg[0]) + pre_dbg_special <= debug_pc; + else begin + pre_dbg_special[15:0] <= (i_dbg_rreg[4]) + ? w_uflags : w_iflags; + pre_dbg_special[31:23] <= w_cpu_info; + pre_dbg_special[CPU_GIE_BIT] <= gie; + end + + always @(posedge i_clk) + if (dbg_reg_sel) + r_dbg_reg <= pre_dbg_special; + else + r_dbg_reg <= pre_dbg_reg; + + assign o_dbg_reg = r_dbg_reg; + // }}} + end + // }}} + end else begin : NO_USER_SETDBG + // {{{ + reg [31:0] r_dbg_reg; + + always @(posedge i_clk) + begin + r_dbg_reg <= regset[i_dbg_rreg[3:0]]; + if (i_dbg_rreg[3:0] == CPU_PC_REG) + r_dbg_reg <= debug_pc; + else if (i_dbg_rreg[3:0] == CPU_CC_REG) + begin + r_dbg_reg[15:0] <= w_iflags; + r_dbg_reg[31:23] <= w_cpu_info; + r_dbg_reg[CPU_GIE_BIT] <= gie; + end + end + + assign o_dbg_reg = r_dbg_reg; + // }}} + end endgenerate + // }}} + + always @(posedge i_clk) + o_dbg_cc <= { i_bus_err, gie, sleep }; + + // r_halted + // {{{ + generate if (OPT_PIPELINED) + begin + // {{{ + initial r_halted = OPT_START_HALTED; + always @(posedge i_clk) + if (i_reset) + r_halted <= OPT_START_HALTED; + else if (!i_halt) + r_halted <= 1'b0; + else if (r_halted) + r_halted <= 1'b1; + else + r_halted <= (!alu_phase)&&(!o_bus_lock)&&( + // To be halted, any long lasting instruction + // must be completed. + (i_pf_valid)&&(!i_mem_busy)&&(!alu_busy) + &&(!div_busy)&&(!fpu_busy) + // Operations must either be valid, or illegal + &&((dcd_valid)||(dcd_illegal))); + // }}} + end else begin + // {{{ + initial r_halted = OPT_START_HALTED; + always @(posedge i_clk) + if (i_reset) + r_halted <= OPT_START_HALTED; + else if (!i_halt) + r_halted <= 1'b0; + else if (r_halted) + r_halted <= 1'b1; + else + r_halted <= (!alu_phase) + // To be halted, any long lasting instruction + // must be completed. + &&(i_pf_valid)&&(!i_mem_busy)&&(!alu_busy) + &&(!div_busy)&&(!fpu_busy); + // }}} + end endgenerate + // }}} + + // o_dbg_stall + // {{{ + initial r_dbg_stall = 1'b1; + + always @(posedge i_clk) + if (i_reset) + r_dbg_stall <= 1'b1; + else if (!r_halted || (wr_reg_ce && wr_reg_id[3:1] == 3'h7)) + r_dbg_stall <= 1'b1; + else + r_dbg_stall <= (OPT_DBGPORT && i_dbg_we && !o_dbg_stall); + + assign o_dbg_stall = OPT_DBGPORT && (!r_halted || r_dbg_stall); + // }}} + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Accounting outputs + // {{{ + + // + // + // Produce accounting outputs: Account for any CPU stalls, so we can + // later evaluate how well we are doing. + // + // + assign o_op_stall = (master_ce)&&(op_stall); + assign o_pf_stall = (master_ce)&&(!i_pf_valid); + assign o_i_count = (alu_pc_valid)&&(!clear_pipeline); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // The debug scope output + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + generate if (OPT_TRACE_PORT) + begin : GEN_DEBUG_PORT + + reg [31:0] r_debug; + reg debug_trigger, dbg_mem_we; + wire [31:0] debug_flags; + reg [2:0] dbgsrc; + // Verilator lint_off UNUSED + wire [27:0] dbg_pc, dbg_wb_addr; + // Verilator lint_on UNUSED + + + initial debug_trigger = 1'b0; + always @(posedge i_clk) + debug_trigger <= (!i_halt)&&(o_break); + + always @(posedge i_clk) + if (o_mem_ce) + dbg_mem_we <= o_mem_op[0]; + + assign debug_flags = { debug_trigger, 3'b101, + master_ce, i_halt, o_break, sleep, + gie, ibus_err_flag, trap, ill_err_i, + o_clear_icache, i_pf_valid, i_pf_illegal, dcd_ce, + dcd_valid, dcd_stalled, op_ce, op_valid, + op_pipe, alu_ce, alu_busy, alu_wR, + alu_illegal, alu_wF, mem_ce, dbg_mem_we, + i_mem_busy, i_mem_pipe_stalled, (new_pc), (dcd_early_branch) }; + + if (AW-1 < 27) + begin + assign dbg_pc[(AW-1):0] = pf_pc[(AW+1):2]; + assign dbg_pc[27:AW] = 0; + + assign dbg_wb_addr[(AW-1):0] = 0; + assign dbg_wb_addr[27:AW] = 0; + end else // if (AW-1 >= 27) + begin + assign dbg_pc[27:0] = pf_pc[29:2]; + assign dbg_wb_addr = 0; + end + + always @(posedge i_clk) + begin + dbgsrc <= 0; + if ((i_halt)||(!master_ce)||(debug_trigger)||(o_break)) + dbgsrc <= 3'b000; + else if ((i_mem_valid)||((!clear_pipeline)&&(!alu_illegal) + &&(((alu_wR)&&(alu_valid)) + ||(div_valid)||(fpu_valid)))) + dbgsrc <= 3'b001; + else if (clear_pipeline) + dbgsrc <= 3'b010; + else + dbgsrc <= 3'b100; + end + + always @(posedge i_clk) + casez(dbgsrc) + 3'b000: r_debug <= debug_flags; + 3'b001: r_debug <= { debug_trigger, 1'b0, wr_reg_id[3:0], + wr_gpreg_vl[25:0]}; + 3'b010: r_debug <= { debug_trigger, 3'b100, dbg_pc }; + 3'b011: r_debug <= 32'h0; + 3'b1??: r_debug <= debug_flags; + endcase + + assign o_debug = r_debug; + + end else begin : NO_TRACE_PORT + + assign o_debug = 32'h0; + + end endgenerate + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // (Optional) Hardware profiler support + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + generate if (OPT_PROFILER) + begin : GEN_PROFILER + reg prof_stb; + reg [AW+1:0] prof_addr; + reg [31:0] prof_ticks; + + initial prof_stb = 1'b0; + always @(posedge i_clk) + if (i_reset || clear_pipeline) + prof_stb <= 1'b0; + else + prof_stb <= (alu_pc_valid || mem_pc_valid); + + initial prof_addr = 0; + always @(posedge i_clk) + if (i_reset || clear_pipeline) + prof_addr <= RESET_ADDRESS[AW+1:0]; + else if (alu_pc_valid || mem_pc_valid) + prof_addr <= alu_pc; + + initial prof_ticks = 0; + always @(posedge i_clk) + if (i_reset) + prof_ticks <= 0; + else if (!i_halt) + prof_ticks <= prof_ticks + 1; + + assign o_prof_stb = prof_stb; + assign o_prof_addr = prof_addr; + assign o_prof_ticks = prof_ticks; + end else begin + assign o_prof_stb = 1'b0; + assign o_prof_addr = 0; + assign o_prof_ticks = 0; + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // (Optional) Verilator $display simulation dumping support + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // The following is somewhat useful for debugging under Icarus Verilog. + // In that case, the VCD file can tell you what's going on at any + // specific moment, but it doesn't put the register set into the VCD + // file, nor does it give you any insight into the current contents of + // the memory, or how they got that way. The following is intended to + // be a first step towards that end. + // + // As currently envisioned, we track values written to the registers, + // and values read and written to memory. The program counter offered + // is typically off by 4 from the actual instruction address being + // completed. It may be off by more in the case of memory instructions. + // I haven't (yet) tried to make the PC given match the instruction + // address--so that may be an item to do later. + // + // You can activate (or de-activate) these instructions via the + // OPT_SIM_DEBUG flag below. If set to one, the simulation debuggin + // statements will be output as the CPU executes. + localparam OPT_SIM_DEBUG = 1'b0; + generate if (OPT_SIM_DEBUG) + begin : GEN_SIM_DEBUG + + reg [31:0] nstime; + + initial nstime = 0; + always @(posedge i_clk) + nstime <= nstime + 10; + + always @(posedge i_clk) + if (!i_reset && o_mem_ce) + begin + if (o_mem_reg[4] && !o_mem_op[0]) + begin + case(o_mem_op[2:1]) + // 3'b000: + // 3'b001: + 2'b01: $display("MEM: %8d LW uR%1d <- @%08x", nstime, o_mem_reg[3:0], o_mem_addr); + 2'b10: $display("MEM: %8d LH uR%1d <- @%08x", nstime, o_mem_reg[3:0], o_mem_addr); + 2'b11: $display("MEM: %8d LB uR%1d <- @%08x", nstime, o_mem_reg[3:0], o_mem_addr); + default: $display("MEM: %8d Unknown MEM op: %d\n", nstime, o_mem_op); + endcase + end else case(o_mem_op[2:0]) + // 3'b000: + // 3'b001: + 3'b010: $display("MEM: %8d LW sR%1d <- @%08x", nstime, o_mem_reg, o_mem_addr); + 3'b011: $display("MEM: %8d SW 0x%08x -> @%08x", nstime, o_mem_data, o_mem_addr); + 3'b100: $display("MEM: %8d LH sR%1d <- @%08x", nstime, o_mem_reg, o_mem_addr); + 3'b101: $display("MEM: %8d SH 0x%08x -> @%04x", nstime, o_mem_data[15:0], o_mem_addr); + 3'b110: $display("MEM: %8d LB sR%1d <- @%08x", nstime, o_mem_reg, o_mem_addr); + 3'b111: $display("MEM: %8d SB 0x%08x -> @%02x", nstime, o_mem_data[7:0], o_mem_addr); + default: $display("MEM: %8d Unknown MEM op: %d\n", nstime, o_mem_op); + endcase + end + + always @(posedge i_clk) + if (!i_reset && i_bus_err) + begin + $display("MEM: %8d BUS ERROR!!", nstime); + end + + always @(posedge i_clk) + if (!i_reset && wr_reg_ce) + begin + if (i_mem_valid) + begin + if (i_mem_wreg[4]) + $display("MEM: %8d Load 0x%08x -> uR%1d", nstime, i_mem_result, i_mem_wreg[3:0]); + else + $display("MEM: %8d Load 0x%08x -> sR%1d", nstime, i_mem_result, i_mem_wreg[3:0]); + end + + if (wr_reg_id[4] && OPT_USERMODE) + begin + if (wr_reg_id[3:0] == CPU_PC_REG) + $display("REG: %8d uPC <- 0x%08x [0x%08x]", nstime, wr_spreg_vl, (gie) ? upc : ipc); + else if (wr_reg_id[3:0] == CPU_CC_REG) + $display("REG: %8d uCC <- 0x%08x [0x%08x]", nstime, wr_spreg_vl, (gie) ? upc : ipc); + else if (wr_reg_id == 4'hd) + $display("REG: %8d uSP <- 0x%08x [0x%08x]", nstime, wr_gpreg_vl, (gie) ? upc : ipc); + else + $display("REG: %8d uR%1x <- 0x%08x [0x%08x]", nstime, wr_reg_id[3:0], wr_gpreg_vl, (gie) ? upc : ipc); + end else begin + + if (wr_reg_id[3:0] == CPU_PC_REG) + $display("REG: %8d sPC <- 0x%08x [0x%08x]", nstime, wr_spreg_vl, ipc); + else if (wr_reg_id[3:0] == CPU_CC_REG) + $display("REG: %8d sCC <- 0x%08x [0x%08x]", nstime, wr_spreg_vl, ipc); + else if (wr_reg_id[3:0] == 4'hd) + $display("REG: %8d sSP <- 0x%08x [0x%08x]", nstime, wr_gpreg_vl, ipc); + else + $display("REG: %8d sR%1x <- 0x%08x [0x%08x]", nstime, wr_reg_id[3:0], wr_gpreg_vl, ipc); + end + end + end endgenerate + // }}} + + // Make verilator happy + // {{{ + // verilator coverage_off + // verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, fpu_ce, wr_spreg_vl[1:0], + ipc[1:0], upc[1:0], pf_pc[1:0], + dcd_rA, dcd_pipe, dcd_zI, + dcd_A_stall, dcd_B_stall, dcd_F_stall, + op_Rcc, op_pipe, op_lock, i_mem_pipe_stalled, prelock_stall, + dcd_F, w_clken }; + generate if (AW+2 < 32) + begin : UNUSED_AW + wire generic_ignore; + assign generic_ignore = &{ 1'b0, wr_spreg_vl[31:(AW+2)] }; + end if (!OPT_USERMODE) + begin : UNUSED_USERMODE + wire unused_usermode; + assign unused_usermode = &{ 1'b0, alu_reg[4], i_mem_wreg[4], + i_dbg_rreg[4] }; + end endgenerate + // verilator lint_on UNUSED + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL + // Declarations + // {{{ +`ifdef ZIPCPU +`define ASSUME assume +`else +`define ASSUME assert +`endif +`define ASSERT assert +// +// + + wire [1+1+4+15+6+4+13+AW+1+32+4+23-1:0] f_dcd_data; + wire fc_op_prepipe; + wire [6:0] fc_alu_Aid; + wire fc_alu_wR, fc_alu_M, fc_alu_prepipe; + reg f_alu_phase; + + reg f_past_valid; + + reg [2:0] f_dbg_pc_seq, f_dbg_cc_seq, f_dbg_reg_seq; + + wire fc_op_illegal, fc_op_wF, fc_op_ALU, fc_op_M, + fc_op_DV, fc_op_FP, fc_op_break, + fc_op_lock, fc_op_wR, fc_op_rA, fc_op_rB, + fc_op_sim; + wire [6:0] fc_op_Rid, fc_op_Aid, fc_op_Bid; + wire [31:0] fc_op_I; + wire [3:0] fc_op_cond; + wire [3:0] fc_op_op; + wire [22:0] fc_op_sim_immv; + wire f_op_insn; //f_alu_insn,f_wb_insn + reg f_op_phase, f_op_early_branch; + reg f_op_zI; + reg f_op_branch; + + wire [31:0] f_Bv; + reg [31:0] f_Av, f_pre_Bv; + + reg f_alu_branch; + + wire [31:0] f_dcd_mem_addr; + wire [AW-1:0] f_next_mem, f_op_mem_addr; + wire [4+AW+2+7+4-1:0] f_op_data; + + wire fc_alu_illegal, fc_alu_wF, fc_alu_ALU, fc_alu_DV, + fc_alu_FP, fc_alu_break, fc_alu_lock, + fc_alu_rA, fc_alu_rB, fc_alu_sim; + wire [6:0] fc_alu_Rid, fc_alu_Bid; + wire [31:0] fc_alu_I; + wire [3:0] fc_alu_cond; + wire [3:0] fc_alu_op; + wire [22:0] fc_alu_sim_immv; + + wire [F_LGDEPTH-1:0] f_mem_outstanding; + wire f_mem_gie, f_mem_pc, f_read_cycle, + f_exwrite_cycle; + wire [4:0] f_last_reg, f_addr_reg; + + initial f_past_valid = 1'b0; + always @(posedge i_clk) + f_past_valid <= 1'b1; + +`ifndef VERIFIC + initial assume(i_reset); +`endif + always @(posedge i_clk) + if (!f_past_valid) + assume(i_reset); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // The debugging interface + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + fdebug #( + .OPT_START_HALTED(OPT_START_HALTED), + .OPT_DISTRIBUTED_RAM(OPT_DISTRIBUTED_REGS) + ) fdbg ( + // {{{ + .i_clk(i_clk), + .i_reset(i_reset), + .i_cpu_reset(i_reset), + .i_halt(i_halt), + .i_halted(r_halted), + .i_clear_cache(i_clear_cache), + .i_dbg_we(i_dbg_we), + .i_dbg_reg(i_dbg_wreg), + .i_dbg_data(i_dbg_data), + .i_dbg_stall(o_dbg_stall), + .i_dbg_break(o_break), + .i_dbg_cc(o_dbg_cc) + // , .i_dbg_rreg(i_dbg_rreg), + // .i_dbg_rdata(o_dbg_rdata), + // }}} + ); + + always @(*) + if (i_halt && r_halted) + begin + `ASSERT(!alu_ce); + `ASSERT(!alu_phase); + `ASSERT(!div_ce); + `ASSERT(!o_mem_ce); + `ASSERT(f_mem_outstanding == 0); + end + + // o_dbg_reg -- Reading from the debugging interface + generate if (OPT_DISTRIBUTED_REGS) + begin : F_CHK_DISTRIBUTED_RAM + // {{{ + always @(posedge i_clk) + if ((f_past_valid)&&($past(i_halt))&&(!$past(i_dbg_we))) + begin + if ($past(i_dbg_rreg[3:1]) != 3'h7) + `ASSERT(o_dbg_reg == $past(regset[i_dbg_rreg])); + if ($past(i_dbg_rreg[4:0]) == 5'h0f) + `ASSERT(o_dbg_reg[AW+1:0] == $past({ ipc[(AW+1):2], ihalt_phase, 1'b0})); + if ($past(i_dbg_rreg[4:0]) == 5'h1f) + `ASSERT(o_dbg_reg[AW+1:0] == $past({ upc[(AW+1):2], uhalt_phase, 1'b0})); + if ($past(i_dbg_rreg[4:0]) == 5'h0e) + begin + `ASSERT(o_dbg_reg[15:6] == $past(w_iflags[15:6])); + `ASSERT(o_dbg_reg[ 4:0] == $past(w_iflags[ 4:0])); + end + + if ($past(i_dbg_rreg[4:0]) == 5'h1e) + begin + `ASSERT(o_dbg_reg[15:6] == $past(w_uflags[15:6])); + `ASSERT(o_dbg_reg[ 4:0] == $past(w_uflags[ 4:0])); + end + + if ($past(i_dbg_rreg[3:0]) == 4'he) + begin + `ASSERT(o_dbg_reg[15] == 1'b0); + `ASSERT(o_dbg_reg[31:23] == w_cpu_info); + `ASSERT(o_dbg_reg[CPU_GIE_BIT] == $past(gie)); + end + end + // }}} + end else begin : F_CHK_NO_DISTRIBUTED_RAM + // {{{ + always @(posedge i_clk) + if (f_past_valid&&$past(f_past_valid) + && $past(i_halt)&&$past(i_halt,2) + &&(!$past(i_dbg_we))) + begin + if ($past(i_dbg_rreg[3:1],2) != 3'h7) + `ASSERT(o_dbg_reg + == $past(regset[i_dbg_rreg],2)); + end + // }}} + end endgenerate + + initial f_dbg_pc_seq = 0; + always @(posedge i_clk) + if (i_reset) + f_dbg_pc_seq <= 0; + else begin + f_dbg_pc_seq[0] <= i_dbg_we && !o_dbg_stall + && (i_dbg_wreg == { gie, CPU_PC_REG }); + f_dbg_pc_seq[2:1] <= f_dbg_pc_seq[1:0]; + end + + always @(posedge i_clk) + begin + if (f_dbg_pc_seq[0]) + begin + `ASSERT(dbgv && alu_reg == { gie, CPU_PC_REG }); + end + + if (f_dbg_pc_seq[1]) + begin + `ASSERT(clear_pipeline); + `ASSERT(o_pf_request_address == $past({ i_dbg_data[31:2], 2'b00 },2)); + end + end + + initial f_dbg_cc_seq = 0; + always @(posedge i_clk) + if (i_reset) + f_dbg_cc_seq <= 0; + else begin + f_dbg_cc_seq[0] <= i_dbg_we && !o_dbg_stall + && (i_dbg_wreg == { gie, CPU_CC_REG }); + f_dbg_cc_seq[2:1] <= f_dbg_cc_seq[1:0]; + end + + always @(posedge i_clk) + if (f_dbg_cc_seq[0]) + begin + `ASSERT(wr_reg_ce); + `ASSERT(wr_reg_id == $past(i_dbg_wreg)); + `ASSERT(wr_spreg_vl == $past(i_dbg_data)); + end + + initial f_dbg_reg_seq = 0; + always @(posedge i_clk) + if (i_reset) + f_dbg_reg_seq <= 0; + else begin + f_dbg_reg_seq[0] <= i_dbg_we && !o_dbg_stall + && (i_dbg_rreg[3:1] != 3'h7 ); + f_dbg_reg_seq[2:1] <= f_dbg_reg_seq[1:0]; + end + + always @(posedge i_clk) + begin + if (f_dbg_reg_seq[0] && !i_reset) + begin + `ASSERT(dbgv && alu_reg == $past(i_dbg_wreg)); + `ASSERT($past(i_dbg_rreg[3:1]) != 3'h7); + `ASSERT(dbg_val == $past(i_dbg_data)); + + `ASSERT(wr_reg_ce); + `ASSERT(wr_gpreg_vl == $past(i_dbg_data)); + `ASSERT(wr_reg_id == $past(i_dbg_wreg)); + end + + // if (f_dbg_reg_seq[1]) + // begin + // end + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Reset checks + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + + always @(posedge i_clk) + if ((!f_past_valid)||($past(i_reset))) + begin + // Initial assertions + `ASSERT(!i_pf_valid); + `ASSERT(!dcd_phase); + `ASSERT(!op_phase); + `ASSERT(!alu_phase); + // + `ASSERT(!i_pf_valid); + `ASSERT(!dcd_valid); + `ASSERT(!op_valid); + `ASSERT(!op_valid_mem); + `ASSERT(!op_valid_div); + `ASSERT(!op_valid_alu); + `ASSERT(!op_valid_fpu); + // + `ASSERT(!alu_valid); + `ASSERT(!alu_busy); + // + `ASSERT(!i_mem_valid); + `ASSERT(!i_mem_rdbusy); + `ASSERT(!i_bus_err); + // + `ASSERT(!div_valid); + `ASSERT(!div_busy); + `ASSERT(!div_error); + // + `ASSERT(!fpu_valid); + `ASSERT(!fpu_busy); + `ASSERT(!fpu_error); + // + `ASSERT(!ill_err_i); + `ASSERT(!ill_err_u); + `ASSERT(!idiv_err_flag); + `ASSERT(!udiv_err_flag); + `ASSERT(!ibus_err_flag); + `ASSERT(!ubus_err_flag); + `ASSERT(!ifpu_err_flag); + `ASSERT(!ufpu_err_flag); + `ASSERT(!ihalt_phase); + `ASSERT(!uhalt_phase); + end + + always @(*) + begin + if (i_pf_valid) `ASSERT(f_past_valid); + if (dcd_valid) `ASSERT(f_past_valid); + if (alu_pc_valid) `ASSERT(f_past_valid); + if (i_mem_valid) `ASSERT(f_past_valid); + if (div_valid) `ASSERT(f_past_valid); + if (fpu_valid) `ASSERT(f_past_valid); + if (w_op_valid) `ASSERT(f_past_valid); + // if (i_mem_busy) `ASSERT(f_past_valid); + if (i_mem_rdbusy) `ASSERT(f_past_valid); + if (div_busy) `ASSERT(f_past_valid); + if (fpu_busy) `ASSERT(f_past_valid); + end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Pipeline signaling check + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if (clear_pipeline) + begin + // `ASSERT(!alu_ce); + `ASSERT(!mem_ce); + end + + always @(posedge i_clk) + if ((f_past_valid)&&($past(clear_pipeline))) + begin + `ASSERT(!alu_busy); + `ASSERT(!div_busy); + `ASSERT(!i_mem_rdbusy); + `ASSERT(!fpu_busy); + // + `ASSERT(!alu_valid); + `ASSERT(!div_valid); + `ASSERT(!fpu_valid); + end + + always @(*) + if (dcd_ce) + `ASSERT((op_ce)||(!dcd_valid)); + + always @(*) + if ((op_ce)&&(!clear_pipeline)) + `ASSERT((adf_ce_unconditional)||(mem_ce)||(!op_valid)); + + // + // Make sure the dcd stage is never permanently stalled + always @(posedge i_clk) + if ((f_past_valid)&&(!$past(alu_wR))&&(!$past(alu_wF)) + &&($past(f_past_valid,2))&&(!$past(alu_wR,2))&&(!$past(alu_wF)) + &&(!op_valid)&&(master_ce) + &&(!clear_pipeline)&&(!i_reset) + &&(!div_busy)&&(!div_valid) + &&(!i_mem_busy)&&(!i_mem_valid)&&(!i_bus_err) + &&(!alu_busy)&&(!alu_pc_valid)&&(!alu_valid) + &&(!fpu_busy)&&(!fpu_valid)&&(!fpu_error) + &&(!op_break)&&(!o_break) + &&(!w_switch_to_interrupt) + &&(!ibus_err_flag)&&(!ill_err_i)&&(!idiv_err_flag)) + begin + if (OPT_PIPELINED) + `ASSERT(dcd_ce || cc_invalid_for_dcd || i_halt); + if (!dcd_valid) + `ASSERT(dcd_ce); + end + + always @(posedge i_clk) + if (OPT_PIPELINED && !i_halt && !i_reset && (wr_flags_ce + || (wr_reg_ce && wr_reg_id == { op_gie, CPU_CC_REG }))) + `ASSERT(cc_invalid_for_dcd); + + always @(posedge i_clk) + if (!f_past_valid || !OPT_PIPELINED) + begin + `ASSERT(!cc_invalid_for_dcd); + end else if (alu_busy) + begin + `ASSERT(cc_invalid_for_dcd == (alu_wF || alu_reg == { gie, CPU_CC_REG })); + end else if (i_mem_rdbusy || i_bus_err) + begin + `ASSERT(i_bus_err || f_exwrite_cycle + || cc_invalid_for_dcd == (alu_reg == { gie, CPU_CC_REG })); + end else if (!clear_pipeline && cc_invalid_for_dcd) + begin + `ASSERT(alu_illegal || wr_flags_ce + || ((i_mem_valid || i_bus_err) + && ($past(i_mem_rdbusy + && f_last_reg == { gie, CPU_CC_REG }))) + || (wr_flags_ce || (wr_reg_ce && wr_reg_id == { op_gie, CPU_CC_REG })) + || ($past(wr_flags_ce) + || $past(wr_reg_ce && wr_reg_id == { op_gie, CPU_CC_REG }))); + end + + // + // Make sure the ops stage is never permanently stalled + always @(*) + if ((op_valid)&&(master_ce)&&(!clear_pipeline)&&(!i_reset) + &&(!div_busy)&&(!div_valid) + &&(!i_mem_busy)&&(!i_mem_valid)&&(!i_bus_err) + &&(!alu_busy)&&(!alu_pc_valid) + &&(!fpu_busy)&&(!fpu_valid)&&(!fpu_error) + &&(!op_break)&&(!o_break) + &&(!w_switch_to_interrupt) + &&(!alu_illegal) && (!prelock_stall) + &&(!step || !stepped) + &&(!ibus_err_flag)&&(!ill_err_i)&&(!idiv_err_flag)) + `ASSERT(adf_ce_unconditional | mem_ce); + + // always @(posedge i_clk) + // if (f_past_valid&& $past(op_valid && dcd_valid && i_pf_valid + // && !op_ce)) + // `ASSERT(!prelock_stall); + + // + // Make sure that, following an op_ce && op_valid, op_valid is only + // true if dcd_valid was as well + always @(posedge i_clk) + if ((f_past_valid)&&($past(op_ce && op_valid && !dcd_valid))) + begin + if ($past(dcd_early_branch)) + begin + `ASSERT(!dcd_early_branch); + end else + `ASSERT(!op_valid); + end + + // + // Same for the next step + always @(posedge i_clk) + if ((f_past_valid)&&($past(op_valid && (mem_ce ||adf_ce_unconditional))) + &&(!$past(dcd_valid))) + begin + if ($past(dcd_early_branch)) + begin + `ASSERT(!dcd_early_branch); + end else + `ASSERT(!op_valid); + end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Assertions about the Program counter + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + always @(*) + if (i_pf_valid) + `ASSERT(i_pf_instruction_pc[1:0]==2'b00); + + always @(*) + if ((dcd_valid)&&(!dcd_illegal)) + `ASSERT((!dcd_pc[1])||(dcd_phase)); + + always @(*) + `ASSERT(!op_pc[0]); + + always @(*) + `ASSERT(!alu_pc[0]); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Assertions about the prefetch (output) stage + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if ((!clear_pipeline)&&(i_pf_valid)) + `ASSERT(pf_gie == gie); + + always @(*) + if ((i_pf_valid)&&(!clear_pipeline)) + `ASSERT(pf_gie == gie); + + ffetch #(.ADDRESS_WIDTH(ADDRESS_WIDTH), .OPT_CONTRACT(1'b0), + .OPT_ALIGNED(1'b1)) + chkifetch( + .i_clk(i_clk), .i_reset(i_reset), + .cpu_new_pc(o_pf_new_pc), + .cpu_clear_cache(o_clear_icache), + .cpu_pc(o_pf_request_address), .pf_valid(i_pf_valid), + .cpu_ready(o_pf_ready), .pf_pc(i_pf_instruction_pc), + .pf_insn(i_pf_instruction), .pf_illegal(i_pf_illegal)); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Assertions about the decode stage + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + assign f_dcd_data = { + dcd_phase, + dcd_opn, dcd_A, dcd_B, dcd_R, // 4+15 + dcd_Acc, dcd_Bcc, dcd_Apc, dcd_Bpc, dcd_Rcc, dcd_Rpc,//6 + dcd_F, // 4 + dcd_wR, dcd_rA, dcd_rB, + dcd_ALU, dcd_M, dcd_DIV, dcd_FP, + dcd_wF, dcd_gie, dcd_break, dcd_lock, + dcd_pipe, dcd_ljmp, + dcd_pc, // AW+1 + dcd_I, // 32 + dcd_zI, // true if dcd_I == 0 + dcd_illegal, + dcd_early_branch, + dcd_sim, dcd_sim_immv + }; + + always @(posedge i_clk) + if ((f_past_valid)&&(!$past(i_reset))&&(!$past(clear_pipeline)) + &&(!$past(o_clear_icache)) + &&($past(dcd_valid))&&($past(dcd_stalled)) + &&(!clear_pipeline)) + begin + `ASSERT((!OPT_PIPELINED)||(dcd_valid)); + `ASSERT($stable(f_dcd_data)); + `ASSERT($stable(f_dcd_insn_word)); + end + + always @(*) + if ((dcd_valid || dcd_phase)&&(!clear_pipeline)) + `ASSERT(f_dcd_insn_gie == dcd_gie); + + always @(posedge i_clk) + if ((dcd_valid)&&(!dcd_illegal)&&(!clear_pipeline)) + begin + `ASSERT(dcd_gie == gie); + if ((gie)||(dcd_phase)) + begin + `ASSERT((!dcd_wR)||(dcd_R[4]==dcd_gie)); + `ASSERT((!dcd_rA)||(dcd_A[4]==dcd_gie)); + `ASSERT((!dcd_rB)||(dcd_B[4]==dcd_gie)); + end else if ((!dcd_early_branch)&&((dcd_M) + ||(dcd_DIV)||(dcd_FP)||(!dcd_wR))) + `ASSERT(!dcd_gie); + if ((dcd_ALU)&&(dcd_opn==CPU_MOV_OP)) + begin + `ASSERT(((!dcd_rA)&&(dcd_wR)) + ||((!dcd_rA)&&(!dcd_rB)&&(!dcd_wR))); + end else if (dcd_ALU) + `ASSERT( + (gie == dcd_R[4]) + &&(gie == dcd_A[4]) + &&((!dcd_rB)||(gie == dcd_B[4])) + &&(dcd_gie == gie)); + end + + always @(*) + if ((op_valid)&&(op_rA)&&(op_Aid[3:1] == 3'h7)&&(!clear_pipeline) + &&(op_Aid[4:0] != { gie, 4'hf})) + `ASSERT(!pending_sreg_write); + + always @(*) + if ((op_valid)&&(op_rB)&&(op_Bid[3:1] == 3'h7)&&(!clear_pipeline) + &&(op_Bid[4:0] != { gie, 4'hf})) + `ASSERT(!pending_sreg_write); + + + always @(*) + if ((dcd_valid)&&(!clear_pipeline)) + `ASSERT(dcd_gie == gie); + + // + // + // Piped Memory assertions + // + // + always @(*) + if ((dcd_valid)&&(dcd_M)&&(dcd_pipe)&&(!dcd_illegal)&&(!alu_illegal) + &&(!break_pending)&&(!clear_pipeline)) + begin + if (op_valid_mem) + begin + `ASSERT(op_opn[0] == dcd_opn[0]); + `ASSERT((!dcd_rB) + ||(op_Bid[4:0] == dcd_B[4:0])); + `ASSERT(op_rB == dcd_rB); + end + `ASSERT(dcd_B[4] == dcd_gie); + end + + always @(*) + if (op_valid_mem && op_pipe && i_mem_busy) + `ASSERT(f_read_cycle == !op_opn[0]); + + always @(*) + if ((dcd_valid)&&(!dcd_M)) + `ASSERT((dcd_illegal)||(!dcd_pipe)); + + assign f_dcd_mem_addr = w_op_BnI+dcd_I; + + always @(posedge i_clk) + if ((f_past_valid)&&($past(dcd_early_branch))&&(!dcd_early_branch) + &&(dcd_valid)) + `ASSERT(!dcd_pipe); + always @(*) + if ((dcd_valid)&&(dcd_early_branch)) + `ASSERT(!dcd_M); + + always @(*) + if ((dcd_valid)&&(!dcd_illegal)&&(!fc_op_prepipe)) + `ASSERT(!dcd_pipe); + + always @(*) + if ((dcd_valid)&&(dcd_pipe)&&(w_op_valid)) + begin + // `ASSERT((dcd_A[3:1] != 3'h7)||(dcd_opn[0])); + `ASSERT(dcd_B[3:1] != 3'h7); + `ASSERT(dcd_rB); + `ASSERT(dcd_M); + `ASSERT(dcd_B == op_Bid); + if (op_valid) + `ASSERT((op_valid_mem)||(op_illegal)); + + if (op_valid_mem) + begin + `ASSERT((dcd_I[AW+1:3] == 0) + ||(!alu_busy)||(!div_busy) + ||(!alu_wR)||(alu_reg != dcd_B)); + `ASSERT((!op_wR)||(op_Aid != op_Bid)); + end + end + + // + // Decode option processing + // + + // OPT_CIS ... the compressed instruction set + always @(*) + if ((!OPT_CIS)&&(dcd_valid)) + begin + `ASSERT(!dcd_phase); + `ASSERT(dcd_pc[1:0] == 2'b0); + end + + always @(*) + if ((dcd_valid)&&(dcd_phase)) + `ASSERT(f_dcd_insn_word[31]); + + + // OPT_EARLY_BRANCHING + always @(*) + if (!OPT_EARLY_BRANCHING) + `ASSERT((!dcd_early_branch) + &&(!dcd_early_branch_stb) + &&(!dcd_ljmp)); + + // OPT_DIV + always @(*) + if ((dcd_DIV)&&(dcd_valid)&&(!dcd_illegal)) + `ASSERT(dcd_wR); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Assertions about the op stage + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + assign f_op_data = { op_valid_mem, op_valid_alu, + op_valid_div, op_valid_fpu, + // The Av and Bv values can change while we are stalled in the + // op stage--that's why we are stalled there + // r_op_Av, r_op_Bv, // 32 ea + op_pc[AW+1:2], // AW + op_wR, op_wF, + r_op_F, // 7 + op_illegal, op_break, + op_lock, op_pipe + }; + + + always @(posedge i_clk) + if ((f_past_valid)&&($past(op_valid))&&(!$past(i_reset)) + &&(!$past(clear_pipeline))) + begin + if (($past(op_valid_mem))&&($past(mem_stalled))) + `ASSERT($stable(f_op_data[AW+16:1])&&(!$rose(op_pipe))); + if (($past(op_valid_div))&&($past(div_busy))) + `ASSERT($stable(f_op_data)); + end + + f_idecode #( + // {{{ + .OPT_MPY((OPT_MPY!=0)? 1'b1:1'b0), + .OPT_SHIFTS(OPT_SHIFTS), + .OPT_DIVIDE(OPT_DIV), + .OPT_FPU(IMPLEMENT_FPU), + .OPT_LOCK(OPT_LOCK), + .OPT_OPIPE(OPT_PIPELINED_BUS_ACCESS), + .OPT_SIM(1'b0), + .OPT_USERMODE(OPT_USERMODE), + .OPT_LOWPOWER(OPT_LOWPOWER), + .OPT_CIS(OPT_CIS) + // }}} + ) f_insn_decode_op( + // {{{ + f_op_insn_word, f_op_phase, op_gie, + fc_op_illegal, fc_op_Rid, fc_op_Aid, fc_op_Bid, + fc_op_I, fc_op_cond, fc_op_wF, fc_op_op, fc_op_ALU, + fc_op_M, fc_op_DV, fc_op_FP, fc_op_break, fc_op_lock, + fc_op_wR, fc_op_rA, fc_op_rB, fc_op_prepipe, + fc_op_sim, fc_op_sim_immv + // }}} + ); + + initial f_op_early_branch = 1'b0; + always @(posedge i_clk) + if (op_ce && (!OPT_MEMPIPE || dcd_valid || dcd_illegal || dcd_early_branch)) + begin + f_op_insn_word <= f_dcd_insn_word; + f_op_phase <= dcd_phase; + f_op_early_branch <= dcd_early_branch; + f_op_zI <= dcd_zI; + end + + initial f_op_branch = 1'b0; + always @(posedge i_clk) + if ((i_reset)||(clear_pipeline)) + f_op_branch <= 1'b0; + else if (op_ce) + f_op_branch <= (dcd_early_branch)||dcd_ljmp; + else if ((adf_ce_unconditional)||(mem_ce)) + f_op_branch <= 1'b0; + + always @(*) + if (!OPT_EARLY_BRANCHING) + begin + `ASSERT(!f_op_branch); + end else if ((f_op_early_branch)&&(op_valid)) + `ASSERT(f_op_branch); + + + always @(posedge i_clk) + if (op_valid &&(f_op_branch || !fc_op_illegal)&& !clear_pipeline) + begin + // {{{ + if (f_op_branch) + begin + // {{{ + `ASSERT(!op_valid_alu); + `ASSERT(!op_valid_mem); + `ASSERT(!op_valid_div); + `ASSERT(!op_valid_fpu); + `ASSERT(!op_illegal); + `ASSERT(!op_rA); + `ASSERT(!op_rB); + `ASSERT(!op_wR); + `ASSERT(!op_wF); + `ASSERT(op_opn == CPU_MOV_OP); + // }}} + end + + if (op_illegal) + begin + // {{{ + `ASSERT(!op_valid_mem); + `ASSERT(!op_valid_div); + `ASSERT(!op_valid_fpu); + `ASSERT( op_valid_alu); + `ASSERT((!OPT_PIPELINED)||(!op_rA)); + `ASSERT((!OPT_PIPELINED)||(!op_rB)); + `ASSERT(!f_op_branch); + // }}} + end else begin + if (!f_op_branch) + begin + // {{{ + `ASSERT(fc_op_ALU == op_valid_alu); + `ASSERT(fc_op_M == op_valid_mem); + `ASSERT(fc_op_DV == op_valid_div); + `ASSERT(fc_op_FP == op_valid_fpu); + `ASSERT(fc_op_rA == op_rA); + `ASSERT(fc_op_rB == op_rB); + `ASSERT(fc_op_wF == op_wF); + `ASSERT(fc_op_Rid[4:0] == op_R); + `ASSERT(f_op_zI == (fc_op_I == 0)); + `ASSERT(fc_op_wF == op_wF); + `ASSERT(fc_op_lock == op_lock); + if (!OPT_PIPELINED && step && stepped) + begin + `ASSERT(!op_break); + end else begin + `ASSERT(fc_op_break == op_break); + end + `ASSERT(fc_op_I == 0 || !i_mem_rdbusy + || f_exwrite_cycle + || !fc_op_rB + || (fc_op_Bid[4:0] != f_last_reg + && (f_mem_outstanding <= 1) + && (!fc_op_M || !op_pipe)) + || fc_op_Bid[4:0] == f_addr_reg); + if (!i_halt && !i_reset && !f_exwrite_cycle) + `ASSERT((!wr_reg_ce) + ||(wr_reg_id != fc_op_Bid[4:0]) + ||(!op_rB)||(fc_op_I == 0)); + + `ASSERT(fc_op_sim == op_sim); + `ASSERT(fc_op_sim_immv == op_sim_immv); + + case(fc_op_cond[2:0]) + 3'h0: `ASSERT(op_F == 8'h00); // Always + 3'h1: `ASSERT(op_F == 8'h11); // Z + 3'h2: `ASSERT(op_F == 8'h44); // LT + 3'h3: `ASSERT(op_F == 8'h22); // C + 3'h4: `ASSERT(op_F == 8'h88); // V + 3'h5: `ASSERT(op_F == 8'h10); // NE + 3'h6: `ASSERT(op_F == 8'h40); // GE (!N) + 3'h7: `ASSERT(op_F == 8'h20); // NC + endcase + + if ((fc_op_wR)&&(fc_op_Rid[4:0] == { gie, CPU_PC_REG})) + begin + `ASSERT(!op_phase); + end else + `ASSERT(f_op_phase == op_phase); + // }}} + end // Bit order is { (flags_not_used), VNCZ mask, VNCZ value } + `ASSERT((!op_wR)||(fc_op_Rid[4:0] == op_R)); + `ASSERT(((!op_wR)&&(!op_rA))||(fc_op_Aid[4:0] == op_Aid[4:0])); + `ASSERT((!op_rB)||(fc_op_Bid[4:0] == op_Bid)); + // + // if ((!alu_illegal)&&(!ill_err_i)&&(!clear_pipeline)) + + if (f_op_early_branch) + begin + // {{{ + `ASSERT(op_opn == CPU_MOV_OP); + `ASSERT(!op_wR); + `ASSERT(!op_wF); + `ASSERT(f_op_branch); + // }}} + end else begin + // {{{ + `ASSERT(fc_op_op == op_opn); + `ASSERT(fc_op_wR == op_wR); + // }}} + end + end + if (!OPT_PIPELINED_BUS_ACCESS) + `ASSERT((!i_mem_rdbusy)||(i_mem_wreg != fc_op_Bid) + ||(!fc_op_rB)||(fc_op_I == 0)); + // }}} + end else if (op_valid && !clear_pipeline && fc_op_illegal) + begin + // {{{ + `ASSERT(op_illegal); + `ASSERT(op_valid_alu); + `ASSERT(!f_op_branch); + // }}} + end + + always @(*) + if ((op_valid)&&(op_illegal)) + begin + `ASSERT(!op_valid_div); + `ASSERT(!op_valid_fpu); + `ASSERT(!op_valid_mem); + end + +// always @(*) +// if (!op_valid) +// `ASSERT(!op_break); + + always @(*) + if ((!OPT_CIS)&&(op_valid)) + begin + `ASSERT((!op_phase)||(op_illegal)); + `ASSERT(op_pc[1:0] == 2'b0); + end + + always @(*) + if ((!OPT_LOCK)&&(op_valid)) + `ASSERT((!op_lock)||(op_illegal)); + + always @(*) + if (!OPT_EARLY_BRANCHING) + `ASSERT(!f_op_early_branch); + + + always @(*) + if (op_ce) + `ASSERT((dcd_valid)||(dcd_illegal)||(dcd_early_branch)); + + always @(*) + if ((f_past_valid)&&(!f_op_zI)&&(i_mem_rdbusy)&&(op_valid)&&(op_rB)) + `ASSERT((!OPT_DCACHE)||(OPT_MEMPIPE) + ||(i_mem_wreg != op_Bid)); + + always @(posedge i_clk) + if ((op_valid)&&(op_rB)&&(!f_op_zI)&&((i_mem_rdbusy)||(i_mem_valid)) + &&(i_mem_wreg != {gie, CPU_PC_REG})) + begin + if (!OPT_MEMPIPE) + begin + `ASSERT(fc_alu_Aid[4:0] == i_mem_wreg); + `ASSERT(i_mem_wreg != op_Bid); + end + end + + always @(posedge i_clk) + if (i_mem_rdbusy) + begin + `ASSERT(fc_alu_M); + `ASSERT(!OPT_PIPELINED||fc_alu_wR || (OPT_LOCK && f_mem_pc)); + end + + always @(*) + if ((op_valid)&&(!clear_pipeline)) + `ASSERT(op_gie == gie); + + always @(*) + if ((op_valid_alu)&&(!op_illegal)) + begin + if ((op_opn != CPU_SUB_OP) + &&(op_opn != CPU_AND_OP) + &&(op_opn != CPU_MOV_OP)) + begin + `ASSERT(op_wR); + end + if ((op_opn != CPU_MOV_OP)&&(op_opn != CPU_BREV_OP)) + `ASSERT(op_rA); + end + + + always @(posedge i_clk) + if ((op_valid)&&(!op_illegal) + &&(!alu_illegal)&&(!ill_err_i)&&(!clear_pipeline)) + begin + `ASSERT(op_gie == gie); + if ((gie)||(op_phase)) + begin + `ASSERT((!op_wR)||(op_R[4] == gie)); + `ASSERT((!op_rA)||(op_Aid[4] == gie)); + `ASSERT((!op_rB)||(op_Bid[4] == gie)); + end else if (((op_valid_mem) + ||(op_valid_div)||(op_valid_fpu) + ||((op_valid_alu)&&(op_opn!=CPU_MOV_OP)))) + begin + `ASSERT((!op_wR)||(op_R[4] == gie)); + `ASSERT((!op_rA)||(op_Aid[4] == gie)); + `ASSERT((!op_rB)||(op_Bid[4] == gie)); + end + end + + always @(posedge i_clk) + if ((!op_valid)&&(!$past(op_illegal)) + &&(!clear_pipeline)&&(!pending_interrupt)) + `ASSERT(!op_illegal); + + always @(*) + begin + if (alu_ce) + `ASSERT(adf_ce_unconditional); + if (div_ce) + `ASSERT(adf_ce_unconditional); + if (fpu_ce) + `ASSERT(adf_ce_unconditional); + + if ((op_valid)&&(op_illegal)) + `ASSERT(op_valid_alu); + end + + always @(*) + if (mem_ce) + `ASSERT((op_valid)&&(op_valid_mem)&&(!op_illegal)); + + always @(*) + if (div_ce) + `ASSERT(op_valid_div); + + + always @(*) + if ((ibus_err_flag)||(ill_err_i)||(idiv_err_flag)) + begin + `ASSERT(master_stall); + `ASSERT(!mem_ce); + `ASSERT(!alu_ce); + `ASSERT(!div_ce); + `ASSERT(!adf_ce_unconditional); + end + + always @(posedge i_clk) + if ((adf_ce_unconditional)||(mem_ce)) + `ASSERT(op_valid); + + always @(*) + if ((op_valid_alu)&&(!adf_ce_unconditional)&&(!clear_pipeline)) + `ASSERT(!op_ce); + + always @(*) + if ((op_valid_div)&&(!adf_ce_unconditional)) + `ASSERT(!op_ce); + + always @(posedge i_clk) + if (alu_stall) + `ASSERT(!alu_ce); + always @(posedge i_clk) + if (mem_stalled) + `ASSERT(!o_mem_ce); + always @(posedge i_clk) + if (div_busy) + `ASSERT(!div_ce); + + always @(*) + if ((!i_reset)&&(break_pending)&&(!clear_pipeline)) + `ASSERT((op_valid)&&(op_break)); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Memory + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // Op: Memory pipeline assertions + // + + assign f_next_mem = o_mem_addr + 1'b1; + assign f_op_mem_addr = op_Bv[AW+1:2]; + + fmem #( + // {{{ + .OPT_LOCK(OPT_LOCK), + .F_LGDEPTH(F_LGDEPTH), + .OPT_MAXDEPTH((OPT_PIPELINED && OPT_PIPELINED_BUS_ACCESS) + ? 14:1), + .OPT_AXI_LOCK(2) // Let the solver pick + // }}} + ) chkmemops( + // {{{ + .i_clk(i_clk), .i_sys_reset(i_reset), .i_cpu_reset(i_reset), + // + .i_stb(o_mem_ce), + .i_pipe_stalled(i_mem_pipe_stalled), + .i_clear_cache(o_clear_dcache), + .i_lock(o_bus_lock), + .i_op(o_mem_op), .i_data(o_mem_data), .i_addr(o_mem_addr), + .i_oreg(o_mem_reg), .i_areg(op_Bid), + .i_busy(i_mem_busy),.i_rdbusy(i_mem_rdbusy), + .i_valid(i_mem_valid), .i_err(i_bus_err), .i_wreg(i_mem_wreg), + .i_result(i_mem_result), + .f_outstanding(f_mem_outstanding), + .f_pc(f_mem_pc), .f_gie(f_mem_gie), + .f_read_cycle(f_read_cycle), + .f_axi_write_cycle(f_exwrite_cycle), + .f_last_reg(f_last_reg), .f_addr_reg(f_addr_reg) + // }}} + ); + + always @(*) + if ((op_valid)&&(!fc_alu_prepipe)) + `ASSERT((!op_valid_mem)||(!op_pipe)); + + // Validate op_opn and (some of) op_Bid + // {{{ + always @(*) + if ((op_valid_mem)&&(op_pipe)) + begin + // {{{ + if (i_mem_rdbusy) + `ASSERT(op_opn[0] == f_exwrite_cycle); + if (f_mem_outstanding > ((i_bus_err || i_mem_valid) ? 1:0)) + `ASSERT(op_opn[0] != fc_alu_wR); + + // if ((i_mem_busy)&&(!i_mem_rdbusy && !i_mem_valid)) + // `ASSERT(!o_mem_ce || op_opn[0] == 1'b1); + + if (i_mem_rdbusy) + begin + if (OPT_PIPELINED_BUS_ACCESS) + begin end + else if (OPT_DCACHE) + `ASSERT(!i_mem_valid || i_mem_wreg != op_Bid); + end + // }}} + end + // }}} + + always @(posedge i_clk) + if (op_valid_mem && op_pipe) + begin + // {{{ + if ((i_mem_busy)&&(!OPT_DCACHE)) + `ASSERT((f_op_mem_addr == o_mem_addr) + ||(f_op_mem_addr == f_next_mem)); + if (i_mem_valid) + `ASSERT(op_Bid != i_mem_wreg); + + if (alu_busy||alu_valid) + `ASSERT((!alu_wR)||(op_Bid != alu_reg)); + + if (f_past_valid) + begin + if ((i_mem_busy)&&(!OPT_DCACHE)) + `ASSERT((op_Bv[(AW+1):2]==o_mem_addr[(AW-1):0]) + ||(op_Bv[(AW+1):2]==o_mem_addr[(AW-1):0]+1'b1)); + + if ($past(mem_ce)) + `ASSERT(op_Bid == $past(op_Bid)); + end + + `ASSERT(op_Bid[3:1] != 3'h7); + + if ((i_mem_rdbusy||i_mem_valid) && !f_exwrite_cycle) + begin + if (!OPT_MEMPIPE) + begin + // {{{ + `ASSERT(fc_alu_Aid[4:0] == i_mem_wreg); + `ASSERT(i_mem_wreg != op_Bid); + // }}} + end else if (OPT_DCACHE) + begin + // {{{ + `ASSERT(fc_alu_Aid[4:0] != op_Bid); + // }}} + end else // if (!OPT_DCACHE) + begin + // {{{ + if ((i_mem_valid) + ||($past(i_mem_rdbusy))) + `ASSERT(i_mem_wreg != op_Bid); + // }}} + end + end + // }}} + end + + always @(*) + if ((dcd_valid)&&(dcd_pipe)) + `ASSERT((op_Aid[3:1] != 3'h7)||(op_opn[0])); + + always @(*) + if ((op_valid)&(!op_valid_mem)) + `ASSERT((op_illegal)||(!op_pipe)); + + // Check f_addr_reg and f_last_reg + // {{{ + always @(*) + if (OPT_MEMPIPE && (op_valid && op_rB) && !f_exwrite_cycle + &&(!f_op_zI)&&(i_mem_rdbusy || i_mem_valid)) + `ASSERT(f_last_reg != op_Bid); + + always @(*) + if (i_mem_rdbusy && !f_exwrite_cycle) + `ASSERT(f_last_reg == alu_reg); + + always @(*) + if ((op_valid_mem)&&(op_pipe) && i_mem_valid) + `ASSERT(op_Bid != i_mem_wreg); + + always @(*) + if ((op_valid_mem)&&(op_pipe) && (i_mem_rdbusy || i_mem_valid)) + `ASSERT(op_Bid == f_addr_reg); + + always @(*) + if (i_mem_rdbusy) + begin + if (op_valid_mem && op_pipe) + begin + `ASSERT(op_Bid == f_addr_reg); + `ASSERT(op_Bid != f_last_reg); + if (dcd_M && dcd_pipe && !dcd_illegal + && !dcd_early_branch) + `ASSERT(op_Bid == dcd_B[4:0]); + end else if (!op_valid_mem && dcd_M && dcd_pipe + && !dcd_illegal && !dcd_early_branch) + `ASSERT(dcd_B[4:0] == f_addr_reg); + end + // }}} + + always @(*) + if (op_valid && !f_op_zI && i_mem_rdbusy && (f_last_reg != { gie, CPU_PC_REG })) + `ASSERT(!op_rB || fc_op_Bid[4:0] == f_addr_reg + ||((f_mem_outstanding == 1) && (fc_op_Bid[4:0] != f_last_reg))); + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Assertions about the ALU stage + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(*) + if ((alu_ce)&&(!clear_pipeline)) + `ASSERT((op_valid_alu)&&(op_gie == gie)); + always @(*) + if ((mem_ce)&&(!clear_pipeline)) + `ASSERT((op_valid_mem)&&(op_gie == gie)); + always @(*) + if ((div_ce)&&(!clear_pipeline)) + `ASSERT((op_valid_div)&&(op_gie == gie)); + + always @(*) + if ((!clear_pipeline)&&((i_mem_valid)||(div_valid)||(div_busy) + ||(i_mem_rdbusy)||(alu_valid))) + `ASSERT(alu_gie == gie); + always @(*) + if ((!OPT_CIS)&&(alu_pc_valid)) + `ASSERT(alu_pc[1:0] == 2'b0); + always @(*) + if (!OPT_LOCK) + `ASSERT((!o_bus_lock)&&(!prelock_stall)); + always @(*) + if (!OPT_DIV) + `ASSERT((!dcd_DIV)&&(!op_valid_div)&&(!div_busy)&&(!div_valid)&&(!div_ce)); + always @(*) + if (OPT_MPY == 0) + `ASSERT(alu_busy == 1'b0); + + + always @(*) + if (!clear_pipeline) + begin + if ((alu_valid)||(alu_illegal)) + `ASSERT(alu_gie == gie); + if (div_valid) + `ASSERT(alu_gie == gie); + end + + always @(*) + if (alu_busy) + begin + `ASSERT(!i_mem_rdbusy); + `ASSERT(!div_busy); + `ASSERT(!fpu_busy); + end else if (i_mem_rdbusy) + begin + `ASSERT(!div_busy); + `ASSERT(!fpu_busy); + end else if (div_busy) + `ASSERT(!fpu_busy); + + always @(posedge i_clk) + if ((div_valid)||(div_busy)) + `ASSERT(alu_reg[3:1] != 3'h7); + + always @(posedge i_clk) + if ((f_past_valid)&&(wr_reg_ce) + &&((!$past(r_halted))||(!$past(i_dbg_we)))) + `ASSERT(alu_gie == gie); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Contract checking : A operand + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(*) + begin + f_Av = regset[fc_op_Aid[4:0]]; + if (fc_op_Aid[3:0] == CPU_PC_REG) + begin + if ((wr_reg_ce)&&(wr_reg_id == fc_op_Aid[4:0])) + f_Av = wr_spreg_vl; + else if (fc_op_Aid[4] == op_gie) + f_Av = op_pc; // f_next_addr; + else if (fc_op_Aid[3:0] == { 1'b1, CPU_PC_REG }) + begin + f_Av[31:(AW+1)] = 0; + f_Av[(AW+1):0] = { upc, uhalt_phase, 1'b0 }; + end + end else if (fc_op_Aid[4:0] == { 1'b0, CPU_CC_REG }) + begin + f_Av = { w_cpu_info, regset[fc_op_Aid[4:0]][22:16], w_iflags }; + if ((wr_reg_ce)&&(wr_reg_id == fc_op_Aid[4:0])) + f_Av[22:16] = wr_spreg_vl[22:16]; + end else if (fc_op_Aid[4:0] == { 1'b1, CPU_CC_REG }) + begin + f_Av = { w_cpu_info, regset[fc_op_Aid[4:0]][22:16], w_uflags }; + if ((wr_reg_ce)&&(wr_reg_id == fc_op_Aid[4:0])) + f_Av[22:16] = wr_spreg_vl[22:16]; + end else if ((wr_reg_ce)&&(wr_reg_id == fc_op_Aid[4:0])) + f_Av = wr_gpreg_vl; + else + f_Av = regset[fc_op_Aid[4:0]]; + end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Contract checking : B operand + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // The PRE-logic + always @(*) + begin + f_pre_Bv = regset[fc_op_Bid[4:0]]; + // + if (fc_op_Bid[3:0] == CPU_PC_REG) + begin + // Can always read your own address + if (fc_op_Bid[4] == op_gie) + f_pre_Bv = { {(30-AW){1'b0}}, op_pc[(AW+1):2], 2'b00 }; // f_next_addr; + else // if (fc_op_Bid[4]) + // Supervisor or user may read the users PC reg + begin + f_pre_Bv = 0; + f_pre_Bv[(AW+1):0] = { upc[(AW+1):2], uhalt_phase, 1'b0 }; + if ((wr_reg_ce)&&(wr_reg_id == fc_op_Bid[4:0])) + f_pre_Bv = wr_spreg_vl; + end + end else if (fc_op_Bid[3:0] == CPU_CC_REG) + begin + f_pre_Bv = { w_cpu_info, regset[fc_op_Bid[4:0]][22:16], + w_uflags }; + // if ((fc_op_Bid[4] == op_gie)&&(!fc_op_Bid[4])) + f_pre_Bv[15:0] = (gie || fc_op_Bid[4]) ? w_uflags : w_iflags; + + if ((wr_reg_ce)&&(wr_reg_id == fc_op_Bid[4:0])) + f_pre_Bv[22:16] = wr_spreg_vl[22:16]; + + end else if ((wr_reg_ce)&&(wr_reg_id == fc_op_Bid[4:0])) + f_pre_Bv = wr_gpreg_vl; + else + f_pre_Bv = regset[fc_op_Bid[4:0]]; + end + + + // The actual calculation of B + assign f_Bv = (fc_op_rB) + ? ((fc_op_Bid[5]) + ? ( { f_pre_Bv }+{ fc_op_I[29:0],2'b00 }) + : (f_pre_Bv + fc_op_I)) + : fc_op_I; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // CONTRACT: The operands to an ALU/MEM/DIV operation must be valid. + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + always @(posedge i_clk) + if (!i_reset && op_valid && !op_illegal + && (!clear_pipeline && !dbg_clear_pipe) + && (!i_halt || !dbgv) + &&((!wr_reg_ce)||(wr_reg_id!= { gie, CPU_PC_REG })) + &&(!f_op_branch)) + begin + if ((fc_op_rA)&&(fc_op_Aid[3:1] != 3'h7)) + `ASSERT(f_Av == op_Av); + + if (!fc_op_rB || fc_op_Bid[4:0] != { gie, CPU_CC_REG } + || !OPT_PIPELINED) + begin + `ASSERT(f_Bv == op_Bv); + end else if (f_op_zI) + `ASSERT(((f_Bv ^ op_Bv) & 32'hffff_c0ff) == 0); + end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Assertions about the ALU stage + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // alu_valid + // alu_ce + // alu_stall + // ALU stage assertions + + always @(posedge i_clk) + if ((alu_ce)||(mem_ce)||(div_ce)||(fpu_ce)) + begin + f_alu_insn_word <= f_op_insn_word; + f_alu_phase <= f_op_phase; + end + + initial f_alu_branch = 1'b0; + always @(posedge i_clk) + if ((adf_ce_unconditional)||(mem_ce)) + f_alu_branch <= f_op_branch; + else + f_alu_branch <= 1'b0; + + f_idecode #( + // {{{ + .OPT_MPY((OPT_MPY!=0)? 1'b1:1'b0), + .OPT_SHIFTS(OPT_SHIFTS), + .OPT_DIVIDE(OPT_DIV), + .OPT_FPU(IMPLEMENT_FPU), + .OPT_LOCK(OPT_LOCK), + .OPT_OPIPE(OPT_PIPELINED_BUS_ACCESS), + .OPT_SIM(1'b0), + .OPT_USERMODE(OPT_USERMODE), + .OPT_LOWPOWER(OPT_LOWPOWER), + .OPT_CIS(OPT_CIS) + // }}} + ) f_insn_decode_alu( + // {{{ + f_alu_insn_word, f_alu_phase, alu_gie, + fc_alu_illegal, fc_alu_Rid, fc_alu_Aid, fc_alu_Bid, + fc_alu_I, fc_alu_cond, fc_alu_wF, fc_alu_op, fc_alu_ALU, + fc_alu_M, fc_alu_DV, fc_alu_FP, fc_alu_break, + fc_alu_lock, fc_alu_wR, fc_alu_rA, fc_alu_rB, + fc_alu_prepipe, fc_alu_sim, fc_alu_sim_immv + // }}} + ); + + always @(posedge i_clk) + if (!wr_reg_ce) + begin + if (f_alu_branch) + begin + `ASSERT((!div_valid)&&(!div_busy)); + `ASSERT((!fpu_valid)&&(!fpu_busy)); + `ASSERT(!i_mem_rdbusy); + `ASSERT(!alu_busy); + end else begin + `ASSERT(fc_alu_sim == alu_sim); + `ASSERT(fc_alu_sim_immv == alu_sim_immv); + + if (!fc_alu_DV) + `ASSERT((!div_valid)&&(!div_busy)&&(!div_error)); + if (!fc_alu_M) + `ASSERT(!i_mem_rdbusy); + if (!fc_alu_ALU) + `ASSERT(!alu_busy); + if (!fc_alu_FP) + `ASSERT((!fpu_busy)&&(!fpu_error)); + if (alu_busy) + `ASSERT((fc_alu_op[3:1] == 3'h5) + ||(fc_alu_op[3:0] == 4'hc)); + if ((alu_busy)||(div_busy)||(fpu_busy)) + begin + `ASSERT(!i_mem_rdbusy); + `ASSERT((clear_pipeline) + ||(fc_alu_Rid[4:0] == alu_reg)); + if (alu_busy) + `ASSERT(fc_alu_wF == alu_wF); + if ((fc_alu_Rid[3:1] == 3'h7)&&(alu_wR) + &&(fc_alu_Rid[4:0] != { gie, 4'hf })) + `ASSERT(pending_sreg_write); + end else if (i_mem_rdbusy) + begin + if ($past(i_mem_rdbusy)) + `ASSERT(fc_alu_Rid[4] == f_mem_gie); + end + + //if ((div_busy)||(fpu_busy)) + // `ASSERT(alu_wR); + //else + if ((alu_busy)&&(alu_wR)) + `ASSERT(fc_alu_wR); + + if (alu_busy || i_mem_rdbusy || div_busy) + begin + if ((fc_alu_wR)&&(fc_alu_Rid[4:0] == { gie, CPU_PC_REG})) + begin + `ASSERT(!alu_phase); + end else + `ASSERT(f_alu_phase == alu_phase); + end + end + + end else if (!dbgv) // && wr_reg_ce + begin + `ASSERT(fc_alu_DV || (!div_valid)&&(!div_error)); + `ASSERT(fc_alu_ALU|| !alu_valid); + `ASSERT(fc_alu_M || !i_mem_valid); + `ASSERT(fc_alu_FP || (!fpu_valid)&&(!fpu_error)); + `ASSERT((!alu_busy)&&(!div_busy)&&(!fpu_busy)); + + if ((!alu_illegal)&&(fc_alu_cond[3])&&(fc_alu_wR)&&(fc_alu_ALU)) + `ASSERT(alu_wR); + if (!i_mem_valid) + `ASSERT(fc_alu_Rid[4:0] == alu_reg); + if (f_exwrite_cycle) + begin + `ASSERT(!alu_wR); + end else + `ASSERT((!alu_wR)||(fc_alu_wR == alu_wR)); + if (alu_valid) + `ASSERT(fc_alu_wF == alu_wF); + if (!fc_alu_wF) + `ASSERT(!wr_flags_ce); + + `ASSERT(!f_alu_branch); + end + + always @(posedge i_clk) + if (f_mem_pc && i_mem_rdbusy) + begin + // {{{ + `ASSERT(!OPT_PIPELINED || cc_invalid_for_dcd || !fc_alu_wR + || fc_alu_Rid[4:0] != { gie, CPU_CC_REG }); + `ASSERT(!mem_ce); + + if (OPT_PIPELINED && fc_alu_Rid[4:0] != { gie, CPU_PC_REG } + && (!OPT_LOCK || fc_alu_wR)) + `ASSERT(pending_sreg_write); + if ((!OPT_DCACHE)||(!OPT_MEMPIPE)) + begin + `ASSERT(!fc_alu_prepipe); + end else if ((i_mem_rdbusy && !f_exwrite_cycle) + &&(!$past(mem_ce))&&(!$past(mem_ce,2))) + `ASSERT(!fc_alu_prepipe); + // }}} + end else if (i_mem_rdbusy) + begin + `ASSERT(!pending_sreg_write); + // assert(!cc_invalid_for_dcd); + end + + // Ongoing memory operation check + // {{{ + always @(posedge i_clk) + if (i_mem_rdbusy) + begin + // In pipelined mode, this is an ongoing load operation + // Otherwise, mem_rdbusy == i_mem_busy and we have no idea + // what type of operation we are in + `ASSERT(!fc_alu_illegal); + `ASSERT(fc_alu_M); + `ASSERT(gie == alu_reg[4]); + if (fc_alu_rB) + `ASSERT(fc_alu_Bid[4:0] == f_addr_reg); + + if (!f_exwrite_cycle) + begin + `ASSERT(alu_reg == f_last_reg); + if (alu_reg == f_addr_reg) + `ASSERT(!op_pipe); + end + if (fc_alu_cond[3]) + `ASSERT(fc_alu_Rid[4:0] == alu_reg); + + if ((fc_alu_wR)&&(fc_alu_Rid[4:0] == { gie, CPU_PC_REG})) + begin + `ASSERT(!alu_phase); + end else + `ASSERT(f_alu_phase == alu_phase); + + if (f_exwrite_cycle) + begin + `ASSERT(!fc_alu_wR); + end else + `ASSERT(fc_alu_wR); + end else if ((i_mem_busy)&&(fc_alu_M) + &&(f_mem_outstanding > ((i_mem_valid || i_bus_err) ? 1:0)) + &&(!i_bus_err)) + begin // Ongoing store operation + // {{{ + `ASSERT(!fc_alu_illegal); + `ASSERT(fc_alu_M); + `ASSERT(!fc_alu_wR); + // }}} + end + // }}} + + always @(*) + if (!fc_alu_wR && i_mem_rdbusy) + begin + `ASSERT(OPT_LOCK); + `ASSERT(f_mem_outstanding == 1); + `ASSERT(f_mem_pc); + `ASSERT(!f_read_cycle); + `ASSERT(!cc_invalid_for_dcd); + `ASSERT(f_exwrite_cycle); + // `ASSERT(i_mem_wreg[3:0] == 4'hf); + `ASSERT(fc_alu_M); + `ASSERT(!pending_sreg_write); + `ASSERT(!alu_wR); + end else if (i_mem_rdbusy) + begin + `ASSERT(fc_alu_wR); + `ASSERT(!f_exwrite_cycle); + `ASSERT(f_read_cycle); + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Assertions about the writeback stage + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if ((f_past_valid)&&($past(i_reset))&&($past(gie) != gie)) + `ASSERT(clear_pipeline); + + always @(*) + if (!IMPLEMENT_FPU) + begin + `ASSERT(!ifpu_err_flag); + `ASSERT(!ufpu_err_flag); + end + + always @(posedge i_clk) + if (dbgv) + begin + `ASSERT(wr_index == 3'h0); + `ASSERT(wr_gpreg_vl == $past(i_dbg_data)); + `ASSERT(wr_spreg_vl == $past(i_dbg_data)); + end + + always @(*) + if (i_mem_rdbusy || i_mem_valid) + begin + `ASSERT(wr_index == 3'h1); + if (i_mem_valid) + `ASSERT(wr_gpreg_vl == i_mem_result); + end + + always @(*) + if (alu_busy || alu_valid) + begin + `ASSERT(wr_index == 3'h2); + if (alu_valid) + begin + `ASSERT(wr_gpreg_vl == alu_result); + `ASSERT(wr_spreg_vl == alu_result); + end + end + + always @(*) + if (div_busy || div_valid || div_error) + begin + `ASSERT(wr_index == 3'h3); + if (div_valid) + `ASSERT(wr_gpreg_vl == div_result); + end + + always @(*) + if (fpu_busy || fpu_valid || fpu_error) + begin + `ASSERT(wr_index == 3'h4); + if (fpu_valid) + `ASSERT(wr_gpreg_vl == fpu_result); + end + + always @(posedge i_clk) + if ((f_past_valid)&&(r_halted)) + begin + `ASSERT(!div_busy); + `ASSERT(!i_mem_rdbusy); + `ASSERT(!alu_busy); + `ASSERT(!div_valid); + `ASSERT(!i_mem_valid); + `ASSERT(!alu_valid); + end + + always @(*) + if (((wr_reg_ce)||(wr_flags_ce))&&(!dbgv)) + `ASSERT(!alu_illegal); + + always @(*) + if (!fc_alu_wR && (!OPT_LOCK || !f_mem_pc)) + `ASSERT(!i_mem_rdbusy); + + always @(*) + if (alu_illegal) + `ASSERT(!i_mem_rdbusy); + + always @(posedge i_clk) + if (wr_reg_ce && !$past(i_dbg_we)) + begin + if (!fc_alu_wR) + `ASSERT(OPT_LOCK && f_exwrite_cycle); + + // Since writes are asynchronous, they can create errors later + `ASSERT((!i_bus_err)||(!i_mem_valid)); + `ASSERT(!fpu_error); + `ASSERT(!div_error); + end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Clock enable checking + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if (!OPT_CLKGATE) + begin + assert(w_clken); + assert(o_clken); + end else if (!f_past_valid) + begin end // clken can be anything on the first clock + else if ($past(i_reset)) + begin + assert(w_clken != OPT_START_HALTED); + end else if ($past((!i_halt || !r_halted) && (!sleep || i_interrupt))) + begin + assert(w_clken); + end else if ($past((i_mem_busy && f_mem_outstanding > 0) || o_mem_ce)) + begin + assert(w_clken); + end else if (i_mem_busy || div_busy || alu_busy || fpu_busy) + begin + assert(o_clken); + end else if ($past((i_interrupt || pending_interrupt) && !i_halt)) + begin + assert(o_clken); + end else if (!sleep && !r_halted) + begin + assert(o_clken); + end + + // always @(posedge i_clk) + // if (f_past_valid && i_mem_busy) + // assert(!r_halted); + + always @(posedge i_clk) + if (!OPT_CLKGATE) + begin + assert(o_clken); + end else if (!f_past_valid) + begin + end else if (i_dbg_we || i_clear_cache || f_mem_outstanding > 0) + begin + assert(o_clken); + end else if (!i_halt && (i_interrupt || !sleep)) + assert(o_clken); + + + always @(posedge i_clk) + if (!i_reset && sleep) + begin + assert(!wr_reg_ce || dbgv); + assert(!i_mem_rdbusy); + end + + always @(posedge i_clk) + if (!i_reset && r_halted) + assert(!i_mem_rdbusy); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Low power checking + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // In low power mode, *nothing* should change unless we are doing + // something. + + generate if (OPT_LOWPOWER && OPT_PIPELINED) + begin : F_OPT_LOWPOWER_CHECK + wire [70:0] op_state; + + // + // OP* registers can only be expected to hold steady if + // pipelined. If not pipelined, many of these registers + // become aliases for the decode registers which will + // (of course) change from one clock to the next. + // + assign op_state = { op_valid_mem, op_valid_div, op_valid_alu, + op_valid_fpu, + op_opn, op_R, op_Rcc, op_Aid, + op_Bid, op_pc, + r_op_break, op_illegal }; + + always @(posedge i_clk) + if (f_past_valid && !$past(i_reset) && !$past(clear_pipeline) + && ($past(op_valid && !adf_ce_unconditional && !mem_ce) + ||($past(!op_valid && !dcd_ljmp) && !op_valid))) + begin + assert($stable(op_state)); + end + + always @(posedge i_clk) + if (f_past_valid && !op_valid) + begin + assert(r_op_F == 7'h00); + assert(!op_wR); + assert(!op_wF); + assert(!op_rA); + assert(!op_rB); + + assert(r_op_Av == 0); + assert(r_op_Bv == 0); + end + + end endgenerate + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Step properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if (f_past_valid && !i_reset && $past(!gie)) + assert(!stepped); + + always @(posedge i_clk) + if (f_past_valid && step && stepped) + begin + assert(!break_pending); + assert(!o_bus_lock); + end + + always @(posedge i_clk) + if (f_past_valid && !i_reset && !o_bus_lock && !$past(o_bus_lock) + && !$past(o_dbg_stall) && !o_dbg_stall) + begin + if (step && !stepped) + begin + `ASSERT(!i_mem_rdbusy); + `ASSERT(!div_busy); + `ASSERT(!alu_busy); + `ASSERT(!fpu_busy); + end + + if (wr_reg_ce || wr_flags_ce) + `ASSERT(!step || stepped); + end + + always @(posedge i_clk) + if (!i_reset &&(i_mem_rdbusy|| div_busy|| alu_busy || fpu_busy)) + assert(!r_halted); + + always @(posedge i_clk) + if (f_past_valid && !$past(i_reset) && i_mem_busy) + assert(!$rose(r_halted)); + + always @(posedge i_clk) + if (step && stepped) + begin + assert(!adf_ce_unconditional); + assert(!div_ce); + assert(!mem_ce); + assert(!fpu_ce); + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Ad-hoc (unsorted) properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if ((f_past_valid)&&(!$past(i_reset))&&($past(i_mem_rdbusy)) + &&(!$past(i_mem_valid) + ||($past(i_mem_wreg[3:1] != 3'h7)))) + `ASSERT(!i_mem_valid || i_mem_wreg[4] == alu_gie); + + always @(posedge i_clk) + if (i_mem_valid) + `ASSERT(i_mem_wreg[4] == alu_gie); + + always @(posedge i_clk) + if (i_mem_rdbusy) + `ASSERT(alu_gie == f_mem_gie); + + always @(posedge i_clk) + if (f_mem_outstanding > 0) + `ASSERT(alu_gie == f_mem_gie); + + + + // Break instructions are not allowed to move past the op stage + always @(*) + if ((break_pending)||(op_break)) + `ASSERT((!alu_ce)&&(!mem_ce)&&(!div_ce)&&(!fpu_ce)); + + always @(*) + if (op_break) + `ASSERT((!alu_ce)&&(!mem_ce)&&(!div_ce)&&(!fpu_ce)); + + always @(posedge i_clk) + if ((f_past_valid)&&(!$past(i_reset)) + &&($past(break_pending))&&(!break_pending)) + `ASSERT((clear_pipeline)||($past(clear_pipeline))); + + always @(*) + if ((o_break)||((alu_valid)&&(alu_illegal))) + begin + `ASSERT(!alu_ce); + `ASSERT(!mem_ce); + `ASSERT(!div_ce); + `ASSERT(!fpu_ce); + `ASSERT(!i_mem_rdbusy); + // The following two shouldn't be true, but will be true + // following a bus error + if (!i_bus_err) + begin + `ASSERT(!alu_busy); + `ASSERT(!div_busy); + `ASSERT(!fpu_busy); + end + end + + always @(posedge i_clk) + if ((f_past_valid)&&(!$past(i_reset))&&(!$past(clear_pipeline))&& + ($past(div_busy))&&(!clear_pipeline)) + begin + `ASSERT($stable(alu_reg)); + `ASSERT(alu_reg[4] == alu_gie); + `ASSERT($stable(alu_pc)); + `ASSERT($stable(alu_phase)); + end + + always @(posedge i_clk) + if ((f_past_valid)&&(!$past(i_reset))&&(!i_reset) + &&(!$past(clear_pipeline))&&(!clear_pipeline) + &&(($past(div_busy))||($past(i_mem_rdbusy)))) + `ASSERT($stable(alu_gie)); + + always @(posedge i_clk) + if (i_mem_rdbusy && f_mem_outstanding > 0) + `ASSERT(!new_pc); + + always @(posedge i_clk) + if ((wr_reg_ce)&&((wr_write_cc)||(wr_write_pc))) + `ASSERT(wr_spreg_vl == wr_gpreg_vl); + + always @(posedge i_clk) + if ((f_past_valid)&&(!$past(clear_pipeline))&&(!$past(i_reset)) + &&($past(op_valid))&&($past(op_illegal))&&(!op_illegal)) + `ASSERT(alu_illegal); + + always @(*) + if ((OPT_PIPELINED)&&(alu_valid)&&(alu_wR)&&(!clear_pipeline) + &&(alu_reg[3:1] == 3'h7) + &&(alu_reg[4:0] != { gie, CPU_PC_REG })) + `ASSERT(pending_sreg_write); + + always @(posedge i_clk) + if ((OPT_PIPELINED)&&(i_mem_valid || i_mem_rdbusy) + &&(f_last_reg[3:1] == 3'h7) + &&(!f_exwrite_cycle + && f_last_reg[4:0] != { gie, CPU_PC_REG })) + begin + `ASSERT(pending_sreg_write); + end else if ((OPT_PIPELINED)&&(OPT_DCACHE) + && i_mem_valid + &&($past(i_mem_rdbusy)) + &&($past(i_mem_rdbusy,2))) + `ASSERT((i_mem_wreg[3:1] != 3'h7) + ||f_exwrite_cycle + ||(i_mem_wreg == { gie, CPU_PC_REG}) + ||(pending_sreg_write)); + + always @(*) + if ((op_valid_alu)||(op_valid_div)||(op_valid_mem)||(op_valid_fpu)) + `ASSERT(op_valid); + + always @(*) + if (!OPT_PIPELINED) + begin + if (op_valid) + begin + `ASSERT(!dcd_valid); + `ASSERT(!i_mem_busy || f_mem_outstanding == 0); + `ASSERT(!alu_busy); + `ASSERT(!div_busy); + `ASSERT((!wr_reg_ce)||(dbgv)); + `ASSERT(!wr_flags_ce); + end + end + + always @(posedge i_clk) + if ((!OPT_PIPELINED)&&(f_past_valid)) + begin + if (op_valid) + `ASSERT($stable(f_dcd_insn_word)); + end + + always @(posedge i_clk) + if ((alu_ce)||(div_ce)||(fpu_ce)) + `ASSERT(adf_ce_unconditional); + + always @(posedge i_clk) + if ((!clear_pipeline)&&(master_ce)&&(op_ce)&&(op_valid)) + begin + if (op_valid_mem) + begin + `ASSERT((mem_ce)||(!set_cond)); + end else begin + `ASSERT(!master_stall); + if ((set_cond)&&(op_valid_div)) + `ASSERT(div_ce||pending_sreg_write); + if (!op_valid_alu) + `ASSERT(!alu_ce); + end + end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Cover statements + // {{{ + //////////////////////////////////////////////////////////////////////// + // + always @(posedge i_clk) + begin + cover(!i_reset); + cover(!i_halt); + cover(!i_reset && !i_halt); + end + + always @(posedge i_clk) + if (!i_halt && !i_reset) + begin + cover(i_pf_valid && i_pf_illegal); + cover(i_pf_valid && !i_pf_illegal); + + cover(dcd_valid && dcd_illegal); + cover(dcd_valid && !dcd_illegal); + + cover(op_valid && !op_illegal); + cover(op_valid && op_illegal); + cover(op_valid && !op_illegal && op_ce); + cover(op_valid && op_illegal && op_ce); + + cover(alu_valid); + cover(div_valid); + cover(o_mem_ce); + cover(i_mem_valid); + + cover(wr_reg_ce); + + cover(gie); + end + + always @(posedge i_clk) + if (!i_halt && !i_reset && !$past(i_reset)) + begin + cover(i_interrupt && !alu_phase && !o_bus_lock); + cover(alu_illegal); + cover( gie && w_switch_to_interrupt); + cover(!gie && w_release_from_interrupt); + + // Cover an illegal instruction + cover(alu_illegal); + cover(alu_illegal && !clear_pipeline); + + + // Cover a break instruction + cover((master_ce)&&(break_pending)&&(!break_en)); + cover(o_break); +// if (master_ce && (break_en || break_pending)) +// `ASSERT(!wr_reg_ce); + + + cover(gie); + cover(step); + cover(step && w_switch_to_interrupt); + cover(step && w_switch_to_interrupt && !i_interrupt); + end + + always @(posedge i_clk) + if (!i_halt && !i_reset && !$past(i_reset)) + begin + cover(step); + cover(step && wr_reg_ce); + cover($fell(step) && $stable(!i_reset && !i_halt)); + cover($past(step && !i_reset && !i_halt)); + cover($past(step && !i_reset && !i_halt,2)); + cover(!o_bus_lock && alu_ce && step); + cover(user_step && !gie); + cover(user_step && gie); + cover(user_step && gie && wr_reg_ce); + + // Cover a division by zero + cover(!OPT_DIV || div_busy); + cover(!OPT_DIV || div_error); + cover(!OPT_DIV || div_valid); + end + + generate if (OPT_DIV) + begin : F_DIVIDE + always @(posedge i_clk) + if (!i_halt && !i_reset && !$past(i_reset)) + begin + // Cover a division by zero + cover(div_busy); + cover(div_error); + cover(div_valid); + end + end endgenerate + + generate if (OPT_LOCK) + begin : F_CVR_LOCK + + always @(posedge i_clk) + if (f_past_valid && !i_reset + && !$past(i_reset || clear_pipeline)) + begin + cover($rose(o_bus_lock)); + cover($fell(o_bus_lock)); + cover($fell(o_bus_lock) + && !$past(i_bus_err || div_error || alu_illegal)); + end + + end else begin + + always @(*) + `ASSERT(!o_bus_lock); + + end endgenerate + + always @(posedge i_clk) + if (!i_halt && !i_reset && !$past(i_reset) && wr_reg_ce) + begin + `ASSERT(!alu_illegal); + end + + always @(posedge i_clk) + if (!i_halt && !i_reset && wr_reg_ce) + begin + cover(o_bus_lock); + + // Cover the switch to interrupt + cover(i_interrupt); + cover(i_interrupt && !alu_phase); + cover(i_interrupt && !o_bus_lock); + cover((i_interrupt)&&(!alu_phase)&&(!o_bus_lock)); + + // Cover a "step" instruction + // + cover(((alu_pc_valid)||(mem_pc_valid)) + &&(step)&&(!alu_phase)&&(!o_bus_lock)); + // `ASSERT(!(((alu_pc_valid)||(mem_pc_valid)) + // &&(step)&&(!alu_phase)&&(!o_bus_lock))); + + // Cover a bus error + cover(i_bus_err); + + // Cover a TRAP instruction to the CC register + cover(!alu_gie && !wr_spreg_vl[CPU_GIE_BIT] + &&(wr_reg_id[4])&&(wr_write_cc)); + + // Cover an AXI lock return branch + cover(i_mem_valid && f_exwrite_cycle && !f_read_cycle); + end + + // Cover all the various reasons to switch to an interrupt + // {{{ + always @(posedge i_clk) + if ((f_past_valid && !i_reset && !$past(i_reset) && !i_halt) && gie) + begin + cover((pending_interrupt) + &&(!alu_phase)&&(!o_bus_lock)&&(!i_mem_busy)); + + cover(div_error); + // cover(fpu_error); + cover(i_bus_err); + cover(wr_reg_ce && !wr_spreg_vl[CPU_GIE_BIT] + && wr_reg_id[4] && wr_write_cc); + + if (!clear_pipeline && !w_switch_to_interrupt) + begin + cover(pending_interrupt); + + cover(i_interrupt); + + cover(mem_ce && step); + cover(break_pending && !adf_ce_unconditional); + cover(adf_ce_unconditional && op_illegal); + cover(adf_ce_unconditional && step); + end + + `ASSERT(!adf_ce_unconditional || !break_pending); + end + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Careless assumptions + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // Once asserted, an interrupt will stay asserted while the CPU is + // in user mode + always @(posedge i_clk) + if ((f_past_valid)&&($past(i_interrupt && (gie || !i_mem_busy)))) + assume(i_interrupt); + // }}} +`endif // FORMAL +// }}} +endmodule diff --git a/rtl/cpu/zipcounter.v b/rtl/cpu/zipcounter.v index 5fa12b8..0452734 100644 --- a/rtl/cpu/zipcounter.v +++ b/rtl/cpu/zipcounter.v @@ -1,14 +1,14 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zipcounter.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: // A very, _very_ simple counter. It's purpose doesn't really // include rollover, but it will interrupt on rollover. It can be set, // although my design concept is that it can be reset. It cannot be -// halted. It will always produce interrupts--whether or not they are +// halted. It will always produce interrupts--whether or not they are // handled interrupts is another question--that's up to the interrupt // controller. // @@ -17,7 +17,7 @@ // each task by resetting the counter at the beginning of every task // interval, and reading the result at the end of the interval. As long // as the interval is less than 2^32 clocks, there should be no problem. -// Similarly, this can be used to measure CPU wishbone bus stalls, +// Similarly, this can be used to measure CPU wishbone bus stalls, // prefetch stalls, or other CPU stalls (i.e. stalling as part of a JMP // instruction, or a read from the condition codes following a write). // @@ -26,11 +26,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -43,60 +43,81 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module zipcounter(i_clk, i_reset, i_event, - i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, - o_wb_stall, o_wb_ack, o_wb_data, - o_int); - parameter BW = 32; - input wire i_clk, i_reset, i_event; - // Wishbone inputs - input wire i_wb_cyc, i_wb_stb, i_wb_we; - input wire [(BW-1):0] i_wb_data; - // Wishbone outputs - output wire o_wb_stall; - output reg o_wb_ack; - output reg [(BW-1):0] o_wb_data; - // Interrupt line - output reg o_int; +// }}} +module zipcounter #( + // {{{ + parameter BW = 32 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, i_event, + // Wishbone inputs + input wire i_wb_cyc, i_wb_stb, i_wb_we, + input wire [(BW-1):0] i_wb_data, + // Wishbone outputs + output wire o_wb_stall, + output reg o_wb_ack, + output reg [(BW-1):0] o_wb_data, + // Interrupt line + output reg o_int + // }}} + ); + // o_int, o_wb_data + // {{{ initial o_int = 0; initial o_wb_data = 32'h00; always @(posedge i_clk) - if (i_reset) - { o_int, o_wb_data } <= 0; - else if ((i_wb_stb)&&(i_wb_we)) - { o_int, o_wb_data } <= { 1'b0, i_wb_data }; - else if (i_event) - { o_int, o_wb_data } <= o_wb_data+{{(BW-1){1'b0}},1'b1}; - else - o_int <= 1'b0; + if (i_reset) + { o_int, o_wb_data } <= 0; + else if ((i_wb_stb)&&(i_wb_we)) + { o_int, o_wb_data } <= { 1'b0, i_wb_data }; + else if (i_event) + { o_int, o_wb_data } <= o_wb_data+{{(BW-1){1'b0}},1'b1}; + else + o_int <= 1'b0; + // }}} + // o_wb_ack + // {{{ initial o_wb_ack = 1'b0; always @(posedge i_clk) if (i_reset) o_wb_ack <= 1'b0; else o_wb_ack <= i_wb_stb; + // }}} assign o_wb_stall = 1'b0; // Make verilator happy + // {{{ + // verilator coverage_off // verilator lint_off UNUSED wire unused; - assign unused = i_wb_cyc; + assign unused = &{ 1'b0, i_wb_cyc }; // verilator lint_on UNUSED - + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // Formal properties for this module are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/zipcpu.v b/rtl/cpu/zipcpu.v deleted file mode 100644 index b470acd..0000000 --- a/rtl/cpu/zipcpu.v +++ /dev/null @@ -1,2749 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// -// Filename: zipcpu.v -// -// Project: Zip CPU -- a small, lightweight, RISC CPU soft core -//{{{ -// Purpose: This is the top level module holding the core of the Zip CPU -// together. The Zip CPU is designed to be as simple as possible. -// (actual implementation aside ...) The instruction set is about as -// RISC as you can get, with only 26 instruction types currently supported. -// (There are still 8-instruction Op-Codes reserved for floating point, -// and 5 which can be used for transactions not requiring registers.) -// Please see the accompanying spec.pdf file for a description of these -// instructions. -// -// All instructions are 32-bits wide. All bus accesses, both address and -// data, are 32-bits over a wishbone bus. -// -// The Zip CPU is fully pipelined with the following pipeline stages: -// -// 1. Prefetch, returns the instruction from memory. -// -// 2. Instruction Decode -// -// 3. Read Operands -// -// 4. Apply Instruction -// -// 4. Write-back Results -// -// Further information about the inner workings of this CPU, such as -// what causes pipeline stalls, may be found in the spec.pdf file. (The -// documentation within this file had become out of date and out of sync -// with the spec.pdf, so look to the spec.pdf for accurate and up to date -// information.) -// -// -// In general, the pipelining is controlled by three pieces of logic -// per stage: _ce, _stall, and _valid. _valid means that the stage -// holds a valid instruction. _ce means that the instruction from the -// previous stage is to move into this one, and _stall means that the -// instruction from the previous stage may not move into this one. -// The difference between these control signals allows individual stages -// to propagate instructions independently. In general, the logic works -// as: -// -// -// assign (n)_ce = (n-1)_valid && (!(n)_stall) -// -// -// always @(posedge i_clk) -// if ((i_reset)||(clear_pipeline)) -// (n)_valid = 0 -// else if (n)_ce -// (n)_valid = 1 -// else if (n+1)_ce -// (n)_valid = 0 -// -// assign (n)_stall = ( (n-1)_valid && ( pipeline hazard detection ) ) -// || ( (n)_valid && (n+1)_stall ); -// -// and ... -// -// always @(posedge i_clk) -// if (n)_ce -// (n)_variable = ... whatever logic for this stage -// -// Note that a stage can stall even if no instruction is loaded into -// it. -// -//}}} -// Creator: Dan Gisselquist, Ph.D. -// Gisselquist Technology, LLC -// -//////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -//{{{ -// This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published -// by the Free Software Foundation, either version 3 of the License, or (at -// your option) any later version. -// -// This program is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program. (It's in the $(ROOT)/doc directory. Run make with no -// target there if the PDF file isn't present.) If not, see -// for a copy. -//}}} -// License: GPL, v3, as defined and found on www.gnu.org, -// http://www.gnu.org/licenses/gpl.html -// -// -//////////////////////////////////////////////////////////////////////////////// -// -// -`default_nettype none -// -`define CPU_SUB_OP 4'h0 // also a compare instruction -`define CPU_AND_OP 4'h1 // also a test instruction -`define CPU_BREV_OP 4'h8 -`define CPU_MOV_OP 4'hd -// -`define CPU_CC_REG 4'he -`define CPU_PC_REG 4'hf -`define CPU_CLRDCACHE_BIT 15 // Set to clear the D-cache,automatically clears -`define CPU_CLRICACHE_BIT 14 // Set to clear the I-cache,automatically clears -`define CPU_PHASE_BIT 13 // Set if we are executing the latter half of a CIS -`define CPU_FPUERR_BIT 12 // Floating point error flag, set on error -`define CPU_DIVERR_BIT 11 // Divide error flag, set on divide by zero -`define CPU_BUSERR_BIT 10 // Bus error flag, set on error -`define CPU_TRAP_BIT 9 // User TRAP has taken place -`define CPU_ILL_BIT 8 // Illegal instruction -`define CPU_BREAK_BIT 7 -`define CPU_STEP_BIT 6 // Will step one (or two CIS) instructions -`define CPU_GIE_BIT 5 -`define CPU_SLEEP_BIT 4 -// Compile time defines -// -`include "cpudefs.v" -// -// -module zipcpu(i_clk, i_reset, i_interrupt, - // Debug interface - i_halt, i_clear_pf_cache, i_dbg_reg, i_dbg_we, i_dbg_data, - o_dbg_stall, o_dbg_reg, o_dbg_cc, - o_break, - // CPU interface to the wishbone bus - o_wb_gbl_cyc, o_wb_gbl_stb, - o_wb_lcl_cyc, o_wb_lcl_stb, - o_wb_we, o_wb_addr, o_wb_data, o_wb_sel, - i_wb_stall, i_wb_ack, i_wb_data, - i_wb_err, - // Accounting/CPU usage interface - o_op_stall, o_pf_stall, o_i_count -`ifdef DEBUG_SCOPE - , o_debug // , o_dcache_debug -`endif - ); - // Parameters - //{{{ - parameter [31:0] RESET_ADDRESS=32'h010_0000; - parameter ADDRESS_WIDTH=30, - LGICACHE=12; -`ifdef OPT_MULTIPLY - parameter IMPLEMENT_MPY = `OPT_MULTIPLY; -`else - parameter IMPLEMENT_MPY = 0; -`endif -`ifdef OPT_DIVIDE - parameter [0:0] IMPLEMENT_DIVIDE = 1; -`else - parameter [0:0] IMPLEMENT_DIVIDE = 0; -`endif -`ifdef OPT_IMPLEMENT_FPU - parameter [0:0] IMPLEMENT_FPU = 1; -`else - parameter [0:0] IMPLEMENT_FPU = 0; -`endif -`ifdef OPT_EARLY_BRANCHING - parameter [0:0] EARLY_BRANCHING = 1; -`else - parameter [0:0] EARLY_BRANCHING = 0; -`endif -`ifdef OPT_CIS - parameter [0:0] OPT_CIS = 1'b1; -`else - parameter [0:0] OPT_CIS = 1'b0; -`endif -`ifdef OPT_NO_USERMODE - localparam [0:0] OPT_NO_USERMODE = 1'b1; -`else - localparam [0:0] OPT_NO_USERMODE = 1'b0; -`endif -`ifdef OPT_PIPELINED - parameter [0:0] OPT_PIPELINED = 1'b1; -`else - parameter [0:0] OPT_PIPELINED = 1'b0; -`endif -`ifdef OPT_PIPELINED_BUS_ACCESS - localparam [0:0] OPT_PIPELINED_BUS_ACCESS = (OPT_PIPELINED); -`else - localparam [0:0] OPT_PIPELINED_BUS_ACCESS = 1'b0; -`endif - localparam [0:0] OPT_MEMPIPE = OPT_PIPELINED_BUS_ACCESS; - parameter [0:0] IMPLEMENT_LOCK=1; - localparam [0:0] OPT_LOCK=(IMPLEMENT_LOCK)&&(OPT_PIPELINED); -`ifdef OPT_DCACHE - parameter OPT_LGDCACHE = 10; -`else - parameter OPT_LGDCACHE = 0; -`endif - localparam [0:0] OPT_DCACHE = (OPT_LGDCACHE > 0); - - parameter [0:0] WITH_LOCAL_BUS = 1'b1; - localparam AW=ADDRESS_WIDTH; - localparam [(AW-1):0] RESET_BUS_ADDRESS = RESET_ADDRESS[(AW+1):2]; - parameter F_LGDEPTH=8; - - //}}} - // I/O declarations - //{{{ - input wire i_clk, i_reset, i_interrupt; - // Debug interface -- inputs - input wire i_halt, i_clear_pf_cache; - input wire [4:0] i_dbg_reg; - input wire i_dbg_we; - input wire [31:0] i_dbg_data; - // Debug interface -- outputs - output wire o_dbg_stall; - output reg [31:0] o_dbg_reg; - output reg [3:0] o_dbg_cc; - output wire o_break; - // Wishbone interface -- outputs - output wire o_wb_gbl_cyc, o_wb_gbl_stb; - output wire o_wb_lcl_cyc, o_wb_lcl_stb, o_wb_we; - output wire [(AW-1):0] o_wb_addr; - output wire [31:0] o_wb_data; - output wire [3:0] o_wb_sel; - // Wishbone interface -- inputs - input wire i_wb_stall, i_wb_ack; - input wire [31:0] i_wb_data; - input wire i_wb_err; - // Accounting outputs ... to help us count stalls and usage - output wire o_op_stall; - output wire o_pf_stall; - output wire o_i_count; - // -`ifdef DEBUG_SCOPE - output reg [31:0] o_debug; - // output wire [31:0] o_dcache_debug; -`endif - //}}} - - - // Registers - // - // The distributed RAM style comment is necessary on the - // SPARTAN6 with XST to prevent XST from oversimplifying the register - // set and in the process ruining everything else. It basically - // optimizes logic away, to where it no longer works. The logic - // as described herein will work, this just makes sure XST implements - // that logic. - // - (* ram_style = "distributed" *) - reg [31:0] regset [0:(OPT_NO_USERMODE)? 15:31]; - - // Condition codes - // (BUS, TRAP,ILL,BREAKEN,STEP,GIE,SLEEP ), V, N, C, Z - reg [3:0] flags, iflags; - wire [15:0] w_uflags, w_iflags; - reg break_en, step, sleep, r_halted; - wire break_pending, trap, gie, ubreak, pending_interrupt; - wire w_clear_icache, w_clear_dcache, ill_err_u; - reg ill_err_i; - reg ibus_err_flag; - wire ubus_err_flag; - wire idiv_err_flag, udiv_err_flag; - wire ifpu_err_flag, ufpu_err_flag; - wire ihalt_phase, uhalt_phase; - - // The master chip enable - wire master_ce, master_stall; - - // - // - // PIPELINE STAGE #1 :: Prefetch - // Variable declarations - // - //{{{ - reg [(AW+1):0] pf_pc; - wire [(AW+1):0] pf_request_address, pf_instruction_pc; - reg new_pc; - wire clear_pipeline; - - reg dcd_stalled; - wire pf_cyc, pf_stb, pf_we, pf_stall, pf_ack, pf_err; - wire [(AW-1):0] pf_addr; - wire [31:0] pf_data; - wire [31:0] pf_instruction; - wire pf_valid, pf_gie, pf_illegal; - wire pf_stalled; - wire pf_new_pc; - - assign clear_pipeline = new_pc; - //}}} - - // - // - // PIPELINE STAGE #2 :: Instruction Decode - // Variable declarations - // - // - //{{{ - reg op_valid /* verilator public_flat */, - op_valid_mem, op_valid_alu; - reg op_valid_div, op_valid_fpu; - wire op_stall, dcd_ce, dcd_phase; - wire [3:0] dcd_opn; - wire [4:0] dcd_A, dcd_B, dcd_R, dcd_preA, dcd_preB; - wire dcd_Acc, dcd_Bcc, dcd_Apc, dcd_Bpc, dcd_Rcc, dcd_Rpc; - wire [3:0] dcd_F; - wire dcd_wR, dcd_rA, dcd_rB, - dcd_ALU, dcd_M, dcd_DIV, dcd_FP, - dcd_wF, dcd_gie, dcd_break, dcd_lock, - dcd_pipe, dcd_ljmp; - wire dcd_valid; - wire [AW+1:0] dcd_pc /* verilator public_flat */; - wire [31:0] dcd_I; - wire dcd_zI; // true if dcd_I == 0 - wire dcd_A_stall, dcd_B_stall, dcd_F_stall; - - wire dcd_illegal; - wire dcd_early_branch, dcd_early_branch_stb; - wire [(AW+1):0] dcd_branch_pc; - - wire dcd_sim; - wire [22:0] dcd_sim_immv; - wire prelock_stall; - wire cc_invalid_for_dcd; - wire pending_sreg_write; - //}}} - - - // - // - // PIPELINE STAGE #3 :: Read Operands - // Variable declarations - // - // - // - //{{{ - // Now, let's read our operands - reg [4:0] alu_reg; - wire [3:0] op_opn; - reg [4:0] op_R; - reg op_Rcc; - reg [4:0] op_Aid, op_Bid; - reg op_rA, op_rB; - reg [31:0] r_op_Av, r_op_Bv; - reg [(AW+1):0] op_pc; - wire [31:0] w_op_Av, w_op_Bv, op_Av, op_Bv; - reg [31:0] w_pcB_v, w_pcA_v; - reg [31:0] w_op_BnI; - reg op_wR, op_wF; - wire op_gie; - wire [3:0] op_Fl; - reg [6:0] r_op_F; - wire [7:0] op_F; - wire op_ce, op_phase, op_pipe; - reg r_op_break; - reg [3:0] r_op_opn; - wire w_op_valid; - wire [8:0] w_cpu_info; - // Some pipeline control wires - reg op_illegal; - wire op_break; - wire op_lock; - -`ifdef VERILATOR - reg op_sim /* verilator public_flat */; - reg [22:0] op_sim_immv /* verilator public_flat */; - reg alu_sim /* verilator public_flat */; - reg [22:0] alu_sim_immv /* verilator public_flat */; -`else - wire op_sim = 1'b0; -`endif - //}}} - - - // - // - // PIPELINE STAGE #4 :: ALU / Memory - // Variable declarations - // - // - //{{{ - wire [(AW+1):0] alu_pc; - reg r_alu_pc_valid, mem_pc_valid; - wire alu_pc_valid; - wire alu_phase; - wire alu_ce /* verilator public_flat */, alu_stall; - wire [31:0] alu_result; - wire [3:0] alu_flags; - wire alu_valid, alu_busy; - wire set_cond; - reg alu_wR, alu_wF; - wire alu_gie, alu_illegal; - - - wire mem_ce, mem_stalled; - wire mem_pipe_stalled; - wire mem_valid, mem_stall, mem_ack, mem_err, bus_err, - mem_cyc_gbl, mem_cyc_lcl, mem_stb_gbl, mem_stb_lcl, mem_we; - wire [4:0] mem_wreg; - - wire mem_busy, mem_rdbusy; - wire [(AW-1):0] mem_addr; - wire [31:0] mem_data, mem_result; - wire [3:0] mem_sel; - - wire div_ce, div_error, div_busy, div_valid; - wire [31:0] div_result; - wire [3:0] div_flags; - - wire fpu_ce, fpu_error, fpu_busy, fpu_valid; - wire [31:0] fpu_result; - wire [3:0] fpu_flags; - reg adf_ce_unconditional; - - wire bus_lock; - - reg dbgv, dbg_clear_pipe; - reg [31:0] dbg_val; - - assign div_ce = (op_valid_div)&&(adf_ce_unconditional)&&(set_cond); - assign fpu_ce = (IMPLEMENT_FPU)&&(op_valid_fpu)&&(adf_ce_unconditional)&&(set_cond); - - //}}} - - // - // - // PIPELINE STAGE #5 :: Write-back - // Variable declarations - // - //{{{ - wire wr_discard, wr_write_pc, wr_write_cc, - wr_write_scc, wr_write_ucc; - reg wr_reg_ce, wr_flags_ce; - reg [3:0] wr_flags; - reg [2:0] wr_index; - wire [4:0] wr_reg_id; - reg [31:0] wr_gpreg_vl, wr_spreg_vl; - wire w_switch_to_interrupt, w_release_from_interrupt; - reg [(AW+1):0] ipc; - wire [(AW+1):0] upc; - reg last_write_to_cc; - wire cc_write_hold; - reg r_clear_icache, r_clear_dcache; - //}}} - - - - // - // MASTER: clock enable. - // - assign master_ce = ((!i_halt)||(alu_phase)) - &&(!cc_write_hold)&&(!o_break)&&(!sleep); - - - // - // PIPELINE STAGE #1 :: Prefetch - // Calculate stall conditions - // - // These are calculated externally, within the prefetch module. - // - - // - // PIPELINE STAGE #2 :: Instruction Decode - // Calculate stall conditions - - always @(*) - if (OPT_PIPELINED) - dcd_stalled = (dcd_valid)&&(op_stall); - else - dcd_stalled = (!master_ce)||(ill_err_i)||(dcd_valid)||(op_valid) - ||(ibus_err_flag)||(idiv_err_flag) - ||(alu_busy)||(div_busy)||(fpu_busy)||(mem_busy); - // - // PIPELINE STAGE #3 :: Read Operands - // Calculate stall conditions - //{{{ - generate if (OPT_PIPELINED) - begin : GEN_OP_STALL - reg r_cc_invalid_for_dcd; - always @(posedge i_clk) - r_cc_invalid_for_dcd <= - (set_cond)&&(op_valid) - &&((op_wF)||((op_wR)&&(op_R[4:0] == { op_gie, `CPU_CC_REG }))) - ||((r_cc_invalid_for_dcd) - &&((alu_busy)||(mem_rdbusy)||(div_busy)||(fpu_busy))); - - assign cc_invalid_for_dcd = r_cc_invalid_for_dcd; - - reg r_pending_sreg_write; - initial r_pending_sreg_write = 1'b0; - always @(posedge i_clk) - if (clear_pipeline) - r_pending_sreg_write <= 1'b0; - else if (((adf_ce_unconditional)||(mem_ce)) - &&(set_cond)&&(!op_illegal) - &&(op_wR) - &&(op_R[3:1] == 3'h7) - &&(op_R[4:0] != { gie, 4'hf })) - r_pending_sreg_write <= 1'b1; - else if ((!mem_rdbusy)&&(!alu_busy)) - r_pending_sreg_write <= 1'b0; - - assign pending_sreg_write = r_pending_sreg_write; - - assign op_stall = (op_valid)&&( - //{{{ - // Only stall if we're loaded w/validins and the - // next stage is accepting our instruction - (!adf_ce_unconditional)&&(!mem_ce) - ) - ||(dcd_valid)&&( - // Stall if we need to wait for an operand A - // to be ready to read - (dcd_A_stall) - // Likewise for B, also includes logic - // regarding immediate offset (register must - // be in register file if we need to add to - // an immediate) - ||(dcd_B_stall) - // Or if we need to wait on flags to work on the - // CC register - ||(dcd_F_stall) - ); - //}}} - assign op_ce = ((dcd_valid)||(dcd_illegal)||(dcd_early_branch))&&(!op_stall); - - end else begin // !OPT_PIPELINED - - assign op_stall = 1'b0; // (o_break)||(pending_interrupt); - assign op_ce = ((dcd_valid)||(dcd_early_branch))&&(!op_stall); - assign pending_sreg_write = 1'b0; - assign cc_invalid_for_dcd = 1'b0; - - // Verilator lint_off UNUSED - wire [1:0] pipe_unused; - assign pipe_unused = { cc_invalid_for_dcd, - pending_sreg_write }; - // Verilator lint_on UNUSED - end endgenerate - - // BUT ... op_ce is too complex for many of the data operations. So - // let's make their circuit enable code simpler. In particular, if - // op_ doesn't need to be preserved, we can change it all we want - // ... right? The clear_pipeline code, for example, really only needs - // to determine whether op_valid is true. - // assign op_change_data_ce = (!op_stall); - //}}} - - // - // PIPELINE STAGE #4 :: ALU / Memory - // Calculate stall conditions - // - //{{{ - // 1. Basic stall is if the previous stage is valid and the next is - // busy. - // 2. Also stall if the prior stage is valid and the master clock enable - // is de-selected - // 3. Stall if someone on the other end is writing the CC register, - // since we don't know if it'll put us to sleep or not. - // 4. Last case: Stall if we would otherwise move a break instruction - // through the ALU. Break instructions are not allowed through - // the ALU. - generate if (OPT_PIPELINED) - begin : GEN_ALU_STALL - assign alu_stall = (((master_stall)||(mem_rdbusy))&&(op_valid_alu)) //Case 1&2 - ||(wr_reg_ce)&&(wr_write_cc); - // assign // alu_ce = (master_ce)&&(op_valid_alu)&&(!alu_stall) - // &&(!clear_pipeline)&&(!op_illegal) - // &&(!pending_sreg_write) - // &&(!alu_sreg_stall); - assign alu_ce = (adf_ce_unconditional)&&(op_valid_alu); - - // Verilator lint_off unused - wire unused_alu_stall = alu_stall; - // Verilator lint_on unused - end else begin - - assign alu_stall = (master_stall); - //assign alu_ce = (master_ce)&&(op_valid_alu) - // &&(!clear_pipeline) - // &&(!alu_stall); - assign alu_ce = (adf_ce_unconditional)&&(op_valid_alu); - - // Verilator lint_off unused - wire unused_alu_stall = alu_stall; - // Verilator lint_on unused - end endgenerate - // - - // - // Note: if you change the conditions for mem_ce, you must also change - // alu_pc_valid. - // - assign mem_ce = (master_ce)&&(op_valid_mem)&&(!mem_stalled) - &&(!clear_pipeline); - - generate if (OPT_PIPELINED_BUS_ACCESS) - begin - - assign mem_stalled = (master_stall)||((op_valid_mem)&&( - (mem_pipe_stalled) - ||(bus_err)||(div_error) - ||((!op_pipe)&&(mem_busy)) - // Stall waiting for flags to be valid - // Or waiting for a write to the PC register - // Or CC register, since that can change the - // PC as well - ||((wr_reg_ce) - &&((wr_write_pc)||(wr_write_cc))))); - end else if (OPT_PIPELINED) - begin - assign mem_stalled = (master_stall)||((op_valid_mem)&&( - (bus_err)||(div_error)||(mem_busy) - // Stall waiting for flags to be valid - // Or waiting for a write to the PC register - // Or CC register, since that can change the - // PC as well - ||((wr_reg_ce) - &&((wr_write_pc)||(wr_write_cc))))); - end else begin - - assign mem_stalled = (master_stall); - - end endgenerate - //}}} - - assign master_stall = (!master_ce)||(!op_valid)||(ill_err_i) - ||(ibus_err_flag)||(idiv_err_flag) - ||(pending_interrupt)&&(!alu_phase) - ||(alu_busy)||(div_busy)||(fpu_busy)||(op_break) - ||((OPT_PIPELINED)&&( - ((OPT_LOCK)&&(prelock_stall)) - ||((mem_busy)&&(op_illegal)) - ||((mem_busy)&&(op_valid_div)) - ||(alu_illegal)||(o_break))); - - - // ALU, DIV, or FPU CE ... equivalent to the OR of all three of these - always @(*) - if (OPT_PIPELINED) - adf_ce_unconditional = - (!master_stall)&&(!op_valid_mem)&&(!mem_rdbusy) - &&((!mem_busy)||(!op_wR)||(op_R[4:1] != { gie, 3'h7})); - else - adf_ce_unconditional = (!master_stall)&&(op_valid)&&(!op_valid_mem); - - // - // - // PIPELINE STAGE #1 :: Prefetch - // - // - //{{{ - assign pf_stalled = (dcd_stalled)||(dcd_phase); - - assign pf_new_pc = (new_pc)||((dcd_early_branch_stb)&&(!clear_pipeline)); - - assign pf_request_address = ((dcd_early_branch_stb)&&(!clear_pipeline)) - ? dcd_branch_pc:pf_pc; - assign pf_gie = gie; -`ifdef OPT_SINGLE_FETCH - prefetch #(ADDRESS_WIDTH) - //{{{ - pf(i_clk, (i_reset), pf_new_pc, w_clear_icache, - (!pf_stalled), - pf_request_address, - pf_instruction, pf_instruction_pc, - pf_valid, pf_illegal, - pf_cyc, pf_stb, pf_we, pf_addr, pf_data, - pf_stall, pf_ack, pf_err, i_wb_data); - //}}} -`else -`ifdef OPT_DOUBLE_FETCH - - dblfetch #(ADDRESS_WIDTH) - //{{{ - pf(i_clk, i_reset, pf_new_pc, w_clear_icache, - (!pf_stalled), - pf_request_address, - pf_instruction, pf_instruction_pc, - pf_valid, - pf_cyc, pf_stb, pf_we, pf_addr, pf_data, - pf_stall, pf_ack, pf_err, i_wb_data, - pf_illegal); - //}}} - -`else // Not single fetch and not double fetch - -`ifdef OPT_TRADITIONAL_PFCACHE - pfcache #(LGICACHE, ADDRESS_WIDTH) - //{{{ - pf(i_clk, i_reset, pf_new_pc, w_clear_icache, - // dcd_pc, - (!pf_stalled), - pf_request_address, - pf_instruction, pf_instruction_pc, pf_valid, - pf_cyc, pf_stb, pf_we, pf_addr, pf_data, - pf_stall, pf_ack, pf_err, i_wb_data, - pf_illegal); - //}}} -`else - pipefetch #({RESET_BUS_ADDRESS, 2'b00}, LGICACHE, ADDRESS_WIDTH) - //{{{ - pf(i_clk, i_reset, pf_new_pc, - w_clear_icache, (!pf_stalled), - (new_pc)?pf_pc:dcd_branch_pc, - pf_instruction, pf_instruction_pc, pf_valid, - pf_cyc, pf_stb, pf_we, pf_addr, pf_data, - pf_stall, pf_ack, pf_err, i_wb_data, - (mem_cyc_lcl)||(mem_cyc_gbl), - pf_illegal); - //}}} -`endif // OPT_TRADITIONAL_CACHE -`endif // OPT_DOUBLE_FETCH -`endif // OPT_SINGLE_FETCH - //}}} - - // - // - // PIPELINE STAGE #2 :: Instruction Decode - // - // - //{{{ - assign dcd_ce =((OPT_PIPELINED)&&(!dcd_valid))||(!dcd_stalled); - idecode #(.ADDRESS_WIDTH(AW), - .OPT_MPY((IMPLEMENT_MPY!=0)? 1'b1:1'b0), - .OPT_PIPELINED(OPT_PIPELINED), - .OPT_EARLY_BRANCHING(EARLY_BRANCHING), - .OPT_DIVIDE(IMPLEMENT_DIVIDE), - .OPT_FPU(IMPLEMENT_FPU), - .OPT_LOCK(OPT_LOCK), - .OPT_OPIPE(OPT_PIPELINED_BUS_ACCESS), - .OPT_NO_USERMODE(OPT_NO_USERMODE), -`ifdef VERILATOR - .OPT_SIM(1'b1), -`else - .OPT_SIM(1'b0), -`endif - .OPT_CIS(OPT_CIS)) - instruction_decoder(i_clk, - (i_reset)||(clear_pipeline)||(w_clear_icache), - dcd_ce, - dcd_stalled, pf_instruction, pf_gie, - pf_instruction_pc, pf_valid, pf_illegal, - dcd_valid, dcd_phase, - dcd_illegal, dcd_pc, - { dcd_Rcc, dcd_Rpc, dcd_R }, - { dcd_Acc, dcd_Apc, dcd_A }, - { dcd_Bcc, dcd_Bpc, dcd_B }, - dcd_preA, dcd_preB, - dcd_I, dcd_zI, dcd_F, dcd_wF, dcd_opn, - dcd_ALU, dcd_M, dcd_DIV, dcd_FP, dcd_break, dcd_lock, - dcd_wR,dcd_rA, dcd_rB, - dcd_early_branch, dcd_early_branch_stb, - dcd_branch_pc, dcd_ljmp, - dcd_pipe, - dcd_sim, dcd_sim_immv - ); - assign dcd_gie = pf_gie; - //}}} - - // - // - // PIPELINE STAGE #3 :: Read Operands (Registers) - // - // - //{{{ - generate if (OPT_PIPELINED_BUS_ACCESS) - begin : GEN_OP_PIPE - reg r_op_pipe; - - initial r_op_pipe = 1'b0; - // To be a pipeable operation, there must be - // two valid adjacent instructions - // Both must be memory instructions - // Both must be writes, or both must be reads - // Both operations must be to the same identical address, - // or at least a single (one) increment above that - // address - // - // However ... we need to know this before this clock, hence - // this is calculated in the instruction decoder. - always @(posedge i_clk) - if ((clear_pipeline)||(i_halt)) - r_op_pipe <= 1'b0; - else if (op_ce) - r_op_pipe <= (dcd_pipe)&&(op_valid_mem); - else if ((wr_reg_ce)&&(wr_reg_id == op_Bid[4:0])) - r_op_pipe <= 1'b0; - else if (mem_ce) // Clear us any time an op_ is clocked in - r_op_pipe <= 1'b0; - - assign op_pipe = r_op_pipe; - end else begin - - assign op_pipe = 1'b0; - - end endgenerate - -// `define NO_DISTRIBUTED_RAM -`ifdef NO_DISTRIBUTED_RAM - reg [31:0] pre_rewrite_value, pre_op_Av, pre_op_Bv; - reg pre_rewrite_flag_A, pre_rewrite_flag_B; - - always @(posedge i_clk) - if (dcd_ce) - begin - pre_rewrite_flag_A <= (wr_reg_ce)&&(dcd_preA == wr_reg_id); - pre_rewrite_flag_B <= (wr_reg_ce)&&(dcd_preB == wr_reg_id); - pre_rewrite_value <= wr_gpreg_vl; - end - - generate if (OPT_NO_USERMODE) - begin - always @(posedge i_clk) - if (dcd_ce) - begin - pre_op_Av <= regset[dcd_preA[3:0]]; - pre_op_Bv <= regset[dcd_preB[3:0]]; - end - end else begin - - always @(posedge i_clk) - if (dcd_ce) - begin - pre_op_Av <= regset[dcd_preA]; - pre_op_Bv <= regset[dcd_preB]; - end - - end endgenerate - - assign w_op_Av = (pre_rewrite_flag_A) ? pre_rewrite_value : pre_op_Av; - assign w_op_Bv = (pre_rewrite_flag_B) ? pre_rewrite_value : pre_op_Bv; -`else - generate if (OPT_NO_USERMODE) - begin - assign w_op_Av = regset[dcd_A[3:0]]; - assign w_op_Bv = regset[dcd_B[3:0]]; - end else begin - - assign w_op_Av = regset[dcd_A]; - assign w_op_Bv = regset[dcd_B]; - - end endgenerate - - // verilator lint_off UNUSED - wire [9:0] unused_prereg_addrs; - assign unused_prereg_addrs = { dcd_preA, dcd_preB }; - // verilator lint_on UNUSED -`endif - - assign w_cpu_info = { - //{{{ - 1'b1, - (IMPLEMENT_MPY >0)? 1'b1:1'b0, - (IMPLEMENT_DIVIDE >0)? 1'b1:1'b0, - (IMPLEMENT_FPU >0)? 1'b1:1'b0, - OPT_PIPELINED, -`ifdef OPT_TRADITIONAL_CACHE - 1'b1, -`else - 1'b0, -`endif - (EARLY_BRANCHING > 0)? 1'b1:1'b0, - OPT_PIPELINED_BUS_ACCESS, - OPT_CIS - }; - //}}} - - always @(*) - begin - w_pcA_v = 0; - if ((OPT_NO_USERMODE)||(dcd_A[4] == dcd_gie)) - w_pcA_v[(AW+1):0] = { dcd_pc[AW+1:2], 2'b00 }; - else - w_pcA_v[(AW+1):0] = { upc[(AW+1):2], uhalt_phase, 1'b0 }; - end - - generate if (OPT_PIPELINED) - begin : OPV - initial op_R = 0; - initial op_Aid = 0; - initial op_Bid = 0; - initial op_rA = 0; - initial op_rB = 0; - initial op_Rcc = 0; - always @(posedge i_clk) - if (op_ce) - begin - op_R <= dcd_R; - op_Aid <= dcd_A; - op_Bid <= dcd_B; - op_rA <= (dcd_rA)&&(!dcd_early_branch)&&(!dcd_illegal); - op_rB <= (dcd_rB)&&(!dcd_early_branch)&&(!dcd_illegal); - op_Rcc <= (dcd_Rcc)&&(dcd_wR)&&(dcd_R[4]==dcd_gie); - end - - end else begin - - always @(*) - begin - op_R = dcd_R; - op_Aid = dcd_A; - op_Bid = dcd_B; - op_rA = dcd_rA; - op_rB = dcd_rB; - op_Rcc = (dcd_Rcc)&&(dcd_wR)&&(dcd_R[4]==dcd_gie); - end - - end endgenerate - - reg [2:0] avsrc; - always @(*) - begin - avsrc = 3'b000; - if (!OPT_PIPELINED || op_ce) - begin - if (dcd_Apc) - avsrc = 3'b101; - else if (dcd_Acc) - avsrc = 3'b110; - else - avsrc = 3'b111; - end - - if (OPT_PIPELINED && wr_reg_ce) - begin - if (!op_ce && wr_reg_id == op_Aid && op_rA) - avsrc = 3'b100; - else if (op_ce && wr_reg_id == dcd_A) - avsrc = 3'b100; - end - end - -// 44313 - always @(posedge i_clk) - if (avsrc[2]) - case(avsrc[1:0]) - 2'b00: r_op_Av <= wr_gpreg_vl; - 2'b01: r_op_Av <= w_pcA_v; - 2'b10: r_op_Av <= { w_cpu_info, w_op_Av[22:16], (dcd_A[4])?w_uflags:w_iflags }; - 2'b11: r_op_Av <= w_op_Av; - endcase - - always @(*) - begin - w_pcB_v = 0; - if ((OPT_NO_USERMODE)||(dcd_B[4] == dcd_gie)) - w_pcB_v[(AW+1):0] = { dcd_pc[AW+1:2], 2'b00 }; - else - w_pcB_v[(AW+1):0] = { upc[(AW+1):2], uhalt_phase, 1'b0 }; - end - - reg [2:0] bvsrc; - reg [1:0] bisrc; - always @(*) - if (!dcd_rB) - bisrc = 0; - else if ((OPT_PIPELINED)&&(wr_reg_ce)&&(wr_reg_id == dcd_B)) - bisrc = 1; - else if (dcd_Bcc) - bisrc = 2; - else - bisrc = 3; - - always @(*) - case(bisrc[1:0]) - 2'b00: w_op_BnI = 0; - 2'b01: w_op_BnI = wr_gpreg_vl; - 2'b10: w_op_BnI = { w_cpu_info, w_op_Bv[22:16], - (dcd_B[4]) ? w_uflags : w_iflags }; - 2'b11: w_op_BnI = w_op_Bv; - endcase - - always @(*) - begin - bvsrc = 0; - if ((!OPT_PIPELINED)||(op_ce)) - begin - if ((dcd_Bpc)&&(dcd_rB)) - bvsrc = 3'b100; - else - bvsrc = 3'b101; - end else if ((OPT_PIPELINED)&&(op_rB) - &&(wr_reg_ce)&&(op_Bid == wr_reg_id)) - bvsrc = 3'b110; - end - - always @(posedge i_clk) - if (bvsrc[2]) casez(bvsrc[1:0]) - 2'b00: r_op_Bv <= w_pcB_v + { dcd_I[29:0], 2'b00 }; - 2'b01: r_op_Bv <= w_op_BnI + dcd_I; - 2'b1?: r_op_Bv <= wr_gpreg_vl; - endcase - - // The logic here has become more complex than it should be, no thanks - // to Xilinx's Vivado trying to help. The conditions are supposed to - // be two sets of four bits: the top bits specify what bits matter, the - // bottom specify what those top bits must equal. However, two of - // conditions check whether bits are on, and those are the only two - // conditions checking those bits. Therefore, Vivado complains that - // these two bits are redundant. Hence the convoluted expression - // below, arriving at what we finally want in the (now wire net) - // op_F. - always @(posedge i_clk) - if ((!OPT_PIPELINED)||(op_ce)) - // Cannot do op_change_data_ce here since op_F depends - // upon being either correct for a valid op, or correct - // for the last valid op - begin // Set the flag condition codes, bit order is [3:0]=VNCZ - case(dcd_F[2:0]) - 3'h0: r_op_F <= 7'h00; // Always - 3'h1: r_op_F <= 7'h11; // Z - 3'h2: r_op_F <= 7'h44; // LT - 3'h3: r_op_F <= 7'h22; // C - 3'h4: r_op_F <= 7'h08; // V - 3'h5: r_op_F <= 7'h10; // NE - 3'h6: r_op_F <= 7'h40; // GE (!N) - 3'h7: r_op_F <= 7'h20; // NC - endcase - end // Bit order is { (flags_not_used), VNCZ mask, VNCZ value } - assign op_F = { r_op_F[3], r_op_F[6:0] }; - - assign w_op_valid = (!clear_pipeline)&&(dcd_valid) - &&(!dcd_ljmp)&&(!dcd_early_branch); - - initial op_valid = 1'b0; - initial op_valid_alu = 1'b0; - initial op_valid_mem = 1'b0; - initial op_valid_div = 1'b0; - initial op_valid_fpu = 1'b0; - always @(posedge i_clk) - if ((i_reset)||(clear_pipeline)) - begin - op_valid <= 1'b0; - op_valid_alu <= 1'b0; - op_valid_mem <= 1'b0; - op_valid_div <= 1'b0; - op_valid_fpu <= 1'b0; - end else if (op_ce) - begin - // Do we have a valid instruction? - // The decoder may vote to stall one of its - // instructions based upon something we currently - // have in our queue. This instruction must then - // move forward, and get a stall cycle inserted. - // Hence, the test on dcd_stalled here. If we must - // wait until our operands are valid, then we aren't - // valid yet until then. - if (OPT_PIPELINED || !op_valid) - begin - op_valid <= (w_op_valid)||(dcd_early_branch); - op_valid_alu <= (w_op_valid)&&((dcd_ALU)||(dcd_illegal)); - op_valid_mem <= (dcd_M)&&(!dcd_illegal) - &&(w_op_valid); - op_valid_div <= (IMPLEMENT_DIVIDE)&&(dcd_DIV)&&(!dcd_illegal)&&(w_op_valid); - op_valid_fpu <= (IMPLEMENT_FPU)&&(dcd_FP)&&(!dcd_illegal)&&(w_op_valid); - end else if ((adf_ce_unconditional)||(mem_ce)) - begin - op_valid <= 1'b0; - op_valid_alu <= 1'b0; - op_valid_mem <= 1'b0; - op_valid_div <= 1'b0; - op_valid_fpu <= 1'b0; - end - end else if ((adf_ce_unconditional)||(mem_ce)) - begin - op_valid <= 1'b0; - op_valid_alu <= 1'b0; - op_valid_mem <= 1'b0; - op_valid_div <= 1'b0; - op_valid_fpu <= 1'b0; - end - - // Here's part of our debug interface. When we recognize a break - // instruction, we set the op_break flag. That'll prevent this - // instruction from entering the ALU, and cause an interrupt before - // this instruction. Thus, returning to this code will cause the - // break to repeat and continue upon return. To get out of this - // condition, replace the break instruction with what it is supposed - // to be, step through it, and then replace it back. In this fashion, - // a debugger can step through code. - // assign w_op_break = (dcd_break)&&(r_dcd_I[15:0] == 16'h0001); - - initial r_op_break = 1'b0; - always @(posedge i_clk) - if (clear_pipeline) - r_op_break <= 1'b0; - else if ((OPT_PIPELINED)&&(op_ce)) - r_op_break <= (dcd_valid)&&(dcd_break)&&(!dcd_illegal); - else if ((!OPT_PIPELINED)&&(dcd_valid)) - r_op_break <= (dcd_break)&&(!dcd_illegal); - assign op_break = r_op_break; - - generate if ((!OPT_PIPELINED)||(!OPT_LOCK)) - begin - - assign op_lock = 1'b0; - - // Verilator lint_off UNUSED - wire dcd_lock_unused; - assign dcd_lock_unused = dcd_lock; - // Verilator lint_on UNUSED - - end else // if (IMPLEMENT_LOCK != 0) - begin : OPLOCK - reg r_op_lock; - - initial r_op_lock = 1'b0; - always @(posedge i_clk) - if (clear_pipeline) - r_op_lock <= 1'b0; - else if (op_ce) - r_op_lock <= (dcd_valid)&&(dcd_lock) - &&(!dcd_illegal); - assign op_lock = r_op_lock; - - end endgenerate - - initial op_illegal = 1'b0; - always @(posedge i_clk) - if ((i_reset)||(clear_pipeline)) - op_illegal <= 1'b0; - else if (OPT_PIPELINED) - begin - if (op_ce) - op_illegal <= (dcd_valid)&&(!dcd_ljmp) - &&(!dcd_early_branch)&&(dcd_illegal); - end else if (!OPT_PIPELINED) - begin - if (dcd_valid) - op_illegal <= (!dcd_ljmp)&&(!dcd_early_branch)&&(dcd_illegal); - end - - always @(posedge i_clk) - if ((!OPT_PIPELINED)||(op_ce)) - op_wF <= (dcd_wF)&&((!dcd_Rcc)||(!dcd_wR)) - &&(!dcd_early_branch); - - generate if ((OPT_PIPELINED)||(EARLY_BRANCHING)) - begin - - always @(posedge i_clk) - if (op_ce) - op_wR <= (dcd_wR)&&(!dcd_early_branch); - - end else begin - - always @(*) - op_wR = (dcd_wR); - - end endgenerate - -`ifdef VERILATOR -`ifdef SINGLE_FETCH - always @(*) - begin - op_sim = dcd_sim; - op_sim_immv = dcd_sim_immv; - - alu_sim = op_sim; - alu_sim_immv = op_sim_immv; - end -`else - always @(posedge i_clk) - if (op_ce) - begin - op_sim <= dcd_sim; - op_sim_immv <= dcd_sim_immv; - end else if (adf_ce_unconditional) - op_sim <= 1'b0; - - // - // We need an extra ALU sim stage. - // - // It's not quite true that all instructions leaving the OP stage - // are committed. ALU instructions aren't committed until the next - // cycle. That way they don't need to wait to determine if a branch - // was taken or not. - always @(posedge i_clk) - begin - if (adf_ce_unconditional) - alu_sim <= op_sim && op_valid_alu; - else - alu_sim <= 1'b0; - - if (adf_ce_unconditional) - alu_sim_immv <= op_sim_immv; - - if (i_reset || clear_pipeline) - alu_sim <= 1'b0; - end -`endif -`endif - - - generate if ((OPT_PIPELINED)||(EARLY_BRANCHING)) - begin : SET_OP_PC - - initial op_pc[0] = 1'b0; - always @(posedge i_clk) - if (op_ce) - op_pc <= (dcd_early_branch)?dcd_branch_pc:dcd_pc; - - end else begin : SET_OP_PC - - always @(*) - op_pc = dcd_pc; - - end endgenerate - - generate if (!OPT_PIPELINED) - begin - always @(*) - r_op_opn = dcd_opn; - - end else begin - - always @(posedge i_clk) - if (op_ce) - begin - // Which ALU operation? Early branches are - // unimplemented moves - r_op_opn <= ((dcd_early_branch)||(dcd_illegal)) - ? `CPU_MOV_OP : dcd_opn; - // opM <= dcd_M; // Is this a memory operation? - // What register will these results be written into? - end - - end endgenerate - - assign op_opn = r_op_opn; - assign op_gie = gie; - - assign op_Fl = (op_gie)?(w_uflags[3:0]):(w_iflags[3:0]); - - generate if (OPT_CIS) - begin : OPT_CIS_OP_PHASE - - reg r_op_phase; - - initial r_op_phase = 1'b0; - always @(posedge i_clk) - if ((i_reset)||(clear_pipeline)) - r_op_phase <= 1'b0; - else if (op_ce) - r_op_phase <= (dcd_phase)&&((!dcd_wR)||(!dcd_Rpc)); - assign op_phase = r_op_phase; - end else begin : OPT_NOCIS_OP_PHASE - assign op_phase = 1'b0; - - // verilator lint_off UNUSED - wire OPT_CIS_dcdRpc; - assign OPT_CIS_dcdRpc = dcd_Rpc; - // verilator lint_on UNUSED - end endgenerate - - // This is tricky. First, the PC and Flags registers aren't kept in - // register set but in special registers of their own. So step one - // is to select the right register. Step to is to replace that - // register with the results of an ALU or memory operation, if such - // results are now available. Otherwise, we'd need to insert a wait - // state of some type. - // - // The alternative approach would be to define some sort of - // op_stall wire, which would stall any upstream stage. - // We'll create a flag here to start our coordination. Once we - // define this flag to something other than just plain zero, then - // the stalls will already be in place. - generate if (OPT_PIPELINED) - begin - - assign op_Av = ((wr_reg_ce)&&(wr_reg_id == op_Aid)) - ? wr_gpreg_vl : r_op_Av; - - end else begin - - assign op_Av = r_op_Av; - - end endgenerate - - // Stall if we have decoded an instruction that will read register A - // AND ... something that may write a register is running - // AND (series of conditions here ...) - // The operation might set flags, and we wish to read the - // CC register - // OR ... (No other conditions) - generate if (OPT_PIPELINED) - begin - - assign dcd_A_stall = (dcd_rA) // &&(dcd_valid) is checked for elsewhere - &&((op_valid)||(mem_rdbusy) - ||(div_busy)||(fpu_busy)) - &&(((op_wF)||(cc_invalid_for_dcd))&&(dcd_Acc)) - ||((dcd_rA)&&(dcd_Acc)&&(cc_invalid_for_dcd)); - end else begin - - // There are no pipeline hazards, if we aren't pipelined - assign dcd_A_stall = 1'b0; - - end endgenerate - - assign op_Bv = ((OPT_PIPELINED)&&(wr_reg_ce) - &&(wr_reg_id == op_Bid)&&(op_rB)) - ? wr_gpreg_vl: r_op_Bv; - - generate if (OPT_PIPELINED) - begin - // Stall if we have decoded an instruction that will read register B - // AND ... something that may write a (unknown) register is running - // AND (series of conditions here ...) - // The operation might set flags, and we wish to read the - // CC register - // OR the operation might set register B, and we still need - // a clock to add the offset to it - assign dcd_B_stall = (dcd_rB) // &&(dcd_valid) is checked for elsewhere - //{{{ - // If the op stage isn't valid, yet something - // is running, then it must have been valid. - // We'll use the last values from that stage - // (op_wR, op_wF, op_R) in our logic below. - &&((op_valid)||(mem_rdbusy) - ||(div_busy)||(fpu_busy)||(alu_busy)) - &&( - // Okay, what happens if the result register - // from instruction 1 becomes the input for - // instruction two, *and* there's an immediate - // offset in instruction two? In that case, we - // need an extra clock between the two - // instructions to calculate the base plus - // offset. - // - // What if instruction 1 (or before) is in a - // memory pipeline? We may no longer know what - // the register was! We will then need to - // blindly wait. We'll temper this only waiting - // if we're not piping this new instruction. - // If we were piping, the pipe logic in the - // decode circuit has told us that the hazard - // is clear, so we're okay then. - // - ((!dcd_zI)&&( - ((op_R == dcd_B)&&(op_wR)) - ||((mem_rdbusy)&&(!dcd_pipe)) - ||(((alu_busy)||(div_busy))&&(alu_reg == dcd_B)) - ||((wr_reg_ce)&&(wr_reg_id[3:1] == 3'h7)) - )) - // Stall following any instruction that will - // set the flags, if we're going to need the - // flags (CC) register for op_B. - ||(((op_wF)||(cc_invalid_for_dcd))&&(dcd_Bcc)) - // Stall on any ongoing memory operation that - // will write to op_B -- captured above - // ||((mem_busy)&&(!mem_we)&&(mem_last_reg==dcd_B)&&(!dcd_zI)) - ) - ||((dcd_rB)&&(dcd_Bcc)&&(cc_invalid_for_dcd)); - //}}} - assign dcd_F_stall = ((!dcd_F[3]) - //{{{ - ||((dcd_rA)&&(dcd_A[3:1]==3'h7) - &&(dcd_A[4:0] != { gie, 4'hf})) - ||((dcd_rB)&&(dcd_B[3:1]==3'h7)) - &&(dcd_B[4:0] != { gie, 4'hf})) - &&(((op_valid)&&(op_wR) - &&(op_R[3:1]==3'h7) - &&(op_R[4:0]!={gie, 4'hf})) - ||(pending_sreg_write)); - // &&(dcd_valid) is checked for elsewhere - //}}} - end else begin - // No stalls without pipelining, 'cause how can you have a pipeline - // hazard without the pipeline? - assign dcd_B_stall = 1'b0; - assign dcd_F_stall = 1'b0; - end endgenerate - - //}}} - // - // - // PIPELINE STAGE #4 :: Apply Instruction - // - // - // ALU - cpuops #(IMPLEMENT_MPY) doalu(i_clk, ((i_reset)||(clear_pipeline)), - //{{{ - alu_ce, op_opn, op_Av, op_Bv, - alu_result, alu_flags, alu_valid, alu_busy); - //}}} - - // Divide - //{{{ - generate if (IMPLEMENT_DIVIDE != 0) - begin : DIVIDE -`define DIVIDE_MODULE div - `DIVIDE_MODULE thedivide(i_clk, ((i_reset)||(clear_pipeline)), - div_ce, op_opn[0], - op_Av, op_Bv, div_busy, div_valid, div_error, div_result, - div_flags); - - end else begin - - assign div_error = 1'b0; // Can't be high unless div_valid - assign div_busy = 1'b0; - assign div_valid = 1'b0; - assign div_result= 32'h00; - assign div_flags = 4'h0; - - // Make verilator happy here - // verilator lint_off UNUSED - wire unused_divide; - assign unused_divide = div_ce; - // verilator lint_on UNUSED - end endgenerate - //}}} - - // (Non-existent) FPU - //{{{ - generate if (IMPLEMENT_FPU != 0) - begin : FPU - // - // sfpu thefpu(i_clk, i_reset, fpu_ce, op_opn[2:0], - // op_Av, op_Bv, fpu_busy, fpu_valid, fpu_err, fpu_result, - // fpu_flags); - // - assign fpu_error = 1'b0; // Must only be true if fpu_valid - assign fpu_busy = 1'b0; - assign fpu_valid = 1'b0; - assign fpu_result= 32'h00; - assign fpu_flags = 4'h0; - end else begin - assign fpu_error = 1'b0; - assign fpu_busy = 1'b0; - assign fpu_valid = 1'b0; - assign fpu_result= 32'h00; - assign fpu_flags = 4'h0; - end endgenerate - //}}} - - - assign set_cond = ((op_F[7:4]&op_Fl[3:0])==op_F[3:0]); - initial alu_wF = 1'b0; - initial alu_wR = 1'b0; - generate if (OPT_PIPELINED) - begin - always @(posedge i_clk) - if (i_reset) - begin - alu_wR <= 1'b0; - alu_wF <= 1'b0; - end else if (alu_ce) - begin - // alu_reg <= op_R; - alu_wR <= (op_wR)&&(set_cond)&&(!op_illegal); - alu_wF <= (op_wF)&&(set_cond)&&(!op_illegal); - end else if (!alu_busy) begin - // These are strobe signals, so clear them if not - // set for any particular clock - alu_wR <= (r_halted)&&(i_dbg_we); - alu_wF <= 1'b0; - end - end else begin - - always @(posedge i_clk) - alu_wR <= (op_wR)&&(set_cond)&&(!op_illegal); - always @(posedge i_clk) - alu_wF <= (op_wF)&&(set_cond)&&(!op_illegal); - - end endgenerate - - generate if (OPT_CIS) - begin : GEN_ALU_PHASE - - reg r_alu_phase; - initial r_alu_phase = 1'b0; - always @(posedge i_clk) - if ((i_reset)||(clear_pipeline)) - r_alu_phase <= 1'b0; - else if (((adf_ce_unconditional)||(mem_ce))&&(op_valid)) - r_alu_phase <= op_phase; - else if ((adf_ce_unconditional)||(mem_ce)) - r_alu_phase <= 1'b0; - assign alu_phase = r_alu_phase; - end else begin - - assign alu_phase = 1'b0; - end endgenerate - - generate if (OPT_PIPELINED) - begin - - always @(posedge i_clk) - if (adf_ce_unconditional) - alu_reg <= op_R; - else if ((r_halted)&&(i_dbg_we)) - alu_reg <= i_dbg_reg; - - end else begin - - always @(posedge i_clk) - if ((r_halted)&&(i_dbg_we)) - alu_reg <= i_dbg_reg; - else - alu_reg <= op_R; - end endgenerate - - initial wr_index = 0; - always @(posedge i_clk) - begin - if ((OPT_PIPELINED && (mem_ce || adf_ce_unconditional)) - ||(!OPT_PIPELINED && op_valid)) - begin - wr_index <= 0; - /* - if (op_valid_mem) - wr_index <= 3'b001; - if (op_valid_alu) - wr_index <= 3'b010; - if (op_valid_div) - wr_index <= 3'b011; - if (op_valid_fpu) - wr_index <= 3'b100; - */ - - wr_index[0] <= (op_valid_mem | op_valid_div); - wr_index[1] <= (op_valid_alu | op_valid_div); - wr_index[2] <= (op_valid_fpu); - end - - if (i_dbg_we && r_halted) - wr_index <= 3'b000; - - if (!IMPLEMENT_FPU) - wr_index[2] <= 1'b0; - end - - // - // DEBUG Register write access starts here - // - //{{{ - initial dbgv = 1'b0; - always @(posedge i_clk) - if (i_reset) - dbgv <= 0; - else - dbgv <= (i_dbg_we)&&(r_halted); - - always @(posedge i_clk) - dbg_val <= i_dbg_data; - always @(posedge i_clk) - if ((i_reset)||(clear_pipeline)) - dbg_clear_pipe <= 0; - else if ((i_dbg_we)&&(r_halted)) - begin - dbg_clear_pipe <= 1'b0; - - if (!OPT_PIPELINED) - dbg_clear_pipe <= 1'b1; - if ((i_dbg_reg == op_Bid)&&(op_rB)) - dbg_clear_pipe <= 1'b1; - if (i_dbg_reg[3:1] == 3'h7) - dbg_clear_pipe <= 1'b1; - end else if ((!OPT_PIPELINED)&&(i_clear_pf_cache)) - dbg_clear_pipe <= 1'b1; - else - dbg_clear_pipe <= 1'b0; - - assign alu_gie = gie; - //}}} - - generate if (OPT_PIPELINED) - begin : GEN_ALU_PC - reg [(AW+1):0] r_alu_pc; - initial r_alu_pc = 0; - always @(posedge i_clk) - if ((adf_ce_unconditional) - ||((master_ce)&&(op_valid_mem) - &&(!clear_pipeline)&&(!mem_stalled))) - r_alu_pc <= op_pc; - assign alu_pc = r_alu_pc; - - end else begin - - assign alu_pc = op_pc; - - end endgenerate - - generate if (OPT_PIPELINED) - begin : SET_ALU_ILLEGAL - reg r_alu_illegal; - - initial r_alu_illegal = 0; - always @(posedge i_clk) - if (clear_pipeline) - r_alu_illegal <= 1'b0; - else if (adf_ce_unconditional) - r_alu_illegal <= op_illegal; - else - r_alu_illegal <= 1'b0; - - assign alu_illegal = (r_alu_illegal); - end else begin : SET_ALU_ILLEGAL - assign alu_illegal = op_illegal; - end endgenerate - - initial r_alu_pc_valid = 1'b0; - initial mem_pc_valid = 1'b0; - always @(posedge i_clk) - if (clear_pipeline) - r_alu_pc_valid <= 1'b0; - else if ((adf_ce_unconditional)&&(!op_phase)) - r_alu_pc_valid <= 1'b1; - else if (((!alu_busy)&&(!div_busy)&&(!fpu_busy))||(clear_pipeline)) - r_alu_pc_valid <= 1'b0; - - assign alu_pc_valid = (r_alu_pc_valid) - &&((!alu_busy)&&(!div_busy)&&(!fpu_busy)); - - always @(posedge i_clk) - if (i_reset) - mem_pc_valid <= 1'b0; - else - mem_pc_valid <= (mem_ce); - - // Bus lock logic - //{{{ - generate - if ((OPT_PIPELINED)&&(!OPT_LOCK)) - begin : BUSLOCK - reg r_prelock_stall; - - initial r_prelock_stall = 1'b0; - always @(posedge i_clk) - if (clear_pipeline) - r_prelock_stall <= 1'b0; - else if ((op_valid)&&(op_lock)&&(op_ce)) - r_prelock_stall <= 1'b1; - else if ((op_valid)&&(dcd_valid)&&(pf_valid)) - r_prelock_stall <= 1'b0; - - assign prelock_stall = r_prelock_stall; - - reg r_prelock_primed; - initial r_prelock_primed = 1'b0; - always @(posedge i_clk) - if (clear_pipeline) - r_prelock_primed <= 1'b0; - else if (r_prelock_stall) - r_prelock_primed <= 1'b1; - else if ((adf_ce_unconditional)||(mem_ce)) - r_prelock_primed <= 1'b0; - - reg [1:0] r_bus_lock; - initial r_bus_lock = 2'b00; - always @(posedge i_clk) - if (clear_pipeline) - r_bus_lock <= 2'b00; - else if ((op_valid)&&((adf_ce_unconditional)||(mem_ce))) - begin - if (r_prelock_primed) - r_bus_lock <= 2'b10; - else if (r_bus_lock != 2'h0) - r_bus_lock <= r_bus_lock + 2'b11; - end - - assign bus_lock = |r_bus_lock; - end else begin - assign prelock_stall = 1'b0; - assign bus_lock = 1'b0; - end endgenerate - //}}} - - // Memory interface - //{{{ - generate if (OPT_DCACHE) - begin : MEM_DCACHE - - dcache #(.LGCACHELEN(OPT_LGDCACHE), .ADDRESS_WIDTH(AW), - .LGNLINES(OPT_LGDCACHE-3), .OPT_LOCAL_BUS(WITH_LOCAL_BUS), - .OPT_PIPE(OPT_MEMPIPE), - .OPT_LOCK(OPT_LOCK) - ) docache(i_clk, i_reset, w_clear_dcache, - ///{{{ - (mem_ce)&&(set_cond), bus_lock, - (op_opn[2:0]), op_Bv, op_Av, op_R, - mem_busy, mem_pipe_stalled, - mem_valid, bus_err, mem_wreg, mem_result, - mem_cyc_gbl, mem_cyc_lcl, - mem_stb_gbl, mem_stb_lcl, - mem_we, mem_addr, mem_data, mem_sel, - mem_stall, mem_ack, mem_err, i_wb_data - // , o_dcache_debug - ); - ///}}} - end else begin : NO_CACHE - if (OPT_PIPELINED_BUS_ACCESS) - begin : MEM - - pipemem #(.ADDRESS_WIDTH(AW), - .IMPLEMENT_LOCK(OPT_LOCK), - .WITH_LOCAL_BUS(WITH_LOCAL_BUS) - ) domem(i_clk,i_reset, - ///{{{ - (mem_ce)&&(set_cond), bus_lock, - (op_opn[2:0]), op_Bv, op_Av, op_R, - mem_busy, mem_pipe_stalled, - mem_valid, bus_err, mem_wreg, mem_result, - mem_cyc_gbl, mem_cyc_lcl, - mem_stb_gbl, mem_stb_lcl, - mem_we, mem_addr, mem_data, mem_sel, - mem_stall, mem_ack, mem_err, i_wb_data - ); - //}}} - end else begin : MEM - - memops #(.ADDRESS_WIDTH(AW), - .IMPLEMENT_LOCK(OPT_LOCK), - .WITH_LOCAL_BUS(WITH_LOCAL_BUS) - ) domem(i_clk,i_reset, - //{{{ - (mem_ce)&&(set_cond), bus_lock, - (op_opn[2:0]), op_Bv, op_Av, op_R, - mem_busy, - mem_valid, bus_err, mem_wreg, mem_result, - mem_cyc_gbl, mem_cyc_lcl, - mem_stb_gbl, mem_stb_lcl, - mem_we, mem_addr, mem_data, mem_sel, - mem_stall, mem_ack, mem_err, i_wb_data - ); - //}}} - assign mem_pipe_stalled = 1'b0; - end end endgenerate - - assign mem_rdbusy = (mem_busy)&&((!OPT_PIPELINED)||(!mem_we)); - - // Either the prefetch or the instruction gets the memory bus, but - // never both. - generate if (OPT_PIPELINED) - begin : PRIORITY_DATA - - wbdblpriarb #(.DW(32),.AW(AW) - ) pformem(i_clk, i_reset, - //{{{ - // Memory access to the arbiter, priority position - mem_cyc_gbl, mem_cyc_lcl, mem_stb_gbl, mem_stb_lcl, - mem_we, mem_addr, mem_data, mem_sel, - mem_stall, mem_ack, mem_err, - // Prefetch access to the arbiter - // - // At a first glance, we might want something like: - // - // pf_cyc, 1'b0, pf_stb, 1'b0, pf_we, pf_addr, pf_data, 4'hf, - // - // However, we know that the prefetch will not generate any - // writes. Therefore, the write specific lines (mem_data and - // mem_sel) can be shared with the memory in order to ease - // timing and LUT usage. - pf_cyc,1'b0,pf_stb, 1'b0, pf_we, pf_addr, mem_data, mem_sel, - pf_stall, pf_ack, pf_err, - // Common wires, in and out, of the arbiter - o_wb_gbl_cyc, o_wb_lcl_cyc, o_wb_gbl_stb, o_wb_lcl_stb, - o_wb_we, o_wb_addr, o_wb_data, o_wb_sel, - i_wb_stall, i_wb_ack, i_wb_err - ); - //}}} - - end else begin : PRIORITY_PREFETCH - - wbdblpriarb #(.DW(32),.AW(AW) - ) pformem(i_clk, i_reset, - //{{{ - // Prefetch access to the arbiter, priority position - // - // At a first glance, we might want something like: - // - // pf_cyc, 1'b0, pf_stb, 1'b0, pf_we, pf_addr, pf_data, 4'hf, - // - // However, we know that the prefetch will not generate any - // writes. Therefore, the write specific lines (mem_data and - // mem_sel) can be shared with the memory in order to ease - // timing and LUT usage. - pf_cyc,1'b0,pf_stb, 1'b0, pf_we, pf_addr, mem_data, mem_sel, - pf_stall, pf_ack, pf_err, - // Memory access to the arbiter - mem_cyc_gbl, mem_cyc_lcl, mem_stb_gbl, mem_stb_lcl, - mem_we, mem_addr, mem_data, mem_sel, - mem_stall, mem_ack, mem_err, - // Common wires, in and out, of the arbiter - o_wb_gbl_cyc, o_wb_lcl_cyc, o_wb_gbl_stb, o_wb_lcl_stb, - o_wb_we, o_wb_addr, o_wb_data, o_wb_sel, - i_wb_stall, i_wb_ack, i_wb_err - ); - //}}} - end endgenerate - - //}}} - //}}} - - - // - // - // - // - // - // - // - // - // PIPELINE STAGE #5 :: Write-back results - // - //{{{ - // - // This stage is not allowed to stall. If results are ready to be - // written back, they are written back at all cost. Sleepy CPU's - // won't prevent write back, nor debug modes, halting the CPU, nor - // anything else. Indeed, the (master_ce) bit is only as relevant - // as knowinig something is available for writeback. - - // - // Write back to our generic register set ... - // When shall we write back? On one of two conditions - // Note that the flags needed to be checked before issuing the - // bus instruction, so they don't need to be checked here. - // Further, alu_wR includes (set_cond), so we don't need to - // check for that here either. - assign wr_discard = (clear_pipeline || alu_illegal); - always @(*) - case(wr_index) - 3'b000: wr_reg_ce = dbgv; - 3'b001: wr_reg_ce = mem_valid; - //3'b010: wr_reg_ce = (!wr_discard)&&(alu_wR && alu_valid); - 3'b011: wr_reg_ce = (!wr_discard)&&(div_valid)&&(!div_error); - 3'b100: wr_reg_ce = (!wr_discard)&&(fpu_valid)&&(!fpu_error); - default: wr_reg_ce = (!wr_discard)&&(alu_wR && alu_valid); - endcase - - // Which register shall be written? - // COULD SIMPLIFY THIS: by adding three bits to these registers, - // One or PC, one for CC, and one for GIE match - // Note that the alu_reg is the register to write on a divide or - // FPU operation. - generate if (OPT_NO_USERMODE) - begin - assign wr_reg_id[3:0] = (mem_valid) - ? mem_wreg[3:0] : alu_reg[3:0]; - - assign wr_reg_id[4] = 1'b0; - end else begin - assign wr_reg_id = (mem_valid) ? mem_wreg : alu_reg; - end endgenerate - - // Are we writing to the CC register? - assign wr_write_cc = (wr_reg_id[3:0] == `CPU_CC_REG); - assign wr_write_scc = (wr_reg_id[4:0] == {1'b0, `CPU_CC_REG}); - assign wr_write_ucc = (wr_reg_id[4:0] == {1'b1, `CPU_CC_REG}); - // Are we writing to the PC? - assign wr_write_pc = (wr_reg_id[3:0] == `CPU_PC_REG); - - // What value to write? - always @(*) - case(wr_index) - 3'b000: wr_gpreg_vl = dbg_val; - 3'b001: wr_gpreg_vl = mem_result; - // 3'b010: wr_gpreg_vl = alu_result; - 3'b011: wr_gpreg_vl = div_result; - 3'b100: wr_gpreg_vl = fpu_result; - default: wr_gpreg_vl = alu_result; - endcase - - always @(*) - case(wr_index[1:0]) - 2'b00: wr_spreg_vl = dbg_val; - 2'b01: wr_spreg_vl = mem_result; - // 3'b010: wr_gpreg_vl = alu_result; - default: wr_spreg_vl = alu_result; - endcase - - generate if (OPT_NO_USERMODE) - begin : SET_REGISTERS - - always @(posedge i_clk) - if (wr_reg_ce) - regset[{1'b0,wr_reg_id[3:0]}] <= wr_gpreg_vl; - - end else begin : SET_REGISTERS - - always @(posedge i_clk) - if (wr_reg_ce) - regset[wr_reg_id] <= wr_gpreg_vl; - - end endgenerate - - - // - // Write back to the condition codes/flags register ... - // When shall we write to our flags register? alu_wF already - // includes the set condition ... - // assign wr_flags_ce = (alu_wF)&&((alu_valid) - // ||(div_valid)||(fpu_valid)) - // &&(!clear_pipeline)&&(!alu_illegal); - always @(*) - begin - wr_flags_ce = 0; - if (alu_wF && !wr_discard) - case(wr_index) - // 3'b000: wr_flags_ce = 0; // Debug - // 3'b001: wr_flags_ce = 0; // Memory - 3'b010: wr_flags_ce = alu_valid; // ALU - 3'b011: wr_flags_ce = div_valid && !div_error; // DIV - 3'b100: wr_flags_ce = fpu_valid && !fpu_error; // FPU - default: wr_flags_ce = 0; - endcase - end - - always @(*) - begin - wr_flags = 0; - case(wr_index) - 3'b010: wr_flags = alu_flags; - 3'b011: wr_flags = div_flags; - 3'b100: wr_flags = fpu_flags; - default: wr_flags = 0; - endcase - end - - assign w_uflags = { 2'b00, uhalt_phase, ufpu_err_flag, - udiv_err_flag, ubus_err_flag, trap, ill_err_u, - ubreak, step, 1'b1, sleep, - ((wr_flags_ce)&&(alu_gie))?wr_flags:flags }; - assign w_iflags = { 2'b00, ihalt_phase, ifpu_err_flag, - idiv_err_flag, ibus_err_flag, trap, ill_err_i, - break_en, 1'b0, 1'b0, sleep, - ((wr_flags_ce)&&(!alu_gie))?wr_flags:iflags }; - - - // What value to write? - always @(posedge i_clk) - // If explicitly writing the register itself - if ((wr_reg_ce)&&(wr_write_ucc)) - flags <= wr_gpreg_vl[3:0]; - // Otherwise if we're setting the flags from an ALU operation - else if ((wr_flags_ce)&&(alu_gie)) - flags <= (div_valid)?div_flags:((fpu_valid)?fpu_flags - : alu_flags); - - always @(posedge i_clk) - if ((wr_reg_ce)&&(wr_write_scc)) - iflags <= wr_gpreg_vl[3:0]; - else if ((wr_flags_ce)&&(!alu_gie)) - iflags <= (div_valid)?div_flags:((fpu_valid)?fpu_flags - : alu_flags); - - // The 'break' enable bit. This bit can only be set from supervisor - // mode. It control what the CPU does upon encountering a break - // instruction. - // - // The goal, upon encountering a break is that the CPU should stop and - // not execute the break instruction, choosing instead to enter into - // either interrupt mode or halt first. - // if ((break_en) AND (break_instruction)) // user mode or not - // HALT CPU - // else if (break_instruction) // only in user mode - // set an interrupt flag, set the user break bit, - // go to supervisor mode, allow supervisor to step the CPU. - // Upon a CPU halt, any break condition will be reset. The - // external debugger will then need to deal with whatever - // condition has taken place. - initial break_en = 1'b0; - always @(posedge i_clk) - if ((i_reset)||(i_halt)) - break_en <= 1'b0; - else if ((wr_reg_ce)&&(wr_write_scc)) - break_en <= wr_spreg_vl[`CPU_BREAK_BIT]; - - generate if (OPT_PIPELINED) - begin : GEN_PENDING_BREAK - reg r_break_pending; - - initial r_break_pending = 1'b0; - always @(posedge i_clk) - if ((clear_pipeline)||(!op_valid)) - r_break_pending <= 1'b0; - else if ((op_break)&&(!r_break_pending)) - r_break_pending <= (!alu_busy)&&(!div_busy) - &&(!fpu_busy)&&(!mem_busy) - &&(!wr_reg_ce); - // else - // r_break_pending <= 1'b0; - - assign break_pending = r_break_pending; - end else begin - - assign break_pending = op_break; - end endgenerate - - - assign o_break = ((break_en)||(!op_gie))&&(break_pending) - &&(!clear_pipeline) - ||(ill_err_i) - ||((!alu_gie)&&(bus_err)) - ||((!alu_gie)&&(div_error)) - ||((!alu_gie)&&(fpu_error)) - ||((!alu_gie)&&(alu_illegal)&&(!clear_pipeline)); - - // The sleep register. Setting the sleep register causes the CPU to - // sleep until the next interrupt. Setting the sleep register within - // interrupt mode causes the processor to halt until a reset. This is - // a panic/fault halt. The trick is that you cannot be allowed to - // set the sleep bit and switch to supervisor mode in the same - // instruction: users are not allowed to halt the CPU. - initial sleep = 1'b0; - generate if (OPT_NO_USERMODE) - begin : GEN_NO_USERMODE_SLEEP - reg r_sleep_is_halt; - initial r_sleep_is_halt = 1'b0; - always @(posedge i_clk) - if (i_reset) - r_sleep_is_halt <= 1'b0; - else if ((wr_reg_ce)&&(wr_write_cc) - &&(wr_spreg_vl[`CPU_SLEEP_BIT]) - &&(!wr_spreg_vl[`CPU_GIE_BIT])) - r_sleep_is_halt <= 1'b1; - - // Trying to switch to user mode, either via a WAIT or an RTU - // instruction will cause the CPU to sleep until an interrupt, - // in the NO-USERMODE build. - always @(posedge i_clk) - if ((i_reset)||((i_interrupt)&&(!r_sleep_is_halt))) - sleep <= 1'b0; - else if ((wr_reg_ce)&&(wr_write_cc) - &&(wr_spreg_vl[`CPU_GIE_BIT])) - sleep <= 1'b1; - - end else begin : GEN_SLEEP - - always @(posedge i_clk) - if ((i_reset)||(w_switch_to_interrupt)) - sleep <= 1'b0; - else if ((wr_reg_ce)&&(wr_write_cc)&&(!alu_gie)) - // In supervisor mode, we have no protections. - // The supervisor can set the sleep bit however - // he wants. Well ... not quite. Switching to - // user mode and sleep mode shouold only be - // possible if the interrupt flag isn't set. - // Thus: if (i_interrupt) - // &&(wr_spreg_vl[GIE]) - // don't set the sleep bit - // otherwise however it would o.w. be set - sleep <= (wr_spreg_vl[`CPU_SLEEP_BIT]) - &&((!i_interrupt) - ||(!wr_spreg_vl[`CPU_GIE_BIT])); - else if ((wr_reg_ce)&&(wr_write_cc) - &&(wr_spreg_vl[`CPU_GIE_BIT])) - // In user mode, however, you can only set the - // sleep mode while remaining in user mode. - // You can't switch to sleep mode *and* - // supervisor mode at the same time, lest you - // halt the CPU. - sleep <= wr_spreg_vl[`CPU_SLEEP_BIT]; - end endgenerate - - always @(posedge i_clk) - if (i_reset) - step <= 1'b0; - else if ((wr_reg_ce)&&(!alu_gie)&&(wr_write_ucc)) - step <= wr_spreg_vl[`CPU_STEP_BIT]; - - // The GIE register. Only interrupts can disable the interrupt register - generate if (OPT_NO_USERMODE) - begin - - assign w_switch_to_interrupt = 1'b0; - assign w_release_from_interrupt = 1'b0; - - end else begin : GEN_PENDING_INTERRUPT - - reg r_pending_interrupt; - - always @(posedge i_clk) - if (i_reset) - r_pending_interrupt <= 1'b0; - else if ((clear_pipeline)||(w_switch_to_interrupt)||(!gie)) - r_pending_interrupt <= 1'b0; - else if (i_interrupt) - r_pending_interrupt <= 1'b1; - else if (adf_ce_unconditional) - begin - if ((op_illegal)||(step)||(break_pending)) - r_pending_interrupt <= 1'b1; - end else if (break_pending) - r_pending_interrupt <= 1'b1; - else if ((mem_ce)&&(step)) - r_pending_interrupt <= 1'b1; - - assign pending_interrupt = r_pending_interrupt; - - - assign w_switch_to_interrupt = (gie)&&( - // On interrupt (obviously) - ((pending_interrupt) - &&(!alu_phase)&&(!bus_lock)&&(!mem_busy)) - // - // On division by zero. If the divide isn't - // implemented, div_valid and div_error will be short - // circuited and that logic will be bypassed - ||(div_error) - // - // Same thing on a floating point error. Note that - // fpu_error must *never* be set unless fpu_valid is - // also set as well, else this will fail. - ||(fpu_error) - // - // - ||(bus_err) - // - // If we write to the CC register - ||((wr_reg_ce)&&(!wr_spreg_vl[`CPU_GIE_BIT]) - &&(wr_reg_id[4])&&(wr_write_cc)) - ); - assign w_release_from_interrupt = (!gie)&&(!i_interrupt) - // Then if we write the sCC register - &&(((wr_reg_ce)&&(wr_spreg_vl[`CPU_GIE_BIT]) - &&(wr_write_scc)) - ); - end endgenerate - - generate if (OPT_NO_USERMODE) - begin - - assign gie = 1'b0; - - end else begin : SET_GIE - - reg r_gie; - - initial r_gie = 1'b0; - always @(posedge i_clk) - if (i_reset) - r_gie <= 1'b0; - else if (w_switch_to_interrupt) - r_gie <= 1'b0; - else if (w_release_from_interrupt) - r_gie <= 1'b1; - - assign gie = r_gie; - end endgenerate - - generate if (OPT_NO_USERMODE) - begin - - assign trap = 1'b0; - assign ubreak = 1'b0; - - end else begin : SET_TRAP_N_UBREAK - - reg r_trap; - - initial r_trap = 1'b0; - always @(posedge i_clk) - if ((i_reset)||(w_release_from_interrupt)) - r_trap <= 1'b0; - else if ((alu_gie)&&(wr_reg_ce)&&(!wr_spreg_vl[`CPU_GIE_BIT]) - &&(wr_write_ucc)) // &&(wr_reg_id[4]) implied - r_trap <= 1'b1; - else if ((wr_reg_ce)&&(wr_write_ucc)&&(!alu_gie)) - r_trap <= (r_trap)&&(wr_spreg_vl[`CPU_TRAP_BIT]); - - reg r_ubreak; - - initial r_ubreak = 1'b0; - always @(posedge i_clk) - if ((i_reset)||(w_release_from_interrupt)) - r_ubreak <= 1'b0; - else if ((op_gie)&&(break_pending)&&(w_switch_to_interrupt)) - r_ubreak <= 1'b1; - else if (((!alu_gie)||(dbgv))&&(wr_reg_ce)&&(wr_write_ucc)) - r_ubreak <= (ubreak)&&(wr_spreg_vl[`CPU_BREAK_BIT]); - - assign trap = r_trap; - assign ubreak = r_ubreak; - - end endgenerate - - - initial ill_err_i = 1'b0; - always @(posedge i_clk) - if (i_reset) - ill_err_i <= 1'b0; - // Only the debug interface can clear this bit - else if ((dbgv)&&(wr_write_scc)) - ill_err_i <= (ill_err_i)&&(wr_spreg_vl[`CPU_ILL_BIT]); - else if ((alu_illegal)&&(!alu_gie)&&(!clear_pipeline)) - ill_err_i <= 1'b1; - - generate if (OPT_NO_USERMODE) - begin - - assign ill_err_u = 1'b0; - - end else begin : SET_USER_ILLEGAL_INSN - - reg r_ill_err_u; - - initial r_ill_err_u = 1'b0; - always @(posedge i_clk) - // The bit is automatically cleared on release from interrupt - // or reset - if ((i_reset)||(w_release_from_interrupt)) - r_ill_err_u <= 1'b0; - // If the supervisor (or debugger) writes to this - // register, clearing the bit, then clear it - else if (((!alu_gie)||(dbgv))&&(wr_reg_ce)&&(wr_write_ucc)) - r_ill_err_u <=((ill_err_u)&&(wr_spreg_vl[`CPU_ILL_BIT])); - else if ((alu_illegal)&&(alu_gie)&&(!clear_pipeline)) - r_ill_err_u <= 1'b1; - - assign ill_err_u = r_ill_err_u; - - end endgenerate - - // Supervisor/interrupt bus error flag -- this will crash the CPU if - // ever set. - initial ibus_err_flag = 1'b0; - always @(posedge i_clk) - if (i_reset) - ibus_err_flag <= 1'b0; - else if ((dbgv)&&(wr_write_scc)) - ibus_err_flag <= (ibus_err_flag)&&(wr_spreg_vl[`CPU_BUSERR_BIT]); - else if ((bus_err)&&(!alu_gie)) - ibus_err_flag <= 1'b1; - // User bus error flag -- if ever set, it will cause an interrupt to - // supervisor mode. - generate if (OPT_NO_USERMODE) - begin - - assign ubus_err_flag = 1'b0; - - end else begin : SET_USER_BUSERR - - reg r_ubus_err_flag; - - initial r_ubus_err_flag = 1'b0; - always @(posedge i_clk) - if ((i_reset)||(w_release_from_interrupt)) - r_ubus_err_flag <= 1'b0; - else if (((!alu_gie)||(dbgv))&&(wr_reg_ce)&&(wr_write_ucc)) - r_ubus_err_flag <= (ubus_err_flag)&&(wr_spreg_vl[`CPU_BUSERR_BIT]); - else if ((bus_err)&&(alu_gie)) - r_ubus_err_flag <= 1'b1; - - assign ubus_err_flag = r_ubus_err_flag; - end endgenerate - - generate if (IMPLEMENT_DIVIDE != 0) - begin : DIVERR - reg r_idiv_err_flag, r_udiv_err_flag; - - // Supervisor/interrupt divide (by zero) error flag -- this will - // crash the CPU if ever set. This bit is thus available for us - // to be able to tell if/why the CPU crashed. - initial r_idiv_err_flag = 1'b0; - always @(posedge i_clk) - if (i_reset) - r_idiv_err_flag <= 1'b0; - else if ((dbgv)&&(wr_write_scc)) - r_idiv_err_flag <= (r_idiv_err_flag)&&(wr_spreg_vl[`CPU_DIVERR_BIT]); - else if ((div_error)&&(!alu_gie)) - r_idiv_err_flag <= 1'b1; - - assign idiv_err_flag = r_idiv_err_flag; - - if (OPT_NO_USERMODE) - begin - assign udiv_err_flag = 1'b0; - end else begin - - // User divide (by zero) error flag -- if ever set, it will - // cause a sudden switch interrupt to supervisor mode. - initial r_udiv_err_flag = 1'b0; - always @(posedge i_clk) - if ((i_reset)||(w_release_from_interrupt)) - r_udiv_err_flag <= 1'b0; - else if (((!alu_gie)||(dbgv))&&(wr_reg_ce) - &&(wr_write_ucc)) - r_udiv_err_flag <= (r_udiv_err_flag)&&(wr_spreg_vl[`CPU_DIVERR_BIT]); - else if ((div_error)&&(alu_gie)) - r_udiv_err_flag <= 1'b1; - - assign udiv_err_flag = r_udiv_err_flag; - end - end else begin - assign idiv_err_flag = 1'b0; - assign udiv_err_flag = 1'b0; - end endgenerate - - generate if (IMPLEMENT_FPU !=0) - begin : FPUERR - // Supervisor/interrupt floating point error flag -- this will - // crash the CPU if ever set. - reg r_ifpu_err_flag, r_ufpu_err_flag; - initial r_ifpu_err_flag = 1'b0; - always @(posedge i_clk) - if (i_reset) - r_ifpu_err_flag <= 1'b0; - else if ((dbgv)&&(wr_write_scc)) - r_ifpu_err_flag <= (r_ifpu_err_flag)&&(wr_spreg_vl[`CPU_FPUERR_BIT]); - else if ((fpu_error)&&(fpu_valid)&&(!alu_gie)) - r_ifpu_err_flag <= 1'b1; - // User floating point error flag -- if ever set, it will cause - // a sudden switch interrupt to supervisor mode. - initial r_ufpu_err_flag = 1'b0; - always @(posedge i_clk) - if ((i_reset)&&(w_release_from_interrupt)) - r_ufpu_err_flag <= 1'b0; - else if (((!alu_gie)||(dbgv))&&(wr_reg_ce) - &&(wr_write_ucc)) - r_ufpu_err_flag <= (r_ufpu_err_flag)&&(wr_spreg_vl[`CPU_FPUERR_BIT]); - else if ((fpu_error)&&(alu_gie)&&(fpu_valid)) - r_ufpu_err_flag <= 1'b1; - - assign ifpu_err_flag = r_ifpu_err_flag; - assign ufpu_err_flag = r_ufpu_err_flag; - end else begin - assign ifpu_err_flag = 1'b0; - assign ufpu_err_flag = 1'b0; - end endgenerate - - generate if (OPT_CIS) - begin : GEN_IHALT_PHASE - reg r_ihalt_phase; - - initial r_ihalt_phase = 0; - always @(posedge i_clk) - if (i_reset) - r_ihalt_phase <= 1'b0; - else if ((!alu_gie)&&(alu_pc_valid)&&(!clear_pipeline)) - r_ihalt_phase <= alu_phase; - - assign ihalt_phase = r_ihalt_phase; - end else begin : GEN_IHALT_PHASE - - assign ihalt_phase = 1'b0; - - end endgenerate - - generate if ((!OPT_CIS) || (OPT_NO_USERMODE)) - begin : GEN_UHALT_PHASE - - assign uhalt_phase = 1'b0; - - end else begin : GEN_UHALT_PHASE - - reg r_uhalt_phase; - - initial r_uhalt_phase = 1'b0; - always @(posedge i_clk) - if ((i_reset)||(w_release_from_interrupt)) - r_uhalt_phase <= 1'b0; - else if ((alu_gie)&&(alu_pc_valid)) - r_uhalt_phase <= alu_phase; - else if ((!alu_gie)&&(wr_reg_ce)&&(wr_write_pc) - &&(wr_reg_id[4])) - r_uhalt_phase <= wr_spreg_vl[1]; - - assign uhalt_phase = r_uhalt_phase; - - end endgenerate - - // - // Write backs to the PC register, and general increments of it - // We support two: upc and ipc. If the instruction is normal, - // we increment upc, if interrupt level we increment ipc. If - // the instruction writes the PC, we write whichever PC is appropriate. - // - // Do we need to all our partial results from the pipeline? - // What happens when the pipeline has gie and !gie instructions within - // it? Do we clear both? What if a gie instruction tries to clear - // a non-gie instruction? - generate if (OPT_NO_USERMODE) - begin - - assign upc = {(AW+2){1'b0}}; - - end else begin : SET_USER_PC - - reg [(AW+1):0] r_upc; - - always @(posedge i_clk) - if ((wr_reg_ce)&&(wr_reg_id[4])&&(wr_write_pc)) - r_upc <= { wr_spreg_vl[(AW+1):2], 2'b00 }; - else if ((alu_gie)&& - (((alu_pc_valid)&&(!clear_pipeline)&&(!alu_illegal)) - ||(mem_pc_valid))) - r_upc <= alu_pc; - assign upc = r_upc; - end endgenerate - - initial ipc = { RESET_BUS_ADDRESS, 2'b00 }; - always @(posedge i_clk) - if (i_reset) - ipc <= { RESET_BUS_ADDRESS, 2'b00 }; - else if ((wr_reg_ce)&&(!wr_reg_id[4])&&(wr_write_pc)) - ipc <= { wr_spreg_vl[(AW+1):2], 2'b00 }; - else if ((!alu_gie)&&(!alu_phase)&& - (((alu_pc_valid)&&(!clear_pipeline)&&(!alu_illegal)) - ||(mem_pc_valid))) - ipc <= alu_pc; - - reg pfpcset; - reg [2:0] pfpcsrc; - always @(*) - begin - pfpcset = 0; - pfpcsrc = 0; - if (i_reset) - begin - pfpcsrc = 0; - pfpcset = 1; - end else if ((dbg_clear_pipe)&&(wr_reg_ce)&&(wr_write_pc)) - begin - pfpcsrc = 1; // spreg - pfpcset = 1; - end else if ((w_switch_to_interrupt) - ||((!gie)&&((w_clear_icache)||(dbg_clear_pipe)))) - begin - pfpcsrc = 2; // ipc - pfpcset = 1; - end else if ((w_release_from_interrupt)||((gie)&&((w_clear_icache)||(dbg_clear_pipe)))) - begin - pfpcsrc = 3; // upc - pfpcset = 1; - end else if ((wr_reg_ce)&&(wr_reg_id[4] == gie)&&(wr_write_pc)) - begin - pfpcsrc = 1; // spreg - pfpcset = 1; - end else if ((dcd_early_branch_stb)&&(!clear_pipeline)) - begin - pfpcsrc = 4; // branch - pfpcset = 1; - end else if ((new_pc)||((!pf_stalled)&&(pf_valid))) - begin - pfpcsrc = 5; // PC increment - pfpcset = 1; - end - end - - initial pf_pc = { RESET_BUS_ADDRESS, 2'b00 }; - always @(posedge i_clk) - if (pfpcset) - case(pfpcsrc) - 3'b000: pf_pc <= { RESET_BUS_ADDRESS, 2'b00 }; - 3'b001: pf_pc <= { wr_spreg_vl[(AW+1):2], 2'b00 }; - 3'b010: pf_pc <= { ipc[(AW+1):2], 2'b00 }; - 3'b011: pf_pc <= { upc[(AW+1):2], 2'b00 }; - 3'b100: pf_pc <= { dcd_branch_pc[AW+1:2] + 1'b1, 2'b00 }; - 3'b101: pf_pc <= { pf_pc[(AW+1):2] + 1'b1, 2'b00 }; - default: pf_pc <= { RESET_BUS_ADDRESS, 2'b00 }; - endcase - - initial last_write_to_cc = 1'b0; - always @(posedge i_clk) - if (i_reset) - last_write_to_cc <= 1'b0; - else - last_write_to_cc <= (wr_reg_ce)&&(wr_write_cc); - assign cc_write_hold = (wr_reg_ce)&&(wr_write_cc)||(last_write_to_cc); - - // If we aren't pipelined, or equivalently if we have no cache, these - // instructions will get quietly (or not so quietly) ignored by the - // optimizer. - initial r_clear_icache = 1'b1; - always @(posedge i_clk) - if (i_reset) - r_clear_icache <= 1'b0; - else if ((r_halted)&&(i_clear_pf_cache)) - r_clear_icache <= 1'b1; - else if ((wr_reg_ce)&&(wr_write_scc)) - r_clear_icache <= wr_spreg_vl[`CPU_CLRICACHE_BIT]; - else - r_clear_icache <= 1'b0; - assign w_clear_icache = r_clear_icache; - - generate if (OPT_LGDCACHE > 0) - begin : CLEAR_DCACHE - initial r_clear_dcache = 1'b1; - always @(posedge i_clk) - if (i_reset) - r_clear_dcache <= 1'b0; - else if ((r_halted)&&(i_clear_pf_cache)) - r_clear_dcache <= 1'b1; - else if ((wr_reg_ce)&&(wr_write_scc)) - r_clear_dcache <= wr_spreg_vl[`CPU_CLRDCACHE_BIT]; - else - r_clear_dcache <= 1'b0; - end else begin : NOCLEAR_DCACHE - always @(*) - r_clear_dcache = 1'b0; - end endgenerate - - assign w_clear_dcache = r_clear_dcache; - - initial new_pc = 1'b1; - always @(posedge i_clk) - if ((i_reset)||(w_clear_icache)||(dbg_clear_pipe)) - new_pc <= 1'b1; - else if (w_switch_to_interrupt) - new_pc <= 1'b1; - else if (w_release_from_interrupt) - new_pc <= 1'b1; - // else if ((wr_reg_ce)&&(wr_reg_id[4] == gie)&&(wr_write_pc)) - // Can't check for *this* PC here, since a user PC might be - // loaded in the pipeline and hence rewritten. Thus, while - // I hate to do it, we'll need to clear the pipeline on any - // PC write - else if ((wr_reg_ce)&&(alu_gie == wr_reg_id[4])&&(wr_write_pc)) - new_pc <= 1'b1; - else - new_pc <= 1'b0; - - // - // The debug write-back interface - //{{{ - reg [31:0] debug_pc; - generate if (OPT_NO_USERMODE) - begin - - always @(*) - begin - debug_pc = 0; - debug_pc[(AW+1):0] = { ipc, 2'b00 }; - end - - end else begin - - always @(*) - begin - debug_pc = 0; - if (i_dbg_reg[4]) - // User mode - debug_pc[(AW+1):0] - = { upc[(AW+1):2], uhalt_phase, 1'b0 }; - else - // Supervisor mode - debug_pc[(AW+1):0] - = { ipc[(AW+1):2], ihalt_phase, 1'b0 }; - end - - end endgenerate - - generate if (OPT_NO_USERMODE) - begin : NO_USER_SETDBG - - always @(posedge i_clk) - begin - o_dbg_reg <= regset[i_dbg_reg[3:0]]; - if (i_dbg_reg[3:0] == `CPU_PC_REG) - o_dbg_reg <= debug_pc; - else if (i_dbg_reg[3:0] == `CPU_CC_REG) - begin - o_dbg_reg[15:0] <= w_iflags; - o_dbg_reg[31:23] <= w_cpu_info; - o_dbg_reg[`CPU_GIE_BIT] <= gie; - end - end - end else begin : SETDBG - -`ifdef NO_DISTRIBUTED_RAM - reg [31:0] pre_dbg_reg; - always @(posedge i_clk) - pre_dbg_reg <= regset[i_dbg_reg]; - - always @(posedge i_clk) - begin - o_dbg_reg <= pre_dbg_reg; - if (i_dbg_reg[3:0] == `CPU_PC_REG) - o_dbg_reg <= debug_pc; - else if (i_dbg_reg[3:0] == `CPU_CC_REG) - begin - o_dbg_reg[15:0] <= (i_dbg_reg[4]) - ? w_uflags : w_iflags; - o_dbg_reg[31:23] <= w_cpu_info; - o_dbg_reg[`CPU_GIE_BIT] <= gie; - end - end - -`else - always @(posedge i_clk) - begin - o_dbg_reg <= regset[i_dbg_reg]; - if (i_dbg_reg[3:0] == `CPU_PC_REG) - o_dbg_reg <= debug_pc; - else if (i_dbg_reg[3:0] == `CPU_CC_REG) - begin - o_dbg_reg[15:0] <= (i_dbg_reg[4]) - ? w_uflags : w_iflags; - o_dbg_reg[31:23] <= w_cpu_info; - o_dbg_reg[`CPU_GIE_BIT] <= gie; - end - end -`endif - - end endgenerate - - always @(posedge i_clk) - o_dbg_cc <= { o_break, bus_err, gie, sleep }; - - generate if (OPT_PIPELINED) - begin - always @(posedge i_clk) - r_halted <= (i_halt)&&(!alu_phase)&&(!bus_lock)&&( - // To be halted, any long lasting instruction - // must be completed. - (!pf_cyc)&&(!mem_busy)&&(!alu_busy) - &&(!div_busy)&&(!fpu_busy) - // Operations must either be valid, or illegal - &&((op_valid)||(i_reset)||(dcd_illegal)) - // Decode stage must be either valid, in reset, - // or producing an illelgal instruction - &&((dcd_valid)||(i_reset)||(pf_illegal))); - end else begin - - always @(posedge i_clk) - r_halted <= (i_halt)&&(!alu_phase) - // To be halted, any long lasting instruction - // must be completed. - &&(!pf_cyc)&&(!mem_busy)&&(!alu_busy) - &&(!div_busy)&&(!fpu_busy); - end endgenerate -`ifdef NO_DISTRIBUTED_RAM - reg r_dbg_stall; - initial r_dbg_stall = 1'b1; - - always @(posedge i_clk) - if (i_reset) - r_dbg_stall <= 1'b1; - else if (!r_halted) - r_dbg_stall <= 1'b1; - else - r_dbg_stall <= (!i_dbg_we)||(!r_dbg_stall); - - assign o_dbg_stall = !r_halted; -`else - assign o_dbg_stall = !r_halted; -`endif - //}}} - - //}}} - - // - // - // Produce accounting outputs: Account for any CPU stalls, so we can - // later evaluate how well we are doing. - // - // - assign o_op_stall = (master_ce)&&(op_stall); - assign o_pf_stall = (master_ce)&&(!pf_valid); - assign o_i_count = (alu_pc_valid)&&(!clear_pipeline); - -`ifdef DEBUG_SCOPE - //{{{ - - reg debug_trigger; - initial debug_trigger = 1'b0; - always @(posedge i_clk) - debug_trigger <= (!i_halt)&&(o_break); - - wire [31:0] debug_flags; - assign debug_flags = { debug_trigger, 3'b101, - master_ce, i_halt, o_break, sleep, - gie, ibus_err_flag, trap, ill_err_i, - w_clear_icache, pf_valid, pf_illegal, dcd_ce, - dcd_valid, dcd_stalled, op_ce, op_valid, - op_pipe, alu_ce, alu_busy, alu_wR, - alu_illegal, alu_wF, mem_ce, mem_we, - mem_busy, mem_pipe_stalled, (new_pc), (dcd_early_branch) }; - - // Verilator lint_off UNUSED - wire [27:0] dbg_pc, dbg_wb_addr; - // Verilator lint_on UNUSED - generate if (AW-1 < 27) - begin - assign dbg_pc[(AW-1):0] = pf_pc[(AW+1):2]; - assign dbg_pc[27:AW] = 0; - - assign dbg_wb_addr[(AW-1):0] = o_wb_addr; - assign dbg_wb_addr[27:AW] = 0; - end else // if (AW-1 >= 27) - begin - assign dbg_pc[27:0] = pf_pc[29:2]; - assign dbg_wb_addr = o_wb_addr; - end endgenerate - - reg [2:0] dbgsrc; - - always @(posedge i_clk) - begin - dbgsrc <= 0; - if ((i_halt)||(!master_ce)||(debug_trigger)||(o_break)) - dbgsrc <= 3'b000; - else if ((mem_valid)||((!clear_pipeline)&&(!alu_illegal) - &&(((alu_wR)&&(alu_valid)) - ||(div_valid)||(fpu_valid)))) - dbgsrc <= 3'b001; - else if (clear_pipeline) - dbgsrc <= 3'b010; - else if ((o_wb_gbl_stb)|(o_wb_lcl_stb)) - dbgsrc <= 3'b011; - else - dbgsrc <= 3'b100; - end - - always @(posedge i_clk) - casez(dbgsrc) - 3'b000: o_debug <= debug_flags; - 3'b001: o_debug <= { debug_trigger, 1'b0, wr_reg_id[3:0], - wr_gpreg_vl[25:0]}; - 3'b010: o_debug <= { debug_trigger, 3'b100, dbg_pc }; - 3'b011: o_debug <= {debug_trigger, 2'b11, o_wb_gbl_stb, o_wb_we, - (o_wb_we)?o_wb_data[26:0] : dbg_wb_addr[26:0] }; - 3'b1??: o_debug <= debug_flags; - endcase - - //}}} -`endif - - // Make verilator happy - //{{{ - // verilator lint_off UNUSED - wire [56:0] unused; - assign unused = { pf_new_pc, - fpu_ce, pf_data, wr_spreg_vl[1:0], - ipc[1:0], upc[1:0], pf_pc[1:0], - dcd_rA, dcd_pipe, dcd_zI, - dcd_A_stall, dcd_B_stall, dcd_F_stall, - op_Rcc, op_pipe, op_lock, mem_pipe_stalled, prelock_stall, - dcd_F }; - generate if (AW+2 < 32) - begin - wire [31:(AW+2)] generic_ignore; - assign generic_ignore = wr_spreg_vl[31:(AW+2)]; - end endgenerate - // verilator lint_on UNUSED - //}}} - - // Formal methods - //{{{ -`ifdef FORMAL -// The ZipCPU formal properties are maintained elsewhere -`endif // FORMAL - //}}} - -endmodule diff --git a/rtl/cpu/zipdma.v b/rtl/cpu/zipdma.v new file mode 100644 index 0000000..9042ac6 --- /dev/null +++ b/rtl/cpu/zipdma.v @@ -0,0 +1,444 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: zipdma.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: (Upgraded) Wishbone DMA controller for the ZipCPU +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module zipdma #( + // {{{ + parameter ADDRESS_WIDTH=30, LGMEMLEN = 10, + parameter LGDMALENGTH=ADDRESS_WIDTH, + parameter SLV_WIDTH=32, + parameter BUS_WIDTH=512, + parameter [0:0] OPT_LITTLE_ENDIAN = 1'b0, + parameter [0:0] OPT_LOWPOWER = 1'b0, + localparam AW=ADDRESS_WIDTH-$clog2(BUS_WIDTH/8) + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Slave port + // {{{ + // Slave/control wishbone inputs + input wire i_swb_cyc, i_swb_stb, i_swb_we, + input wire [1:0] i_swb_addr, + input wire [SLV_WIDTH-1:0] i_swb_data, + input wire [SLV_WIDTH/8-1:0] i_swb_sel, + // Slave/control wishbone outputs + output wire o_swb_stall, + output reg o_swb_ack, + output reg [SLV_WIDTH-1:0] o_swb_data, + // }}} + // Master/DMA port + // {{{ + output wire o_mwb_cyc, o_mwb_stb, o_mwb_we, + output reg [AW-1:0] o_mwb_addr, + output reg [BUS_WIDTH-1:0] o_mwb_data, + output reg [BUS_WIDTH/8-1:0] o_mwb_sel, + // Master/DMA wishbone responses from the bus + input wire i_mwb_stall, i_mwb_ack, + input wire [BUS_WIDTH-1:0] i_mwb_data, + input wire i_mwb_err, + // }}} + // The interrupt device interrupt lines + input wire [31:0] i_dev_ints, + // An interrupt to be set upon completion + output wire o_interrupt + // }}} + ); + + // Local declarations + // {{{ + localparam FIFO_WIDTH = BUS_WIDTH+$clog2(BUS_WIDTH/8)+2; + localparam LGFIFO = LGMEMLEN-$clog2(BUS_WIDTH/8); + + wire dma_request, dma_abort, + dma_busy, dma_err; + wire [ADDRESS_WIDTH-1:0] dma_src, dma_dst, + read_addr, write_addr; + wire [LGDMALENGTH-1:0] dma_length, remaining_len; + wire [LGMEMLEN:0] dma_transferlen; + wire dma_trigger; + + wire mm2s_request, s2mm_request; + wire mm2s_busy, s2mm_busy; + wire mm2s_err, s2mm_err; + wire mm2s_inc, s2mm_inc; + wire [1:0] mm2s_size, s2mm_size; + wire [ADDRESS_WIDTH-1:0] mm2s_addr, s2mm_addr; + wire [LGMEMLEN:0] mm2s_transferlen, s2mm_transferlen; + + wire mm2s_rd_cyc, mm2s_rd_stb, mm2s_rd_we, + mm2s_rd_stall, mm2s_rd_ack, mm2s_rd_err; + wire [AW-1:0] mm2s_rd_addr; + wire [BUS_WIDTH-1:0] mm2s_rd_data; + wire [BUS_WIDTH/8-1:0] mm2s_rd_sel; + + wire mm2s_valid, mm2s_ready, mm2s_last; + wire [BUS_WIDTH-1:0] mm2s_data; + wire [$clog2(BUS_WIDTH/8):0] mm2s_bytes; + + wire rx_valid, rx_ready, rx_last; + wire [BUS_WIDTH-1:0] rx_data; + wire [$clog2(BUS_WIDTH/8):0] rx_bytes; + + wire tx_valid, tx_ready, tx_last; + wire [BUS_WIDTH-1:0] tx_data; + wire [$clog2(BUS_WIDTH/8):0] tx_bytes; + + wire sfifo_full, sfifo_empty; + wire [LGFIFO:0] ign_sfifo_fill; + + wire s2mm_valid, s2mm_ready, s2mm_last; + wire [BUS_WIDTH-1:0] s2mm_data; + wire [$clog2(BUS_WIDTH/8):0] s2mm_bytes; + + wire s2mm_wr_cyc, s2mm_wr_stb, s2mm_wr_we, + s2mm_wr_stall, s2mm_wr_ack, s2mm_wr_err; + wire [AW-1:0] s2mm_wr_addr; + wire [BUS_WIDTH-1:0] s2mm_wr_data; + wire [BUS_WIDTH/8-1:0] s2mm_wr_sel; + + wire wb_cyc, wb_stb, wb_we, + wb_stall, wb_ack, wb_err; + wire [AW-1:0] wb_addr; + wire [BUS_WIDTH-1:0] wb_data, wb_idata; + wire [BUS_WIDTH/8-1:0] wb_sel; + // }}} + + + zipdma_ctrl #( + // {{{ + .ADDRESS_WIDTH(ADDRESS_WIDTH), + .LGMEMLEN(LGMEMLEN), + .LGDMALENGTH(ADDRESS_WIDTH), + .OPT_LOWPOWER(OPT_LOWPOWER) + // }}} + ) u_controller ( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + // Slave WB control port + // {{{ + // Slave/control wishbone inputs + .i_cyc(i_swb_cyc), .i_stb(i_swb_stb), .i_we(i_swb_we), + .i_addr(i_swb_addr), + .i_data(i_swb_data), .i_sel(i_swb_sel), + // Slave/control wishbone outputs + .o_stall(o_swb_stall), + .o_ack(o_swb_ack), .o_data(o_swb_data), + // }}} + // Internal DMA controls + // {{{ + .o_dma_request(dma_request), .o_dma_abort(dma_abort), + .i_dma_busy(dma_busy), .i_dma_err(dma_err), + .o_src_addr(dma_src), .o_dst_addr(dma_dst), + .o_length(dma_length), .o_transferlen(dma_transferlen), + .o_mm2s_inc(mm2s_inc), .o_mm2s_size(mm2s_size), + .o_s2mm_inc(s2mm_inc), .o_s2mm_size(s2mm_size), + // + .o_trigger(dma_trigger), + // + .i_current_src(read_addr), + .i_current_dst(write_addr), + .i_remaining_len(remaining_len), + // }}} + .i_dma_int(i_dev_ints), .o_interrupt(o_interrupt) + // }}} + ); + + zipdma_fsm #( + // {{{ + .ADDRESS_WIDTH(ADDRESS_WIDTH), + .LGDMALENGTH(ADDRESS_WIDTH), + .LGSUBLENGTH(LGMEMLEN) + // }}} + ) u_dma_fsm ( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + .i_soft_reset(dma_abort), + // DMA control + // {{{ + .i_dma_request(dma_request), + .o_dma_busy(dma_busy), .o_dma_err(dma_err), + .i_src_addr(dma_src), .i_dst_addr(dma_dst), + .i_length(dma_length), .i_transferlen(dma_transferlen), + .i_trigger(dma_trigger), + .o_remaining_len(remaining_len), + // }}} + // Downstream MM2S configuration + // {{{ + .o_mm2s_request(mm2s_request), + .i_mm2s_busy(mm2s_busy), + .i_mm2s_err(mm2s_err), + .i_mm2s_inc(mm2s_inc), + .o_mm2s_addr(mm2s_addr), + .o_mm2s_transferlen(mm2s_transferlen), + // }}} + // Downstream S2MM configuration + // {{{ + .o_s2mm_request(s2mm_request), + .i_s2mm_busy(s2mm_busy), + .i_s2mm_err(s2mm_err), + .i_s2mm_inc(s2mm_inc), + .o_s2mm_addr(s2mm_addr), + .o_s2mm_transferlen(s2mm_transferlen) + // }}} + // }}} + ); + + zipdma_mm2s #( + // {{{ + .ADDRESS_WIDTH(ADDRESS_WIDTH), + .BUS_WIDTH(BUS_WIDTH), + .LGLENGTH(LGMEMLEN), + .OPT_LITTLE_ENDIAN(OPT_LITTLE_ENDIAN), + .OPT_LOWPOWER(OPT_LOWPOWER) + // }}} + ) u_mm2s ( + // {{{ + .i_clk(i_clk), .i_reset(i_reset || dma_abort), + // DMA control + // {{{ + .i_request(mm2s_request), + .o_busy(mm2s_busy), .o_err(mm2s_err), + .i_inc(mm2s_inc), .i_size(mm2s_size), + .i_transferlen(mm2s_transferlen), + .i_addr(mm2s_addr), + // }}} + // Wishbone master interface + // {{{ + .o_rd_cyc(mm2s_rd_cyc), + .o_rd_stb(mm2s_rd_stb), + .o_rd_we(mm2s_rd_we), + .o_rd_addr(mm2s_rd_addr), + .o_rd_data(mm2s_rd_data), + .o_rd_sel(mm2s_rd_sel), + // + .i_rd_stall(mm2s_rd_stall), + .i_rd_ack(mm2s_rd_ack), + .i_rd_data(wb_idata), + .i_rd_err(mm2s_rd_err), + // }}} + // MM2S Stream interface + // {{{ + .M_VALID(mm2s_valid), + .M_READY(mm2s_ready), + .M_DATA(mm2s_data), + .M_BYTES(mm2s_bytes), + .M_LAST(mm2s_last) + // }}} + // }}} + ); + + zipdma_rxgears #( + // {{{ + .BUS_WIDTH(BUS_WIDTH), + .OPT_LITTLE_ENDIAN(OPT_LITTLE_ENDIAN) + // }}} + ) u_rxgears ( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), .i_soft_reset(dma_abort), + // + .S_VALID(mm2s_valid), .S_READY(mm2s_ready), + .S_DATA(mm2s_data), .S_BYTES(mm2s_bytes), + .S_LAST(mm2s_last), + // + .M_VALID(rx_valid), .M_READY(rx_ready), + .M_DATA(rx_data), .M_BYTES(rx_bytes), + .M_LAST(rx_last) + // }}} + ); + + sfifo #( + // {{{ + .BW(FIFO_WIDTH), + .LGFLEN(LGFIFO), + .OPT_ASYNC_READ(1'b1), + .OPT_WRITE_ON_FULL(1'b0), + .OPT_READ_ON_EMPTY(1'b0) + // }}} + ) u_sfifo ( + // {{{ + .i_clk(i_clk), .i_reset(i_reset || dma_abort), + // + .i_wr(rx_valid), .i_data({ rx_last, rx_bytes, rx_data }), + .o_full(sfifo_full), .o_fill(ign_sfifo_fill), + // + .i_rd(tx_ready), .o_data({ tx_last, tx_bytes, tx_data }), + .o_empty(sfifo_empty) + // }}} + ); + + assign rx_ready = !sfifo_full; + assign tx_valid = !sfifo_empty; + + zipdma_txgears #( + // {{{ + .BUS_WIDTH(BUS_WIDTH), + .OPT_LITTLE_ENDIAN(OPT_LITTLE_ENDIAN) + // }}} + ) u_txgears ( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + .i_soft_reset(dma_abort), .i_size(s2mm_size), + // Incoming stream + // {{{ + .S_VALID(tx_valid), .S_READY(tx_ready), + .S_DATA(tx_data), .S_BYTES(tx_bytes), + .S_LAST(tx_last), + // }}} + // Outgoing stream to S2MM + // {{{ + .M_VALID(s2mm_valid), .M_READY(s2mm_ready), + .M_DATA(s2mm_data), .M_BYTES(s2mm_bytes), + .M_LAST(s2mm_last) + // }}} + // }}} + ); + + zipdma_s2mm #( + // {{{ + .ADDRESS_WIDTH(ADDRESS_WIDTH), + .BUS_WIDTH(BUS_WIDTH), + .OPT_LITTLE_ENDIAN(OPT_LITTLE_ENDIAN), + .LGPIPE(LGMEMLEN) + // }}} + ) u_s2mm ( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + // S2MM configuration + // {{{ + .i_request(s2mm_request), + .o_busy(s2mm_busy), .o_err(s2mm_err), + .i_inc(s2mm_inc), .i_size(s2mm_size), + .i_addr(s2mm_addr), + // }}} + // Stream data source + // {{{ + .S_VALID(s2mm_valid), .S_READY(s2mm_ready), + .S_DATA(s2mm_data), .S_BYTES(s2mm_bytes), + .S_LAST(s2mm_last), + // }}} + // Outgoing Wishbone interface + // {{{ + .o_wr_cyc(s2mm_wr_cyc), + .o_wr_stb(s2mm_wr_stb), + .o_wr_we(s2mm_wr_we), + .o_wr_addr(s2mm_wr_addr), + .o_wr_data(s2mm_wr_data), + .o_wr_sel(s2mm_wr_sel), + // + .i_wr_stall(s2mm_wr_stall), + .i_wr_ack(s2mm_wr_ack), + .i_wr_data(wb_idata), + .i_wr_err(s2mm_wr_err) + // }}} + // }}} + ); + + wbarbiter #( + // {{{ + .DW(BUS_WIDTH), .AW(AW), .OPT_ZERO_ON_IDLE(OPT_LOWPOWER) + // }}} + ) u_arbiter ( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + // + .i_a_cyc(mm2s_rd_cyc), .i_a_stb(mm2s_rd_stb), + .i_a_we(mm2s_rd_we), .i_a_adr(mm2s_rd_addr), + .i_a_dat((OPT_LOWPOWER) ? mm2s_rd_data : s2mm_wr_data), + .i_a_sel(mm2s_rd_sel), + .o_a_stall(mm2s_rd_stall), .o_a_ack(mm2s_rd_ack), + .o_a_err(mm2s_rd_err), + // + .i_b_cyc(s2mm_wr_cyc), .i_b_stb(s2mm_wr_stb), + .i_b_we(s2mm_wr_we), .i_b_adr(s2mm_wr_addr), + .i_b_dat(s2mm_wr_data), .i_b_sel(s2mm_wr_sel), + .o_b_stall(s2mm_wr_stall), .o_b_ack(s2mm_wr_ack), + .o_b_err(s2mm_wr_err), + // + .o_cyc(wb_cyc), .o_stb(wb_stb), + .o_we(wb_we), .o_adr(wb_addr), + .o_dat(wb_data), .o_sel(wb_sel), + .i_stall(wb_stall), .i_ack(wb_ack), + .i_err(wb_err) + // }}} + ); + + assign o_mwb_cyc = wb_cyc; + assign o_mwb_stb = wb_stb; + assign o_mwb_we = wb_we; + assign o_mwb_addr = wb_addr; + assign o_mwb_data = wb_data; + assign o_mwb_sel = wb_sel; + assign wb_stall = i_mwb_stall; + assign wb_ack = i_mwb_ack; + assign wb_idata = i_mwb_data; + assign wb_err = i_mwb_err; + + assign read_addr = { mm2s_rd_addr, {($clog2(BUS_WIDTH/8)){1'b0}} }; + assign write_addr = { s2mm_wr_addr, {($clog2(BUS_WIDTH/8)){1'b0}} }; + + // Make verilator happy + // {{{ + // verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, ign_sfifo_fill, mm2s_rd_data, rx_ready, + s2mm_transferlen }; + // verilator lint_on UNUSED + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL + reg f_past_valid; + + initial f_past_valid = 1'b0; + always @(posedge i_clk) + f_past_valid <= 1'b1; + + always @(*) + if (!f_past_valid) + assume(i_reset); +`endif +// }}} +endmodule diff --git a/rtl/cpu/zipdma_ctrl.v b/rtl/cpu/zipdma_ctrl.v new file mode 100644 index 0000000..3b101ed --- /dev/null +++ b/rtl/cpu/zipdma_ctrl.v @@ -0,0 +1,343 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: zipdma_ctrl.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: ZipDMA -- Simply handles the control requests for the ZipDMA. +// Status reads and control writes using a 32-bit Wishbone bus +// interface are handled here. +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module zipdma_ctrl #( + // {{{ + parameter ADDRESS_WIDTH=30, LGMEMLEN = 10, + parameter SLV_WIDTH=32, + // BUS_WIDTH=512, + parameter LGDMALENGTH = ADDRESS_WIDTH, + parameter ABORT_KEY = 32'h41425254, + parameter [0:0] OPT_LOWPOWER = 1'b0, + // == { 8'd65, 8'd66, 8'd82, 8'd84 }, "ABRT", + localparam AW = ADDRESS_WIDTH + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Slave / control port + // {{{ + input wire i_cyc, i_stb, i_we, + input wire [1:0] i_addr, + input wire [SLV_WIDTH-1:0] i_data, + input wire [SLV_WIDTH/8-1:0] i_sel, + // + output wire o_stall, + output reg o_ack, + output reg [SLV_WIDTH-1:0] o_data, + // }}} + // DMA control wires and feedback + // {{{ + output reg o_dma_request, + output reg o_dma_abort, + input wire i_dma_busy, i_dma_err, + output reg [AW-1:0] o_src_addr, + output reg [AW-1:0] o_dst_addr, + output reg [LGDMALENGTH-1:0] o_length, + output reg [LGMEMLEN:0] o_transferlen, + output reg o_mm2s_inc, o_s2mm_inc, + output reg [1:0] o_mm2s_size,o_s2mm_size, + // + output reg o_trigger, + // + input wire [AW-1:0] i_current_src, + input wire [AW-1:0] i_current_dst, + input wire [LGDMALENGTH-1:0] i_remaining_len, + // }}} + input wire [31:0] i_dma_int, + output reg o_interrupt + // }}} + ); + + // Local declarations + // {{{ + reg int_trigger, r_err, r_zero_len, r_busy; + reg [4:0] int_sel; + reg [SLV_WIDTH-1:0] next_src, next_dst, next_len, next_tlen, + w_control_reg; + // }}} + + // o_ack + // {{{ + initial o_ack = 1'b0; + always @(posedge i_clk) + if (i_reset) + o_ack <= 1'b0; + else + o_ack <= i_stb; + // }}} + + assign o_stall = 1'b0; + + // w_control_reg + // {{{ + always @(*) + begin + w_control_reg = 0; + w_control_reg[31] = i_dma_busy; + w_control_reg[30] = r_err || i_dma_err; + w_control_reg[29] = int_trigger; + w_control_reg[28:24] = int_sel; + // + w_control_reg[22] = !o_s2mm_inc; + w_control_reg[21:20] = o_s2mm_size; + // + w_control_reg[19] = !o_mm2s_inc; + w_control_reg[18:17] = o_mm2s_size; + // + w_control_reg[LGMEMLEN:0] = o_transferlen; + end + // }}} + + // o_data + // {{{ + always @(posedge i_clk) + if (!OPT_LOWPOWER || (i_stb && !o_stall && !i_we)) + begin + o_data <= 0; + case(i_addr) + 2'b00: o_data <= w_control_reg; + 2'b01: o_data[AW-1:0] <= (i_dma_busy) ? i_current_src : o_src_addr; + 2'b10: o_data[AW-1:0] <= (i_dma_busy) ? i_current_dst : o_dst_addr; + 2'b11: o_data[LGDMALENGTH-1:0] <= (i_dma_busy) ? i_remaining_len : o_length; + endcase + end + // }}} + + // o_trigger + // {{{ + always @(posedge i_clk) + if (i_reset || o_dma_abort || i_dma_err || !i_dma_busy) + o_trigger <= 1'b0; + else if (!int_trigger) + o_trigger <= 1'b1; + else + o_trigger <= i_dma_int[int_sel]; + // }}} + + // next_src + // {{{ + always @(*) + begin + next_src = 32'h00; + next_src[AW-1:0] = o_src_addr; + + if (i_sel[0]) next_src[ 7: 0] = i_data[ 7: 0]; + if (i_sel[1]) next_src[15: 8] = i_data[15: 8]; + if (i_sel[2]) next_src[23:16] = i_data[23:16]; + if (i_sel[3]) next_src[31:24] = i_data[31:24]; + end + // }}} + + // next_dst + // {{{ + always @(*) + begin + next_dst = 32'h00; + next_dst[AW-1:0] = o_dst_addr; + + if (i_sel[0]) next_dst[ 7: 0] = i_data[ 7: 0]; + if (i_sel[1]) next_dst[15: 8] = i_data[15: 8]; + if (i_sel[2]) next_dst[23:16] = i_data[23:16]; + if (i_sel[3]) next_dst[31:24] = i_data[31:24]; + end + // }}} + + // next_len + // {{{ + always @(*) + begin + next_len = 32'h00; + next_len[LGDMALENGTH-1:0] = o_length; + + if (i_sel[0]) next_len[ 7: 0] = i_data[ 7: 0]; + if (i_sel[1]) next_len[15: 8] = i_data[15: 8]; + if (i_sel[2]) next_len[23:16] = i_data[23:16]; + if (i_sel[3]) next_len[31:24] = i_data[31:24]; + end + // }}} + + // next_tlen + // {{{ + always @(*) + begin + next_tlen = 32'h00; + next_tlen[LGMEMLEN-1:0] = o_transferlen[LGMEMLEN-1:0]; + + if (i_sel[0]) next_tlen[ 7: 0] = i_data[ 7: 0]; + if (i_sel[1]) next_tlen[15: 8] = i_data[15: 8]; + if (i_sel[2]) next_tlen[23:16] = i_data[23:16]; + if (i_sel[3]) next_tlen[31:24] = i_data[31:24]; + + next_tlen[SLV_WIDTH-1:LGMEMLEN] = 0; + next_tlen[LGMEMLEN] = (next_tlen[LGMEMLEN-1:0] == 0); + end + // }}} + + // Process i_data writes + // {{{ + always @(posedge i_clk) + if (i_reset) + begin + // {{{ + o_src_addr <= {(AW){1'b0}}; + o_dst_addr <= {(AW){1'b0}}; + o_length <= {(LGDMALENGTH){1'b0}}; + r_zero_len <= 1'b1; + o_transferlen <= { 1'b1, {(LGMEMLEN){1'b0}} }; + + o_s2mm_inc <= 1'b0; + o_s2mm_size <= 2'b0; + + o_mm2s_inc <= 1'b0; + o_mm2s_size <= 2'b0; + + int_trigger <= 1'b0; + int_sel <= 5'h0; + // }}} + end else if (i_stb && !o_stall && i_we && !o_dma_request && !i_dma_busy) + begin // Register write, DMA is idle + // {{{ + case(i_addr) + 2'b00: begin // Control registers + // {{{ + o_transferlen[LGMEMLEN:0] <= next_tlen[LGMEMLEN:0]; + if (i_sel[2]) + begin + o_s2mm_inc <= !i_data[22]; + o_s2mm_size <= i_data[21:20]; + // + o_mm2s_inc <= !i_data[26]; + o_mm2s_size <= i_data[25:24]; + end + + if (i_sel[3]) + begin + int_trigger <= i_data[29]; + int_sel <= i_data[28:24]; + end end + // }}} + 2'b01: o_src_addr <= next_src[AW-1:0]; + 2'b10: o_dst_addr <= next_dst[AW-1:0]; + 2'b11: begin // o_length + // {{{ + o_length <= next_len[LGDMALENGTH-1:0]; + r_zero_len <= (next_len[LGDMALENGTH-1:0] == 0); + end + // }}} + endcase + // }}} + end + // }}} + + // o_dma_request, o_dma_abort, o_interrupt, r_err + // {{{ + always @(posedge i_clk) + if (i_reset) + begin + // {{{ + o_dma_request <= 1'b0; + o_dma_abort <= 1'b0; + + r_err <= 1'b0; + + o_interrupt <= 1'b0; + // }}} + end else begin + r_busy <= i_dma_busy; + + if (!i_dma_busy) + begin + o_dma_request <= 1'b0; + o_dma_abort <= 1'b0; + end + + if (i_dma_err) + r_err <= 1'b1; + if (r_busy && (!i_dma_busy || i_dma_err)) + o_interrupt <= 1'b1; + + if (i_stb && !o_stall && i_we && i_addr == 2'b00) + begin + // {{{ + if (o_dma_request || i_dma_busy) + begin + // Deal with abort requests + if ((&i_sel) && i_data == ABORT_KEY) + { o_dma_request, o_dma_abort } <= 2'b01; + + end else if (i_sel[3]) + begin + // Clear interrupts, errors, and potentially + // restart the DMA + if (i_data[29] || !i_data[30]) + o_interrupt <= 1'b0; + if (i_data[30]) + r_err <= 1'b0; + if (!i_data[31] && (!r_err || i_data[30])) + o_dma_request <= !r_zero_len; + end + // }}} + end + end + // }}} + + // Keep Verilator happy + // {{{ + // Verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, i_cyc, next_tlen[SLV_WIDTH-1:LGMEMLEN] }; + generate if (SLV_WIDTH > AW) + begin + wire unused_addr; + assign unused_addr = &{ 1'b0, + next_src[SLV_WIDTH-1:AW], + next_dst[SLV_WIDTH-1:AW] + }; + end if (SLV_WIDTH > LGDMALENGTH) + begin + wire unused_len; + assign unused_len = &{ 1'b0, + next_len[SLV_WIDTH-1:LGDMALENGTH] + }; + end endgenerate + // Verilator lint_on UNUSED + // }}} +endmodule diff --git a/rtl/cpu/zipdma_fsm.v b/rtl/cpu/zipdma_fsm.v new file mode 100644 index 0000000..bbb59f5 --- /dev/null +++ b/rtl/cpu/zipdma_fsm.v @@ -0,0 +1,223 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: zipdma_fsm.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: ZipDMA's control FSM +// +// Since the Wishbone bus can only accommodate either a read or a write +// transaction, large DMA transfers need to be broken up between reads +// and writes. This function accomplishes that purpose--issuing read +// requests of the zipdma_mm2s controller, followed by write requests +// of the zipdma_s2mm controller. +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module zipdma_fsm #( + // {{{ + parameter ADDRESS_WIDTH = 32, // Byte ADDR width + parameter LGDMALENGTH = ADDRESS_WIDTH, + parameter LGSUBLENGTH = 10 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + input wire i_soft_reset, + // DMA control + // {{{ + input wire i_dma_request, + output reg o_dma_busy, + output reg o_dma_err, + input wire [ADDRESS_WIDTH-1:0] i_src_addr, + input wire [ADDRESS_WIDTH-1:0] i_dst_addr, + input wire [LGDMALENGTH-1:0] i_length, + input wire [LGSUBLENGTH:0] i_transferlen, + + output wire [LGDMALENGTH-1:0] o_remaining_len, + // }}} + input wire i_trigger, + // MM2S control + // {{{ + output reg o_mm2s_request, + input wire i_mm2s_busy, + input wire i_mm2s_err, + input wire i_mm2s_inc, + // input wire [1:0] i_mm2s_size, + output reg [ADDRESS_WIDTH-1:0] o_mm2s_addr, + output wire [LGSUBLENGTH:0] o_mm2s_transferlen, + // }}} + // S2MM control + // {{{ + output reg o_s2mm_request, + input wire i_s2mm_busy, + input wire i_s2mm_err, + input wire i_s2mm_inc, + // input wire [1:0] i_s2mm_size, + output reg [ADDRESS_WIDTH-1:0] o_s2mm_addr, + output wire [LGSUBLENGTH:0] o_s2mm_transferlen + // }}} + // }}} + ); + + // Local declarations + // {{{ + localparam [1:0] S_IDLE = 2'b00, + S_WAIT = 2'b01, + S_READ = 2'b10, + S_WRITE = 2'b11; + + reg [LGDMALENGTH-1:0] r_length; + reg [LGSUBLENGTH:0] r_transferlen; + reg [1:0] fsm_state; + // }}} + + always @(posedge i_clk) + if (i_reset || i_soft_reset || i_mm2s_err || i_s2mm_err) + begin + // {{{ + o_dma_busy <= 0; + r_length <= 0; + r_transferlen <= 0; + o_mm2s_request <= 0; + o_s2mm_request <= 0; + + o_mm2s_addr <= 0; + o_s2mm_addr <= 0; + + fsm_state <= S_IDLE; + // }}} + end else if (!o_dma_busy) + begin + // {{{ + o_dma_busy <= 1'b0; + + r_length <= 0; + // Verilator lint_off WIDTH + r_transferlen <= (i_length < i_transferlen) ? i_length + : i_transferlen; + // Verilator lint_on WIDTH + + fsm_state <= S_IDLE; + + o_mm2s_request <= 0; + o_s2mm_request <= 0; + + if (i_dma_request) + begin + o_dma_busy <= 1'b1; + + fsm_state <= (i_trigger) ? S_READ : S_WAIT; + o_mm2s_request <= i_trigger; + + o_mm2s_addr <= i_src_addr; + o_s2mm_addr <= i_dst_addr; + r_length <= i_length; + end +`ifdef FORMAL + assert(fsm_state == S_IDLE); +`endif + // }}} + end else case(fsm_state) + S_WAIT: begin + // {{{ + if (r_length == 0) + o_dma_busy <= 0; + else if (i_trigger) + begin + fsm_state <= S_READ; + o_mm2s_request <= 1'b1; + end end + // }}} + S_READ: begin + // {{{ + if (o_mm2s_request && !i_mm2s_busy) // VALID && READY + o_mm2s_request <= 1'b0; + if (!i_mm2s_busy && !o_mm2s_request) + begin + fsm_state <= S_WRITE; + o_s2mm_request <= 1'b1; + + if (i_mm2s_inc) + // Verilator lint_off WIDTH + o_mm2s_addr <= o_mm2s_addr + r_transferlen; + // Verilator lint_on WIDTH + + // Verilator lint_off WIDTH + r_length <= (r_length > r_transferlen) + ? r_length - r_transferlen : 0; + // Verilator lint_on WIDTH + end end + // }}} + S_WRITE: begin + // {{{ + if (o_s2mm_request && !i_s2mm_busy) // VALID && READY + o_s2mm_request <= 1'b0; + if (!i_s2mm_busy && !o_s2mm_request) + begin + fsm_state <= (i_trigger) ? S_READ : S_WAIT; + o_mm2s_request <= (i_trigger); + + // Verilator lint_off WIDTH + if (r_transferlen > r_length) + // Verilator lint_on WIDTH + r_transferlen <= r_length[LGSUBLENGTH:0]; + + if (i_s2mm_inc) + // Verilator lint_off WIDTH + o_s2mm_addr <= o_s2mm_addr + r_transferlen; + // Verilator lint_on WIDTH + + if (r_length == 0) + begin + fsm_state <= S_IDLE; + o_mm2s_request <= 1'b0; + o_dma_busy <= 1'b0; + end + end end + // }}} + default: begin end + endcase + + assign o_s2mm_transferlen = r_transferlen; + assign o_mm2s_transferlen = r_transferlen; + assign o_remaining_len = r_length; + + // o_dma_err + // {{{ + always @(posedge i_clk) + if (i_reset || i_soft_reset || !o_dma_busy) + o_dma_err <= 1'b0; + else + o_dma_err <= (i_mm2s_busy && i_mm2s_err) + || (i_s2mm_busy && i_s2mm_err); + // }}} +endmodule diff --git a/rtl/cpu/zipdma_mm2s.v b/rtl/cpu/zipdma_mm2s.v new file mode 100644 index 0000000..885317f --- /dev/null +++ b/rtl/cpu/zipdma_mm2s.v @@ -0,0 +1,1027 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: zipdma_mm2s.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: ZipDMA -- Read values from memory +// +// This is the first component of the DMA sequence. It reads values from +// memory, and aligns them with an outgoing data stream. +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module zipdma_mm2s #( + // {{{ + parameter ADDRESS_WIDTH=30, + parameter BUS_WIDTH = 64, + parameter LGLENGTH=10, + parameter [0:0] OPT_LITTLE_ENDIAN = 1'b0, + parameter [0:0] OPT_LOWPOWER = 1'b0, + // Abbreviations + localparam DW = BUS_WIDTH, + localparam AW = ADDRESS_WIDTH-$clog2(DW/8) + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Configuration + // {{{ + input wire i_request, + output reg o_busy, o_err, + input wire i_inc, + input wire [1:0] i_size, + input wire [LGLENGTH:0] i_transferlen, + input wire [ADDRESS_WIDTH-1:0] i_addr, // Byte address + // }}} + // Wishbone master interface + // {{{ + output reg o_rd_cyc, o_rd_stb, + output wire o_rd_we, + output reg [AW-1:0] o_rd_addr, + output wire [DW-1:0] o_rd_data, + output reg [DW/8-1:0] o_rd_sel, + input wire i_rd_stall, + input wire i_rd_ack, + input wire [DW-1:0] i_rd_data, + input wire i_rd_err, + // }}} + // Outgoing Stream interface + // {{{ + output wire M_VALID, + input wire M_READY, // *MUST* be 1 + output wire [DW-1:0] M_DATA, + // How many bytes are valid? + output wire [$clog2(DW/8):0] M_BYTES, + output wire M_LAST + // }}} + // }}} + ); + + // Local declarations + // {{{ + localparam [1:0] SZ_BYTE = 2'b11, + SZ_16B = 2'b10, + SZ_32B = 2'b01, + SZ_BUS = 2'b00; + localparam WBLSB = $clog2(DW/8); + reg [WBLSB:0] nxtstb_size, rdstb_size, rdack_size, first_size; + reg [ADDRESS_WIDTH-1:0] next_addr, last_request_addr; + reg [WBLSB-1:0] subaddr, rdack_subaddr; + reg [DW/8-1:0] nxtstb_sel, first_sel; + reg [LGLENGTH:0] wb_outstanding; + + reg [WBLSB+1:0] fill, next_fill; + + reg m_valid, m_last; + reg [2*DW-1:0] sreg; + reg [WBLSB:0] m_bytes; + + reg [LGLENGTH:0] rdstb_len, rdack_len; + + reg [WBLSB-1:0] pre_shift; + reg [DW-1:0] pre_shifted_data; + + reg r_inc; + reg [1:0] r_size; + // }}} + + assign o_rd_we = 1'b0; + assign o_rd_data = {(DW){1'b0}}; + + // Copy the configuration whenever i_request && !o_busy + // {{{ + always @(posedge i_clk) + if (!o_busy && (!OPT_LOWPOWER || i_request)) + begin + r_inc <= i_inc; + r_size <= i_size; + // r_transferlen <= i_transferlen; + // r_addr <= i_addr; + end + // }}} + + // nxtstb_size + // {{{ + generate if (BUS_WIDTH > 32) + begin : GEN_NXTSTB_SIZE + // {{{ + always @(*) + begin + first_size = 0; + case(i_size) + SZ_BYTE: first_size = 1; + SZ_16B: first_size = (i_addr[0]) ? 1:2; + // Verilator lint_off WIDTH + SZ_32B: first_size = 4 - i_addr[1:0]; + SZ_BUS: first_size = (DW/8)-i_addr[WBLSB-1:0]; + endcase + + if (first_size > i_transferlen) + first_size = i_transferlen; + end + + always @(*) + begin + nxtstb_size = rdstb_size; + + case(r_size) + SZ_BYTE: nxtstb_size = 1; + SZ_16B: nxtstb_size = (rdstb_len == 3) ? 1 : 2; + // Verilator lint_off WIDTH + SZ_32B: nxtstb_size = (rdstb_len >= 4 && rdstb_len < 8) + ? (rdstb_len - 4) : 4; + SZ_BUS: begin + nxtstb_size = (DW/8); + if (DW/8 > rdstb_len - rdstb_size) + nxtstb_size = + { 1'b0, rdstb_len[WBLSB:0] } + -{ 1'b0, rdstb_size[WBLSB:0]}; + // if (o_rd_stb && nxtstb_size > (DW/8)-subaddr) + // nxtstb_size = (DW/8)-subaddr; + end + // Verilator lint_on WIDTH + endcase + end + // }}} + end else begin + // {{{ + always @(*) + begin + first_size = 0; + case(i_size) + SZ_BYTE: first_size = 1; + SZ_16B: first_size = (i_addr[0]) ? 1:2; + // Verilator lint_off WIDTH + default: + first_size = (DW/8)-i_addr[WBLSB-1:0]; + endcase + + if (first_size > i_transferlen) + first_size = i_transferlen; + // Verilator lint_on WIDTH + end + + always @(*) + begin + nxtstb_size = rdstb_size; + + casez(r_size) + SZ_BYTE: nxtstb_size = 1; + SZ_16B: nxtstb_size = (rdstb_len == 3) ? 1 : 2; + default: begin + // Verilator lint_off WIDTH + nxtstb_size = (DW/8); + if (DW/8 > rdstb_len - rdstb_size) + nxtstb_size = + { 1'b0, rdstb_len[WBLSB:0] } + -{ 1'b0, rdstb_size[WBLSB:0]}; + end + // Verilator lint_on WIDTH + endcase + end + // }}} + end endgenerate + // }}} + + // next_addr + // {{{ + always @(*) + begin + next_addr = { o_rd_addr, subaddr }; + + if (o_rd_stb && !i_rd_stall) + next_addr = next_addr + + { {(ADDRESS_WIDTH-WBLSB-1){1'b0}}, rdstb_size }; + end + // }}} + + // o_rd_cyc, o_rd_stb, o_busy, o_err, rdstb_len, rdstb_size + // {{{ + initial { o_rd_cyc, o_rd_stb } = 2'b00; + initial { o_busy, o_err } = 2'b00; + always @(posedge i_clk) + if (i_reset) + begin + // {{{ + o_rd_cyc <= 1'b0; + o_rd_stb <= 1'b0; + { o_rd_addr, subaddr } <= 0; + + rdstb_size <= 0; + rdstb_len <= 0; + o_busy <= 0; + o_err <= 0; + // }}} + end else if (o_rd_cyc && i_rd_err) + begin + // {{{ + o_rd_cyc <= 1'b0; + o_rd_stb <= 1'b0; + { o_rd_addr, subaddr } <= 0; + + rdstb_size <= 0; + rdstb_len <= 0; + + o_busy <= 0; + o_err <= 1; + // }}} + end else if (!o_busy) + begin + // {{{ + o_rd_cyc <= i_request; + o_rd_stb <= i_request; + o_busy <= i_request; + o_err <= 0; + + rdstb_size <= 0; + rdstb_len <= 0; + if (!OPT_LOWPOWER || i_request) + begin + { o_rd_addr, subaddr } <= i_addr; + + // rdstb_size + rdstb_size <= first_size; + + // rdstb_len + rdstb_len <= i_transferlen; + end + // }}} + end else begin + if (!i_rd_stall) + o_rd_stb <= 1'b0; + + if (rdstb_len > { {(LGLENGTH-WBLSB){1'b0}}, rdstb_size }) + o_rd_stb <= 1'b1; + + if (o_rd_stb && !i_rd_stall) + begin + // {{{ + if (rdstb_len <= { {(LGLENGTH-WBLSB){1'b0}}, rdstb_size }) + begin + rdstb_len <= 0; + end else begin + rdstb_len <= rdstb_len + - { {(LGLENGTH-WBLSB){1'b0}}, rdstb_size }; + o_rd_stb <= 1'b1; + end + + // rdstb_size + rdstb_size <= nxtstb_size; + + { o_rd_addr, subaddr } <= next_addr; + // }}} + end + + if (wb_outstanding == (i_rd_ack ? 1:0) && !o_rd_stb) + o_rd_cyc <= 1'b0; + + if (m_valid && m_last) + o_busy <= 0; + end +`ifdef FORMAL + always @(*) + if (!o_busy) + assert(!m_valid); + else if (m_valid && m_last) + begin + assert(rdack_len == 0); + assert(fill == m_bytes); + end + + always @(*) + if (o_busy) + assert(rdstb_len <= rdack_len); +`endif + // }}} + + // o_rd_sel + // {{{ + + // nxtstb_sel + // {{{ + always @(*) + if (OPT_LITTLE_ENDIAN) + begin + nxtstb_sel = ((1<> next_addr[WBLSB-1:0]; + // Verilator lint_on WIDTH + end + // }}} + + // first_sel + generate if (BUS_WIDTH > 32) + begin : GEN_STRB + // {{{ + always @(*) + begin + first_sel = 0; + + if (OPT_LITTLE_ENDIAN) + begin + // {{{ + case(i_size) + SZ_BYTE: first_sel = {{(DW/8-1){1'b0}}, 1'b1} << i_addr[WBLSB-1:0]; + SZ_16B: first_sel = {{(DW/8-2){1'b0}}, 1'b1,i_addr[0]} << {i_addr[WBLSB-1:1], 1'b0 }; + SZ_32B: case(i_addr[1:0]) + 2'b00: first_sel = {{(DW/8-4){1'b0}}, 4'b1111} << {i_addr[WBLSB-1:2], 2'b0 }; + 2'b01: first_sel = {{(DW/8-4){1'b0}}, 4'b1110} << {i_addr[WBLSB-1:2], 2'b0 }; + 2'b10: first_sel = {{(DW/8-4){1'b0}}, 4'b1100} << {i_addr[WBLSB-1:2], 2'b0 }; + 2'b11: first_sel = {{(DW/8-4){1'b0}}, 4'b1000} << {i_addr[WBLSB-1:2], 2'b0 }; + endcase + SZ_BUS: first_sel = {(DW/8){1'b1}} << i_addr[WBLSB-1:0]; + endcase + // }}} + end else begin + // {{{ + case(i_size) + SZ_BYTE: first_sel = {1'b1, {(DW/8-1){1'b0}} } >> i_addr[WBLSB-1:0]; + SZ_16B: first_sel = {i_addr[0], 1'b1, {(DW/8-2){1'b0}} } + >> {i_addr[WBLSB-1:1], 1'b0 }; + SZ_32B: case(i_addr[1:0]) + 2'b00: first_sel = {4'b1111, {(DW/8-4){1'b0}} } >> {i_addr[WBLSB-1:2], 2'b0 }; + 2'b01: first_sel = {4'b0111, {(DW/8-4){1'b0}} } >> {i_addr[WBLSB-1:2], 2'b0 }; + 2'b10: first_sel = {4'b0011, {(DW/8-4){1'b0}} } >> {i_addr[WBLSB-1:2], 2'b0 }; + 2'b11: first_sel = {4'b0001, {(DW/8-4){1'b0}} } >> {i_addr[WBLSB-1:2], 2'b0 }; + endcase + SZ_BUS: first_sel = {(DW/8){1'b1}} >> i_addr[WBLSB-1:0]; + endcase + // }}} + end + end + // }}} + end else begin : MIN_STRB + // {{{ + always @(*) + begin + first_sel = 0; + + if (OPT_LITTLE_ENDIAN) + begin + // {{{ + casez(i_size) + SZ_BYTE: first_sel = 4'b0001 << i_addr[WBLSB-1:0]; + SZ_16B: first_sel = 4'b0011 << {i_addr[WBLSB-1:1], 1'b0 }; + default: case(i_addr[1:0]) + 2'b00: first_sel = 4'b1111; + 2'b01: first_sel = 4'b1110; + 2'b10: first_sel = 4'b1100; + 2'b11: first_sel = 4'b1000; + endcase + endcase + // }}} + end else begin + // {{{ + casez(i_size) + SZ_BYTE: first_sel = 4'b1000 >> i_addr[WBLSB-1:0]; + SZ_16B: first_sel = 4'b1100 + >> {i_addr[WBLSB-1:1], 1'b0 }; + default: case(i_addr[1:0]) + 2'b00: first_sel = 4'b1111; + 2'b01: first_sel = 4'b0111; + 2'b10: first_sel = 4'b0011; + 2'b11: first_sel = 4'b0001; + endcase + endcase + // }}} + end + end + // }}} + end endgenerate + + // o_rd_sel + always @(posedge i_clk) + if (i_reset || (o_rd_cyc && i_rd_err)) + begin + // {{{ + o_rd_sel <= 0; + // }}} + end else if (!o_busy) + begin + // {{{ + o_rd_sel <= {(DW/8){1'b0}}; + + if (!OPT_LOWPOWER || i_request) + o_rd_sel <= first_sel; + // }}} + end else if (o_rd_stb && !i_rd_stall) + o_rd_sel <= nxtstb_sel; + // }}} + + // wb_outstanding + // {{{ + initial wb_outstanding = 0; + always @(posedge i_clk) + if (i_reset || !o_rd_cyc || i_rd_err) + wb_outstanding <= 0; + // wb_pipeline_full <= 1'b0; + else case({ (o_rd_stb && !i_rd_stall), i_rd_ack }) + 2'b10: wb_outstanding <= wb_outstanding + 1; + 2'b01: wb_outstanding <= wb_outstanding - 1; + default: begin end + endcase + // }}} + + // rdack_subaddr + // {{{ + always @(posedge i_clk) + if (!o_busy) + begin + if (!OPT_LOWPOWER || i_request) + rdack_subaddr <= i_addr[WBLSB-1:0]; + end else if (i_rd_ack) + begin + // Verilator lint_off WIDTH + if (r_inc) + rdack_subaddr <= rdack_subaddr + rdack_size; + else case(r_size) + SZ_BYTE: begin end + SZ_16B: rdack_subaddr[ 0] <= 1'b0; + SZ_32B: rdack_subaddr[1:0] <= 2'b0; + SZ_BUS: rdack_subaddr[WBLSB-1:0] <= {(WBLSB){1'b0}}; + endcase + // Verilator lint_on WIDTH + end + // }}} + + // rdack_len + // {{{ + always @(posedge i_clk) + if (!o_busy) + begin + if (!OPT_LOWPOWER || i_request) + rdack_len <= i_transferlen; + end else if (i_rd_ack) + begin + rdack_len <= rdack_len-{ {(LGLENGTH-WBLSB){1'b0}}, rdack_size }; + if (rdack_len <= { {(LGLENGTH-WBLSB){1'b0}}, rdack_size }) + rdack_len <= 0; + end + // }}} + + // rdack_size + // {{{ + always @(posedge i_clk) + if (!o_busy) + begin + if (!OPT_LOWPOWER || i_request) + rdack_size <= first_size; + end else if (i_rd_ack) + begin + case(r_size) + SZ_BYTE:rdack_size <= 1; + SZ_16B: rdack_size <= 2; + SZ_32B: rdack_size <= 4; + // Verilator lint_off WIDTH + SZ_BUS: if (rdack_len > DW/8 + rdack_size) + rdack_size <= DW/8; + else + rdack_size <= rdack_len - rdack_size; + // Verilator lint_on WIDTH + endcase + end + // }}} + + // fill, next_fill (depends on rdack_size) + // {{{ + always @(*) + begin + next_fill = fill; + if (M_VALID) + next_fill = next_fill - M_BYTES; + if (i_rd_ack) + next_fill = next_fill + { 1'b0, rdack_size }; + end + + always @(posedge i_clk) + if (!o_busy) + fill <= 0; + else + fill <= next_fill; + // }}} + + // m_valid + // {{{ + initial m_valid = 0; + always @(posedge i_clk) + if (!o_busy) + m_valid <= 1'b0; + else begin + // Verilator lint_off WIDTH + m_valid <= 0; + if ((!m_valid || !m_last) && rdack_len == 0 && fill > 0) + m_valid <= 1; + else if (o_rd_cyc && i_rd_ack) + m_valid <= ((next_fill >= DW/8) + || (rdack_len <= { {(LGLENGTH-1){1'b0}}, rdack_size })); + // Verilator lint_on WIDTH + end + // }}} + + // sreg + // {{{ + initial pre_shift = 0; + always @(posedge i_clk) + if (!o_busy) + begin + if (!OPT_LOWPOWER || i_request) + pre_shift <= i_addr[WBLSB-1:0]; + end else if (o_rd_cyc && i_rd_ack) + begin + if (r_inc) + case(r_size) + SZ_BYTE: pre_shift <= pre_shift + 1; + SZ_16B: begin + // {{{ + pre_shift <= pre_shift + 2; + pre_shift[0] <= 1'b0; + end + // }}} + SZ_32B: begin + // {{{ + // Verilator lint_off WIDTH + pre_shift <= pre_shift + 4; + // Verilator lint_on WIDTH + pre_shift[1:0] <= 2'b0; + end + // }}} + SZ_BUS: pre_shift <= 0; + endcase + end + + always @(*) + if (OPT_LITTLE_ENDIAN) + pre_shifted_data = i_rd_data >> (8*pre_shift); + else + pre_shifted_data = i_rd_data << (8*pre_shift); + + initial sreg = 0; + always @(posedge i_clk) + if (!o_busy) + sreg <= 0; + else if (o_rd_cyc && i_rd_ack) + begin + // {{{ + // Verilator lint_off WIDTH + if (OPT_LITTLE_ENDIAN) + begin + // {{{ + if (m_valid) + sreg <= { {(DW){1'b0}}, sreg[2*DW-1:DW] } + | (pre_shifted_data << ((fill-DW/8)*8)); + else + sreg <= sreg | (i_rd_data << (fill * 8)); + // }}} + end else begin + if (m_valid) + sreg <= (sreg << DW) + | ({ pre_shifted_data, {(DW){1'b0}} } >> ((fill-DW/8)*8)); + else + sreg <= sreg | ({ pre_shifted_data, {(DW){1'b0}} } >> (fill * 8)); + end + // Verilator lint_on WIDTH + // }}} + end else if (m_valid) + begin + // {{{ + if (OPT_LITTLE_ENDIAN) + sreg <= { {(DW){1'b0}}, sreg[2*DW-1:DW] }; + else + sreg <= { sreg[DW-1:0], {(DW){1'b0}} }; + // }}} + end + // }}} + + // m_bytes + // {{{ + initial m_bytes = 0; + always @(posedge i_clk) + if (!o_busy) + begin + m_bytes <= 0; + end else if (i_rd_ack) + begin + if (|next_fill[WBLSB+1:WBLSB]) // if next_fill >= DW/8) + // Verilator lint_off WIDTH + m_bytes <= DW/8; + // Verilator lint_on WIDTH + else + m_bytes <= { 1'b0, next_fill[WBLSB-1:0] }; + end else if (rdack_len == 0) + m_bytes <= next_fill[WBLSB:0]; +`ifdef FORMAL + always @(*) + if (M_VALID) + assert(M_BYTES <= DW/8); +`endif + // }}} + + // m_last + // {{{ + always @(*) + begin + last_request_addr = i_addr; + if (r_inc) + // Verilator lint_off WIDTH + last_request_addr = i_addr + i_transferlen - 1; + // Verilator lint_on WIDTH + end + + initial m_last = 0; + always @(posedge i_clk) + if (!o_busy) + begin + m_last <= 1'b0; + if (!OPT_LOWPOWER || i_request) + case(i_size) + SZ_BYTE: m_last <= (i_transferlen <= 1); + SZ_16B: m_last <= (last_request_addr[ADDRESS_WIDTH-1:1] != i_addr[ADDRESS_WIDTH-1:1]); + SZ_32B: m_last <= (last_request_addr[ADDRESS_WIDTH-1:2] != i_addr[ADDRESS_WIDTH-1:2]); + SZ_BUS: m_last <= (last_request_addr[ADDRESS_WIDTH-1:WBLSB] != i_addr[ADDRESS_WIDTH-1:WBLSB]); + endcase + end else if (i_rd_ack) + begin + // Verilator lint_off WIDTH + m_last <= (rdack_len <= rdack_size) && (next_fill <= DW/8); + // Verilator lint_on WIDTH + end else if (rdack_len == 0) + m_last <= 1; + // }}} + + assign M_VALID = m_valid; + assign M_DATA = (OPT_LITTLE_ENDIAN) ? sreg[DW-1:0] : sreg[2*DW-1:DW]; + assign M_BYTES= m_bytes; + assign M_LAST = m_last; + + // Keep Verilator happy + // {{{ + // Verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, M_READY, last_request_addr[0] }; + // Verilator lint_on UNUSED + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL + localparam F_LGDEPTH = LGLENGTH+1-WBLSB; + localparam F_LGCOUNT = LGLENGTH+1; + reg f_past_valid; + wire [F_LGDEPTH-1:0] fwb_nreqs, fwb_nacks, fwb_outstanding; + (* anyconst *) reg f_cfg_inc; + (* anyconst *) reg [1:0] f_cfg_size; + (* anyconst *) reg [ADDRESS_WIDTH-1:0] f_cfg_addr; + (* anyconst *) reg [LGLENGTH:0] f_cfg_len; + reg [F_LGCOUNT-1:0] f_rcvd, f_sent; + reg [WBLSB:0] f_ack_size; + reg [LGLENGTH:0] r_transferlen; + reg [ADDRESS_WIDTH-1:0] r_addr; + + + initial f_past_valid = 0; + always @(posedge i_clk) + f_past_valid <= 1; + + always @(*) + if (!f_past_valid) + assume(i_reset); + + //////////////////////////////////////////////////////////////////////// + // + // Configuration properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if (!f_past_valid || $past(i_reset) || $past(o_err)) + begin + assume(!i_request); + end else if ($past(o_busy && i_request)) + begin + assume(i_request); + assume($stable(i_inc)); + assume($stable(i_size)); + assume($stable(i_addr)); + assume($stable(i_transferlen)); + end + + always @(posedge i_clk) + if (!o_busy && (!OPT_LOWPOWER || i_request)) + begin + // Shadow copy of these registers + r_transferlen <= i_transferlen; + r_addr <= i_addr; + end + + always @(*) + if (i_request && !o_busy) + begin + assume(i_inc == f_cfg_inc); + assume(i_size == f_cfg_size); + assume(i_addr == f_cfg_addr); + assume(i_transferlen == f_cfg_len); + end else if (o_busy) + begin + assert(r_inc == f_cfg_inc); + assert(r_size == f_cfg_size); + assert(r_addr == f_cfg_addr); + assert(r_transferlen == f_cfg_len); + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Wishbone properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + fwb_master #( + // {{{ + .AW(AW), .DW(DW), .F_LGDEPTH(F_LGDEPTH), + .F_OPT_RMW_BUS_OPTION(1'b0), + .F_OPT_DISCONTINUOUS(1'b0), + .F_OPT_SOURCE(1'b1) + // }}} + ) fwb ( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + // + .i_wb_cyc(o_rd_cyc), + .i_wb_stb(o_rd_stb), + .i_wb_we(o_rd_we), + .i_wb_addr(o_rd_addr), + .i_wb_data(o_rd_data), + .i_wb_sel(o_rd_sel), + // + .i_wb_stall(i_rd_stall), + .i_wb_ack(i_rd_ack), + .i_wb_idata(i_rd_data), + .i_wb_err(i_rd_err), + // + .f_nreqs(fwb_nreqs), .f_nacks(fwb_nacks), + .f_outstanding(fwb_outstanding) + // }}} + ); + + always @(*) + assert(!o_rd_we); + + always @(*) + if (f_past_valid && o_rd_stb) + begin + assert(o_rd_sel != 0); + + case(i_size) + SZ_BYTE:assert($countones(o_rd_sel) == 1); + SZ_16B: assert($countones(o_rd_sel) <= 2); + SZ_32B: assert($countones(o_rd_sel) <= 4); + default: begin end + endcase + end + + always @(*) + if (f_past_valid && o_rd_cyc) + assert(fwb_outstanding == wb_outstanding); + + // f_rcvd + // {{{ + initial f_rcvd = 0; + always @(posedge i_clk) + if (i_reset || !o_busy || o_err) + f_rcvd <= 0; + else if (o_rd_cyc && i_rd_ack) + begin + if (f_rcvd == 0) + begin + case(i_size) + SZ_BYTE: f_rcvd <= f_rcvd + 1; + SZ_16B: f_rcvd <= f_rcvd + (2 - i_addr[ 0]); + SZ_32B: f_rcvd <= f_rcvd + (4 - i_addr[1:0]); + SZ_BUS: f_rcvd <= f_rcvd + (DW/8 - i_addr[WBLSB-1:0]); + endcase + end else case(i_size) + SZ_BYTE: f_rcvd <= f_rcvd + 1; + SZ_16B: f_rcvd <= f_rcvd + 2; + SZ_32B: f_rcvd <= f_rcvd + 4; + SZ_BUS: f_rcvd <= f_rcvd + DW/8; + endcase + end + // }}} + + // f_ack_size + // {{{ + always @(*) + begin + case(i_size) + SZ_BYTE: f_ack_size = 1; + SZ_16B: f_ack_size = 2; + SZ_32B: f_ack_size = 4; + SZ_BUS: f_ack_size = DW/8; + endcase + + if (f_rcvd == 0) + case(i_size) + SZ_BYTE: f_ack_size = 1; + SZ_16B: f_ack_size = (2 - i_addr[ 0]); + SZ_32B: f_ack_size = (4 - i_addr[1:0]); + SZ_BUS: f_ack_size = (DW/8-i_addr[WBLSB-1:0]); + endcase + + if (f_rcvd + f_ack_size > i_transferlen) + f_ack_size = i_transferlen - f_rcvd; + end + // }}} + + always @(*) + if (!i_reset && o_busy) + begin + assert(rdack_size == f_ack_size); + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Outgoing stream properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if (!f_past_valid || $past(i_reset)) + begin + assert(!M_VALID); + end + + always @(*) + if (f_past_valid && M_VALID) + begin + assert(M_BYTES > 0); + assert(M_BYTES <= (DW/8)); + + if (M_LAST) + assert(!o_rd_stb); + end + + initial f_sent = 0; + always @(posedge i_clk) + if (i_reset) + f_sent <= 0; + else if (M_VALID && M_READY) + begin + if (M_LAST) + f_sent <= 0; + else + f_sent <= f_sent + M_BYTES; + end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // "Contract" properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + (* anyconst *) reg fc_check; + (* anyconst *) reg [F_LGCOUNT-1:0] fc_posn; + (* anyconst *) reg [7:0] fc_byte; + + wire fwb_check, fm_check; + reg [WBLSB-1:0] fwb_shift, fm_shift; + reg [DW-1:0] fwb_shifted, fm_shifted; + + // Assume a known response from the bus + // {{{ + assign fwb_check = fc_check && (o_rd_cyc && i_rd_ack) + && (f_rcvd <= fc_posn) + && (fc_posn < f_rcvd + f_ack_size); + + always @(*) + begin + fwb_shift = f_rcvd - fc_posn; + fwb_shift = fwb_shift + pre_shift; + end + + always @(*) + if (OPT_LITTLE_ENDIAN) + fwb_shifted = i_rd_data >> (8*fwb_shift); + else + fwb_shifted = i_rd_data << (8*fwb_shift); + + always @(*) + if (fwb_check) + begin + if (OPT_LITTLE_ENDIAN) + assume(fwb_shifted[7:0] == fc_byte); + else + assume(fwb_shifted[DW-1:DW-8] == fc_byte); + end + // }}} + + // Assert a specific output + // {{{ + assign fm_check = fc_check && M_VALID + && (f_sent <= fc_posn) + && (fc_posn < f_sent + fill); + + always @(*) + fm_shift = fc_posn - f_sent; + + always @(*) + if (OPT_LITTLE_ENDIAN) + fm_shifted = sreg >> (8*fm_shift); + else + fm_shifted = sreg << (8*fm_shift); + + always @(*) + if (!i_reset && fm_check) + begin + if (OPT_LITTLE_ENDIAN) + assert(fm_shifted[7:0] == fc_byte); + else + assert(fm_shifted[2*DW-1:2*DW-8] == fc_byte); + end + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Cover properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(*) + begin + cover(!i_reset && i_request); + cover(o_busy); + cover(o_rd_cyc); + cover(o_rd_cyc && i_rd_ack); + end + + always @(*) + cover(!i_reset && fm_check); + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // "Careless" assumptions + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // The outgoing stream isn't quite an AXI stream master interface, + // since WB doesn't have backpressure. Therefore, we assume M_READY + // is always high when we need it to be. + + always @(*) + if (!i_reset && M_VALID) + assume(M_READY); + // }}} +`endif +// }}} +endmodule diff --git a/rtl/cpu/zipdma_rxgears.v b/rtl/cpu/zipdma_rxgears.v new file mode 100644 index 0000000..a716bf9 --- /dev/null +++ b/rtl/cpu/zipdma_rxgears.v @@ -0,0 +1,540 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: zipdma_rxgears.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: ZipDMA -- A gearbox to pack received data. This is part of our +// alignment process. Data comes in, gets read, gets packed, goes +// into the FIFO, gets unpacked, and eventually written back to the bus. +// Here, we simply pack words prior to stuffing them into the FIFO. +// +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module zipdma_rxgears #( + // {{{ + parameter BUS_WIDTH = 64, + parameter [0:0] OPT_LITTLE_ENDIAN = 1'b0, + // Abbreviations + localparam DW = BUS_WIDTH + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Configuration + // {{{ + input wire i_soft_reset, + // }}} + // Incoming Stream interface + // {{{ + input wire S_VALID, + output wire S_READY, + input wire [DW-1:0] S_DATA, + // How many bytes are valid? + input wire [$clog2(DW/8):0] S_BYTES, + input wire S_LAST, + // }}} + // Outgoing Stream interface + // {{{ + output wire M_VALID, + input wire M_READY, + output wire [DW-1:0] M_DATA, + // How many bytes are valid? + output wire [$clog2(DW/8):0] M_BYTES, + output wire M_LAST + // }}} + // }}} + ); + + // Local declarations + // {{{ + localparam WBLSB = $clog2(DW/8); + reg [2*DW-1:0] sreg; + reg [WBLSB+1:0] next_fill, fill; + reg m_valid, m_last, next_last, + r_last, r_full; + reg [WBLSB:0] m_bytes; + reg [WBLSB-1:0] shift; + // }}} + + // next_fill, next_last + // {{{ + always @(*) + begin + next_fill = fill; + if (M_VALID && M_READY) + begin + if (M_LAST) + next_fill = 0; + else + next_fill[WBLSB+1:WBLSB] + = next_fill[WBLSB+1:WBLSB] - 1; + end + + if (S_VALID && S_READY) + next_fill = next_fill + S_BYTES; + + next_last = 0; + if (S_VALID && S_READY && S_LAST) + next_last = (next_fill[WBLSB+1:WBLSB] == 2'b00) + ||((next_fill[WBLSB+1:WBLSB] == 2'b01) + &&(next_fill[WBLSB-1:0] == 0)); + // Was next_fill <= DW/8); + end + // }}} + + // fill + // {{{ + initial fill = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + fill <= 0; + else + fill <= next_fill; +`ifdef FORMAL + always @(*) + assert(fill < 2*DW/8); +`endif + // }}} + + // r_full + // {{{ +/* + // This isn't necessary, since r_full == fill[WBLSB]; + initial r_full = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + r_full <= 0; + else if (M_VALID && M_READY) + r_full <= 1'b0; + else if (S_VALID && S_READY) + r_full <= (next_fill >= (DW/8)); +*/ + always @(*) + // Verilator lint_off WIDTH + r_full = (fill >= (DW/8)); + // Verilator lint_on WIDTH + // }}} + + // m_valid + // {{{ + initial m_valid = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + m_valid <= 0; + else if (!M_VALID || M_READY) + m_valid <= r_last || (S_VALID && S_READY && S_LAST) + || (|next_fill[WBLSB+1:WBLSB]); +`ifdef FORMAL + always @(*) + if (fill >= (DW/8) || r_last || m_last) + assert(m_valid); + + always @(*) + if (m_last) + assert(m_bytes == fill); +`endif + // }}} + + // m_bytes + // {{{ + initial m_bytes = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + m_bytes <= 0; + else if (!M_VALID || M_READY) + m_bytes <= (|next_fill[WBLSB+1:WBLSB]) + ? { 1'b1, {(WBLSB){1'b0}} } // DW/8 + : { 1'b0, next_fill[WBLSB-1:0] }; + // m_bytes <= (next_fill > (DW/8)) ? DW/8 : next_fill; + // }}} + + // r_last, m_last + // {{{ + initial { r_last, m_last } = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + { r_last, m_last } <= 0; + else if (S_VALID && S_READY && S_LAST) + { r_last, m_last } <= { !next_last, next_last }; + else if (M_VALID && M_READY) + { r_last, m_last } <= { 1'b0, r_last }; + // }}} + + // sreg + // {{{ + reg [DW-1:0] s_data; + integer ik; + + always @(*) + begin + s_data = 0; + for(ik=0; ik>(shift*8)); + // Verilator lint_on WIDTH + else + sreg <= { sreg[DW-1:0], {(DW){1'b0}} }; + end else if (S_VALID && S_READY) + // Verilator lint_off WIDTH + sreg <= sreg | ({ s_data, {(DW){1'b0}} } >> (shift*8)); + // Verilator lint_on WIDTH + end + // Verilator lint_on WIDTH + // }}} + + assign M_VALID = m_valid; + assign M_DATA = (OPT_LITTLE_ENDIAN) ? sreg[DW-1:0] : sreg[2*DW-1:DW]; + assign M_BYTES = m_bytes; + assign M_LAST = m_last; + + assign S_READY = (!M_LAST && !r_last) + && (!M_VALID || (M_READY || !r_full)); + // || (S_BYTES + fill < 2*DW/8)))); + + // Keep Verilator happy + // {{{ + // wire unused; + // assign unused = &{ 1'b0, ... }; + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL + localparam F_LGCOUNT = 16; + reg f_past_valid; + reg [F_LGCOUNT-1:0] f_rcvd, f_sent; + + initial f_past_valid = 0; + always @(posedge i_clk) + f_past_valid <= 1; + + always @(*) + if (!f_past_valid) + assume(i_reset); + + //////////////////////////////////////////////////////////////////////// + // + // Incoming stream properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if (!f_past_valid || $past(i_reset || i_soft_reset)) + assume(!S_VALID); + else if ($past(S_VALID && !S_READY)) + begin + assume(S_VALID); + assume($stable(S_DATA)); + assume($stable(S_BYTES)); + assume($stable(S_LAST)); + end + + always @(*) + if (!i_reset && S_VALID) + begin + assume(S_BYTES <= (DW/8)); + assume(S_BYTES > 0); + end + + initial f_rcvd = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + f_rcvd <= 0; + else if (S_VALID && S_READY) + begin + if (S_LAST) + f_rcvd <= 0; + else + f_rcvd <= f_rcvd + S_BYTES; + end + + always @(*) + begin + assume(!f_rcvd[F_LGCOUNT-1]); + assume({ 1'b0, f_rcvd } + (S_VALID ? S_BYTES : 0) + < (1<<(F_LGCOUNT-1))); + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Outgoing stream properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if (!f_past_valid || $past(i_reset || i_soft_reset)) + begin + assert(!M_VALID); + assert(!r_last); + assert(!M_LAST); + end else if ($past(M_VALID && !M_READY)) + begin + assert(M_VALID); + assert($stable(M_DATA)); + assert($stable(M_BYTES)); + assert($stable(M_LAST)); + end + + always @(*) + if (!i_reset && M_VALID) + begin + assert(M_BYTES <= (DW/8)); + assert(M_BYTES > 0); + + if (!M_LAST) + assert(M_BYTES == (DW/8)); + end + + initial f_sent = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + f_sent <= 0; + else if (M_VALID && M_READY) + begin + if (M_LAST) + f_sent <= 0; + else + f_sent <= f_sent + M_BYTES; + end + + reg [F_LGCOUNT-1:0] f_buffered; + + always @(*) + begin + f_buffered = f_sent + fill; + + assert(!f_sent[F_LGCOUNT-1]); + assert(!f_buffered[F_LGCOUNT-1]); + + assert(f_buffered >= f_sent); + + assert(f_sent[WBLSB-1:0] == 0); + end + + always @(*) + if (!i_reset) + begin + if (!m_last && !r_last) + begin + assert(f_sent + fill == f_rcvd); + assert(f_sent <= f_rcvd); + end + end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Induction properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(*) + if (!i_reset) + begin + assert(!r_last || !M_LAST); + + if (r_last) + assert(M_VALID && fill > (DW/8)); + end + + always @(*) + if (!i_reset) + begin + if (fill > (DW/8)) + assert(M_BYTES == (DW/8)); + else + assert(M_BYTES == fill); + end + + always @(*) + if (!i_reset && (r_last || M_LAST)) + assert(f_rcvd == 0); + else if (!i_reset) + assert(fill[WBLSB-1:0] == f_rcvd[WBLSB-1:0]); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Contract properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + (* anyconst *) reg fc_check; + (* anyconst *) reg [F_LGCOUNT-1:0] fc_posn; + (* anyconst *) reg [7:0] fc_byte; + + wire frx_check, ftx_check; + reg [WBLSB-1:0] frx_shift; + reg [WBLSB+1:0] ftx_shift; + reg [DW-1:0] frx_shifted; + reg [2*DW-1:0] ftx_shifted; + + always @(*) + frx_shift = fc_posn [WBLSB-1:0]- f_rcvd[WBLSB-1:0]; + + always @(*) + if (OPT_LITTLE_ENDIAN) + frx_shifted = S_DATA >> (8*frx_shift); + else + frx_shifted = S_DATA << (8*frx_shift); + + assign frx_check = fc_check && S_VALID && f_rcvd <= fc_posn + && (fc_posn < f_rcvd + S_BYTES); + + always @(*) + if (!i_reset && frx_check) + begin + if (OPT_LITTLE_ENDIAN) + begin + assume(frx_shifted[7:0] == fc_byte); + end else begin + assume(frx_shifted[DW-1:DW-8] == fc_byte); + end + end + + always @(*) + begin + ftx_shift = fc_posn[WBLSB:0]- f_sent[WBLSB:0]; + ftx_shift[WBLSB+1] = 0; + end + + always @(*) + if (OPT_LITTLE_ENDIAN) + ftx_shifted = sreg >> (8*ftx_shift); + else + ftx_shifted = sreg << (8*ftx_shift); + + assign ftx_check = fc_check && f_sent <= fc_posn + && (fc_posn < f_sent + fill); + + always @(*) + if (!i_reset && ftx_check) + begin + if (OPT_LITTLE_ENDIAN) + begin + assert(ftx_shifted[7:0] == fc_byte); + end else begin + assert(ftx_shifted[2*DW-1:2*DW-8] == fc_byte); + end + end + + always @(*) + if (!i_reset) + begin + for(ik=0; ik<2*DW/8; ik=ik+1) + if (fill <= ik) + begin + if (OPT_LITTLE_ENDIAN) + assert(sreg[8*ik +: 8] == 8'h00); + else + assert(sreg[2*DW-8-8*ik +: 8] == 8'h00); + end + end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Cover properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // "Careless" assumptions + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // }}} +`endif +// }}} +endmodule diff --git a/rtl/cpu/zipdma_s2mm.v b/rtl/cpu/zipdma_s2mm.v new file mode 100644 index 0000000..4a8c1ba --- /dev/null +++ b/rtl/cpu/zipdma_s2mm.v @@ -0,0 +1,971 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: zipdma_s2mm.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: ZipDMA -- Writes data, having gone through the realignment +// pipeline, back to the bus. This data will be written either +// 1, 2, or 4 bytes at a time, or at the full width of the bus. +// +// When writing 2 or 4 bytes at a time, the address must (currently) be +// aligned. +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module zipdma_s2mm #( + // {{{ + parameter ADDRESS_WIDTH=30, + parameter BUS_WIDTH = 64, + parameter [0:0] OPT_LITTLE_ENDIAN = 1'b0, + parameter LGPIPE = 10, + // Abbreviations + localparam DW = BUS_WIDTH, + localparam AW = ADDRESS_WIDTH-$clog2(DW/8) + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Configuration + // {{{ + input wire i_request, + output reg o_busy, o_err, + input wire i_inc, + input wire [1:0] i_size, + // input wire [LGLENGTH:0] i_transferlen, + input wire [ADDRESS_WIDTH-1:0] i_addr, // Byte address + // }}} + // Incoming Stream interface + // {{{ + input wire S_VALID, + output wire S_READY, + input wire [DW-1:0] S_DATA, + // How many bytes are valid? + input wire [$clog2(DW/8):0] S_BYTES, + input wire S_LAST, + // }}} + // Outgoing Wishbone interface + // {{{ + output reg o_wr_cyc, o_wr_stb, + output wire o_wr_we, + output reg [AW-1:0] o_wr_addr, + output reg [DW-1:0] o_wr_data, + output reg [DW/8-1:0] o_wr_sel, + input wire i_wr_stall, + input wire i_wr_ack, + input wire [DW-1:0] i_wr_data, + input wire i_wr_err + // }}} + // }}} + ); + + // Local decalarations + // {{{ + localparam [1:0] SZ_BYTE = 2'b11, + SZ_16B = 2'b10, + SZ_32B = 2'b01, + SZ_BUS = 2'b00; + localparam WBLSB = $clog2(DW/8); + integer ik; + // + reg r_inc; + reg [1:0] r_size; + // + reg [ADDRESS_WIDTH:0] next_addr; + reg [WBLSB-1:0] subaddr; + reg [2*DW-1:0] next_data; + reg [DW-1:0] r_data; + reg [2*DW/8-1:0] next_sel, pre_sel; + reg [DW/8-1:0] r_sel; + reg r_last; + + reg [LGPIPE-1:0] wb_outstanding; + reg wb_pipeline_full; + reg addr_overflow; + // }}} + + assign o_wr_we = 1'b1; + + // Copy config: r_inc, r_size(, r_addr) + // {{{ + always @(posedge i_clk) + if (i_request && !o_busy) + begin + r_inc <= i_inc; + r_size <= i_size; + // r_addr <= i_addr; + end + // }}} + + // next_addr + // {{{ + always @(*) + begin + next_addr = { 1'b0, o_wr_addr, subaddr }; + + if (o_wr_stb && !i_wr_stall) + case(r_size) + SZ_BYTE: if (r_inc) + next_addr = next_addr + 1; + SZ_16B: begin // 16-bit addressing + if (r_inc) + next_addr = next_addr + 2; + else + next_addr[ 0] = 0; + end + SZ_32B: begin // 32-bit addressing + if (r_inc) + next_addr = next_addr + 4; + else + next_addr[1:0] = 0; + end + SZ_BUS: begin // Full word addressing + if (r_inc) + next_addr = next_addr + { {(AW-1){1'b0}}, 1'b1, + {(WBLSB){1'b0}} }; + else + next_addr[WBLSB-1:0] = 0; + end + endcase + end + + always @(*) + addr_overflow = next_addr[ADDRESS_WIDTH]; +`ifdef FORMAL + always @(posedge i_clk) + if (!i_reset && o_busy && !r_inc) + case(r_size) + SZ_BYTE: begin end + SZ_16B: assert(next_addr[0] == 0); + SZ_32B: assert(next_addr[1:0] == 0); + SZ_BUS: assert(next_addr[WBLSB-1:0] == 0); + endcase +`endif + // }}} + + // next_data + // {{{ + always @(*) + if (OPT_LITTLE_ENDIAN) + begin + next_data = { {(DW){1'b0}}, r_data }; + + // Zero out unused data + for(ik=0; ik> (next_addr[WBLSB-1:0]*8)); + end + // }}} + + // next_sel + // {{{ + always @(*) + begin + pre_sel = 0; + + if (OPT_LITTLE_ENDIAN) + begin + for(ik=0; ik> (next_addr[WBLSB-1:0])); + end + end + // }}} + + // wb_pipeline_full, wb_outstanding + // {{{ + initial wb_pipeline_full = 1'b0; + initial wb_outstanding = 0; + always @(posedge i_clk) + if (i_reset || !o_wr_cyc || i_wr_err) + begin + wb_pipeline_full <= 1'b0; + wb_outstanding <= 0; + end else case({ (o_wr_stb && !i_wr_stall), i_wr_ack }) + 2'b10: begin + wb_pipeline_full <= (&wb_outstanding[LGPIPE-1:2]) + && (|wb_outstanding[1:0]); + wb_outstanding <= wb_outstanding + 1; + end + 2'b01: begin + wb_pipeline_full <= (&wb_outstanding[LGPIPE-1:0]); + wb_outstanding <= wb_outstanding - 1; + end + default: begin end + endcase + +`ifdef FORMAL + always @(*) + if (!i_reset || !o_wr_cyc) + assert(wb_pipeline_full == (&wb_outstanding[LGPIPE-1:1])); + + always @(*) + if (!i_reset && o_wr_cyc && (&wb_outstanding)) + assert(!o_wr_stb); +`endif + // }}} + + // crc, stb, o_wr_addr, o_wr_data, o_wr_sel, o_busy, o_err, subaddr + // {{{ + initial o_wr_cyc = 1'b0; + initial o_wr_stb = 1'b0; + initial o_busy = 1'b0; + initial o_err = 1'b0; + always @(posedge i_clk) + if (i_reset) + begin + // {{{ + o_wr_cyc <= 0; + o_wr_stb <= 0; + o_wr_addr <= 0; + o_wr_data <= 0; + o_wr_sel <= 0; + + o_busy <= 1'b0; + o_err <= 1'b0; + { o_wr_addr, subaddr } <= {(ADDRESS_WIDTH){1'b0}}; + { r_data, o_wr_data } <= {(2*DW ){1'b0}}; + { r_sel, o_wr_sel } <= {(2*DW/8){1'b0}}; + r_last <= 1'b0; + // }}} + end else if (!o_busy || o_err || (o_wr_cyc && i_wr_err)) + begin + // {{{ + o_wr_cyc <= 0; + o_wr_stb <= 0; + o_wr_addr <= 0; + o_wr_data <= 0; + o_wr_sel <= 0; + + o_busy <= i_request && !o_busy; + + o_err <= o_wr_cyc && i_wr_err; + if (o_wr_cyc && i_wr_err) + o_busy <= 1'b0; + + o_wr_addr <= i_addr[ADDRESS_WIDTH-1:WBLSB]; + subaddr <= i_addr[WBLSB-1:0]; + { r_data, o_wr_data } <= {(2*DW ){1'b0}}; + { r_sel, o_wr_sel } <= {(2*DW/8){1'b0}}; + r_last <= 1'b0; + // }}} + end else if (!o_wr_stb || !i_wr_stall) + begin + // {{{ + o_wr_stb <= 1'b0; + + if (o_wr_stb) + { o_wr_addr, subaddr } <= next_addr[ADDRESS_WIDTH-1:0]; + + if (addr_overflow) + { o_err, o_wr_cyc, o_wr_stb } <= 3'b100; + else if (!wb_pipeline_full) + begin + if ((r_last && (|r_sel)) || (S_VALID && !r_last)) + begin + // Need to flush our last result out + { o_wr_cyc, o_wr_stb } <= 2'b11; + + if (OPT_LITTLE_ENDIAN) + begin + { r_data, o_wr_data } <= next_data; + { r_sel, o_wr_sel } <= next_sel; + end else begin + { o_wr_data, r_data } <= next_data; + { o_wr_sel, r_sel } <= next_sel; + end + + end else if (wb_outstanding + (o_wr_stb ? 1:0) + == (i_wr_ack ? 1:0)) + begin + // We are all done writing + o_wr_cyc <= 1'b0; + o_busy <= !r_last; + end + end + + if (S_VALID && !r_last) + r_last <= S_LAST; + // }}} + end + +`ifdef FORMAL + always @(posedge i_clk) + if (i_reset || !o_busy || o_err || (o_wr_cyc && i_wr_err)) + begin + end else if (!o_wr_stb || !i_wr_stall) + begin + // {{{ + if (addr_overflow) + begin + end else if (!wb_pipeline_full) + begin + if (r_last && (|r_sel)) + begin + // Need to flush our last result out + assert(!S_READY); + end else if (S_VALID && !r_last) + begin + assert(S_VALID && S_READY); + end else begin + assert(!S_VALID || !S_READY); + end + end else begin + assert(!S_READY); + end + // }}} + end else begin + assert(!S_READY); + end + + always @(*) + if (!i_reset && !o_busy) + assert(!o_wr_cyc); +`endif + // }}} + + assign S_READY = !r_last && o_busy && (!o_wr_stb || !i_wr_stall) + && !wb_pipeline_full; + + // Keep Verilator happy + // {{{ + // Verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, i_wr_data }; + // Verilator lint_on UNUSED + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL + localparam F_LGDEPTH = LGPIPE+1; + reg f_past_valid; + (* anyconst *) reg [ADDRESS_WIDTH-1:0] f_cfg_addr; + (* anyconst *) reg [1:0] f_cfg_size; + (* anyconst *) reg f_cfg_inc; + wire [F_LGDEPTH-1:0] fwb_nreqs, fwb_nacks, fwb_outstanding; + reg [ADDRESS_WIDTH:0] f_posn, fwb_addr, fwb_posn; + reg [WBLSB-1:0] fr_sel_count, fr_sel_count_past; + reg [ADDRESS_WIDTH-1:0] r_addr; + + initial f_past_valid = 0; + always @(posedge i_clk) + f_past_valid <= 1; + + always @(*) + if (!f_past_valid) + assume(i_reset); + + //////////////////////////////////////////////////////////////////////// + // + // Control interface properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if (!f_past_valid || $past(i_reset)) + assume(!i_request); + else if ($past(i_request && o_busy)) + begin + assume(i_request); + assume($stable(i_inc)); + assume($stable(i_size)); + assume($stable(i_addr)); + end + + always @(posedge i_clk) + if (i_request && !o_busy) + begin + // r_inc <= i_inc; + // r_size <= i_size; + r_addr <= i_addr; + end + + always @(*) + if (!f_cfg_inc) case(f_cfg_size) + SZ_BYTE: begin end + SZ_16B: assume(f_cfg_addr[0] == 1'b0); + SZ_32B: assume(f_cfg_addr[1:0] == 2'b0); + SZ_BUS: assume(f_cfg_addr[WBLSB-1:0] == 0); + endcase + + always @(*) + if (i_request && !o_busy) + begin + assume(i_inc == f_cfg_inc); + assume(i_size == f_cfg_size); + assume(i_addr == f_cfg_addr); + end + + always @(*) + if (!i_reset && o_busy) + begin + assert(r_addr == f_cfg_addr); + assert(r_size == f_cfg_size); + assert(r_inc == f_cfg_inc); + + if (!r_inc) + case(r_size) + SZ_BYTE: begin end + SZ_16B: assert(r_addr[0] == 1'b0); + SZ_32B: assert(r_addr[1:0] == 2'b0); + SZ_BUS: assert(r_addr[WBLSB-1:0] == 0); + endcase + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Stream properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if (!f_past_valid || $past(i_reset)) + begin + assume(!S_VALID); + end else if ($past(S_VALID && !S_READY)) + begin + assume(S_VALID); + assume($stable(S_DATA)); + assume($stable(S_BYTES)); + assume($stable(S_LAST)); + end + + always @(*) + if (S_VALID) + begin + assume(S_BYTES <= DW/8); + assume(S_BYTES > 0); + + if (!S_LAST) + case(f_cfg_size) + 2'b11: assume(S_BYTES == 1); + 2'b10: assume(S_BYTES == 2); + 2'b01: assume(S_BYTES == 4); + 2'b00: assume(S_BYTES == (DW/8)); + endcase + else case(f_cfg_size) + 2'b11: assume(S_BYTES == 1); + 2'b10: assume(S_BYTES <= 2); + 2'b01: assume(S_BYTES <= 4); + 2'b00: assume(S_BYTES <= (DW/8)); + endcase + end + + always @(posedge i_clk) + if (i_reset || !o_busy) + f_posn <= 0; + else if (S_VALID && S_READY) + f_posn <= f_posn + S_BYTES; + + always @(*) + if (o_busy) + assume(!f_posn[ADDRESS_WIDTH] || (f_posn[ADDRESS_WIDTH-1:0]==0 + && r_last)); + + always @(*) + if (!i_reset && o_busy) + begin + if (r_last) + begin + end else case(f_cfg_size) + 2'b11: begin end + 2'b10: assert(f_posn[0] == 1'b0); + 2'b01: assert(f_posn[1:0] == 2'b0); + 2'b00: assert(f_posn[WBLSB-1:0] == 0); + endcase + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Wishbone properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + fwb_master #( + .AW(AW), .DW(DW), .F_LGDEPTH(F_LGDEPTH), + .F_OPT_DISCONTINUOUS(1'b1), + .F_MAX_STALL(2), + .F_MAX_ACK_DELAY(2) + ) fwb ( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + .i_wb_cyc( o_wr_cyc), + .i_wb_stb( o_wr_stb), + .i_wb_we( o_wr_we), + .i_wb_addr(o_wr_addr), + .i_wb_data(o_wr_data), + .i_wb_sel( o_wr_sel), + // + .i_wb_stall(i_wr_stall), + .i_wb_ack( i_wr_ack), + .i_wb_idata(i_wr_data), + .i_wb_err( i_wr_err), + // + .f_nreqs(fwb_nreqs), + .f_nacks(fwb_nacks), + .f_outstanding(fwb_outstanding) + // }}} + ); + + always @(*) + if (!i_reset && o_wr_stb) + assert(|o_wr_sel); + + always @(*) + if (o_wr_cyc) + assert(fwb_outstanding == wb_outstanding); + + initial fwb_posn = 0; + always @(posedge i_clk) + if (i_reset || !o_busy) + fwb_posn <= 0; + else if (o_wr_stb && !i_wr_stall) + fwb_posn <= fwb_posn + $countones(o_wr_sel); + + always @(*) + if (!i_reset && o_busy && !o_err) + assert(f_posn == fwb_posn + $countones(r_sel) + + (o_wr_stb ? $countones(o_wr_sel) : 0)); + + always @(*) + if (r_inc) + begin + fwb_addr = r_addr + fwb_posn; + + if (fwb_posn > 0) + case(r_size) + SZ_16B: fwb_addr[ 0] = 0; + SZ_32B: fwb_addr[1:0] = 0; + SZ_BUS: fwb_addr[WBLSB-1:0] = 0; + default: begin end + endcase + end else begin + // fwb_addr = r_addr + fwb_posn; + fwb_addr = r_addr; + + case(r_size) + SZ_BYTE: begin end + SZ_16B: fwb_addr[0] = 0; + SZ_32B: fwb_addr[1:0] = 0; + SZ_BUS: fwb_addr[WBLSB-1:0] = 0; + endcase + end + + always @(*) + if (!i_reset && o_busy && !r_last && !o_err) + // && (o_wr_stb || !fwb_addr[ADDRESS_WIDTH]) && !r_last) + begin + assert({ 1'b0, o_wr_addr } == fwb_addr[ADDRESS_WIDTH:WBLSB]); + case(r_size) + SZ_BUS: assert(subaddr == r_addr[WBLSB-1:0]); + SZ_16B: assert(subaddr == { fwb_addr[WBLSB-1:1], r_addr[0] }); + SZ_32B: assert(subaddr == { fwb_addr[WBLSB-1:2], r_addr[1:0] }); + default: + assert(subaddr == fwb_addr[WBLSB-1:0]); + endcase + end + + always @(*) + if (o_busy && fwb_addr[ADDRESS_WIDTH] && !r_last) + begin + assert(o_err); + // assert(fwb_addr[ADDRESS_WIDTH-1:0] <= DW/8); + end + + always @(*) + if (!o_busy || o_err) + assert(!o_wr_cyc); + + always @(*) + if (!i_reset && o_busy) + begin + if (!r_inc) + case(r_size) + SZ_BYTE: begin end + SZ_16B: assert(subaddr[0] == 1'b0); + SZ_32B: assert(subaddr[1:0] == 2'b00); + SZ_BUS: assert(subaddr == 0); + endcase else case(r_size) + SZ_BYTE: begin end + SZ_16B: assert(subaddr[0] == r_addr[0]); + SZ_32B: assert(subaddr[1:0] == r_addr[1:0]); + SZ_BUS: assert(subaddr == r_addr[WBLSB-1:0]); + endcase + end + + always @(*) + fr_sel_count = $countones(r_sel); + + always @(posedge i_clk) + if (!o_wr_sel || !i_wr_stall) + fr_sel_count_past <= fr_sel_count; + + always @(posedge i_clk) + if(!i_reset && o_busy && !o_err && !r_last && $past(S_VALID && S_READY)) + begin + assert(o_wr_stb); + assert($countones({ r_sel, o_wr_sel }) + == $past(S_BYTES + fr_sel_count)); + end + + always @(*) + if (!i_reset && o_busy) + begin + if (o_err) + assert(!o_wr_cyc); + if (f_posn == 0) + begin + assert(r_sel == 0); + assert(!o_wr_cyc); + assert(wb_outstanding == 0); + end else + case(r_size) + SZ_BYTE: assert(r_sel == 0); + SZ_16B: begin + if (OPT_LITTLE_ENDIAN) + begin + assert(r_sel[DW/8-1:1] == 0); + end else begin + assert(r_sel[DW/8-2:0] == 0); + end + + if (!o_wr_stb) begin end + else if (subaddr + 2 <= DW/8) + begin + assert(r_sel == 0); + end else if (!r_last) + begin + assert(r_sel[(OPT_LITTLE_ENDIAN) ? 0 : (DW/8-1)] == (subaddr+2 > DW/8)); + end else begin + assert(r_sel[(OPT_LITTLE_ENDIAN) ? 0 : (DW/8-1)] <= (subaddr+2 > DW/8)); + end end + SZ_32B: begin + if (OPT_LITTLE_ENDIAN) + begin + assert(r_sel[DW/8-1:3] == 0); + end else begin + assert(r_sel[DW/8-4:0] == 0); + end + + if (!o_wr_stb) begin end + else if (subaddr + 4 <= DW/8) + begin + assert(r_sel == 0); + /* + end else if (!r_last) + begin + assert($countones(r_sel) == subaddr+4-DW/8); + end else begin + assert($countones(r_sel) <= subaddr+4-DW/8); + */ + end end + SZ_BUS: begin + assert(!(&r_sel)); + if (subaddr == 0) + assert(r_sel == 0); + end + default: begin end + endcase + + if (OPT_LITTLE_ENDIAN) + begin + if (!o_wr_sel[DW/8-1]) + assert(!r_sel[0]); + end else begin + if (!o_wr_sel[0]) + assert(!r_sel[DW/8-1]); + end + + for(ik=0; ik 2) + assert(!r_sel[ik]); + SZ_BUS: if (ik >= r_addr[DW/8-1:0]) + assert(!r_sel[ik]); + default: begin end + endcase + end else begin + if (ik > 0 && !r_sel[DW/8-ik]) + begin + assert(!r_sel[DW/8-1-ik]); + end + + case(r_size) + SZ_32B: begin + /*if (ik >= 4 || subaddr + ik < DW/8 + || subaddr + ik - DW/8 >= 4) + // assert(!r_sel[subaddr+ik-DW/8]); + assert(!r_sel[DW/5-1-ik-subaddr]); + */ + end + SZ_BUS: begin + /* + if (ik > DW/8-1-i_addr[DW/8-1:0]) + assert(!r_sel[ik]); + */ + end + default: begin end + endcase + end + end + + always @(*) + if (!i_reset && o_busy && !r_inc) + assert(r_sel == 0); + + reg [WBLSB-1:0] f_sum; + + always @(*) + if (r_inc) + f_sum = fwb_posn[WBLSB-1:0] + r_addr[WBLSB-1:0]; + else + f_sum = fwb_posn[WBLSB-1:0]; + + always @(*) + if (!i_reset && o_busy && fwb_posn > 0) + begin + if (!r_last) case(r_size) + SZ_16B: assert(f_sum[ 0] == 0); + SZ_32B: assert(f_sum[1:0] == 0); + SZ_BUS: assert(f_sum[WBLSB-1:0] == 0); + default: begin end + endcase + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Contract properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + (* anyconst *) reg fc_check; + (* anyconst *) reg [ADDRESS_WIDTH:0] fc_posn; + (* anyconst *) reg [7:0] fc_byte; + + reg [ADDRESS_WIDTH:0] f_shift, fwb_shift; + reg [DW-1:0] fc_partial; + reg [2*DW-1:0] fc_partial_wb; + reg [2*DW/8-1:0] fc_partial_sel; + wire [DW-1:0] fz_data; + wire [DW/8-1:0] fz_sel; + + assign fz_data = 0; + assign fz_sel = 0; + + always @(*) + begin + f_shift = (fc_posn - f_posn); + f_shift[ADDRESS_WIDTH:WBLSB] = 0; + end + + always @(*) + if (OPT_LITTLE_ENDIAN) + fc_partial = S_DATA >> (8*f_shift); + else + fc_partial = S_DATA << (8*f_shift); + + wire fin_check, fwb_check; + assign fin_check = fc_check && S_VALID && (f_posn <= fc_posn) + && (fc_posn < f_posn + S_BYTES); + assign fwb_check = fc_check && o_busy && !o_err + && (fwb_posn <= fc_posn) + &&((o_wr_stb && fc_posn < fwb_posn + $countones(o_wr_sel)) + ||(!o_wr_stb && fc_posn < fwb_posn + $countones(r_sel))); + + always @(*) + if (fin_check) + begin + if (OPT_LITTLE_ENDIAN) + assume(fc_partial[7:0] == fc_byte); + else + assume(fc_partial[DW-1:DW-8] == fc_byte); + end + + always @(*) + begin + fwb_shift = 0; + if (r_inc) + begin + fwb_shift[WBLSB-1:0] = fc_posn[WBLSB-1:0] + - fwb_posn[WBLSB-1:0]; + if (o_wr_stb) + fwb_shift[WBLSB-1:0] = fwb_shift[WBLSB-1:0] + fwb_addr[WBLSB-1:0]; + end else + fwb_shift[WBLSB-1:0] = fc_posn[WBLSB-1:0] - fwb_posn[WBLSB-1:0] + + r_addr[WBLSB-1:0]; + end + + always @(*) + if (OPT_LITTLE_ENDIAN) + begin + if (o_wr_stb) + begin + fc_partial_wb ={ r_data, o_wr_data} >> (8*fwb_shift); + fc_partial_sel={ r_sel, o_wr_sel } >> fwb_shift; + end else begin + fc_partial_wb ={ fz_data, r_data} >> (8*fwb_shift); + fc_partial_sel={ fz_sel, r_sel } >> fwb_shift; + end + end else begin + if (o_wr_stb) + begin + fc_partial_wb ={ o_wr_data, r_data }<< (8*fwb_shift); + fc_partial_sel={ o_wr_sel, r_sel } << fwb_shift; + end else begin + fc_partial_wb ={ r_data,fz_data } << (8*fwb_shift); + fc_partial_sel={ r_sel, fz_sel } << fwb_shift; + end + end + + always @(*) + if (o_busy) + assert(fwb_posn <= f_posn); + + always @(*) + if (fwb_check) + begin + if (OPT_LITTLE_ENDIAN) + begin + assert(fc_partial_wb[7:0] == fc_byte); + assert(fc_partial_sel[0]); + end else begin + assert(fc_partial_wb[2*DW-1:2*DW-8] == fc_byte); + assert(fc_partial_sel[2*DW/8-1]); + end + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Cover properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if (!i_reset && !$past(i_reset)) + cover(i_request); + + always @(posedge i_clk) + if (!i_reset && !$past(i_reset)) + cover(o_busy); + + always @(posedge i_clk) + if (!i_reset && !$past(i_reset) && $past(o_busy)) + begin + cover(!o_busy); + + if (!o_busy) + begin + case({ r_inc, r_size }) + 3'b000: cover(f_posn > DW/8); + 3'b001: cover(f_posn > DW/8); + 3'b010: cover(f_posn > DW/8); + 3'b011: cover(f_posn > DW/8); + 3'b100: cover(f_posn > DW/8); + 3'b101: cover(f_posn > DW/8); + 3'b110: cover(f_posn > DW/8); + 3'b111: cover(f_posn > DW/8); + endcase + end + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // "Careless" assumptions + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(*) + case(f_cfg_size) + SZ_BYTE: begin end + SZ_BUS: begin end + SZ_16B: assume(f_cfg_addr[0] == 1'b0); + SZ_32B: assume(f_cfg_addr[1:0] == 2'b0); + endcase + + // }}} +`endif +// }}} +endmodule diff --git a/rtl/cpu/zipdma_txgears.v b/rtl/cpu/zipdma_txgears.v new file mode 100644 index 0000000..fb9439c --- /dev/null +++ b/rtl/cpu/zipdma_txgears.v @@ -0,0 +1,759 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: zipdma_txgears.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: ZipDMA -- Unpack bus words into 1, 2, 4, or more bytes per +// outgong word. This is to support peripherals which require +// 1, 2, or 4 byte transfers (only), as well as peripherals like memory +// with no such restrictions. +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module zipdma_txgears #( + // {{{ + parameter BUS_WIDTH = 512, + parameter [0:0] OPT_LITTLE_ENDIAN = 1'b0, + // Abbreviations + localparam DW = BUS_WIDTH + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Configuration + // {{{ + input wire i_soft_reset, + input wire [1:0] i_size, + // }}} + // Incoming Stream interface + // {{{ + input wire S_VALID, + output wire S_READY, + input wire [DW-1:0] S_DATA, + // How many bytes are valid? + input wire [$clog2(DW/8):0] S_BYTES, + input wire S_LAST, + // }}} + // Outgoing Stream interface + // {{{ + output wire M_VALID, + input wire M_READY, + output wire [DW-1:0] M_DATA, + // How many bytes are valid? + output wire [$clog2(DW/8):0] M_BYTES, + output wire M_LAST + // }}} + // }}} + ); + + // Local declarations + // {{{ + localparam WBLSB = $clog2(DW/8); + localparam [1:0] SZ_BYTE = 2'b11, + SZ_16B = 2'b10, + SZ_32B = 2'b01, + SZ_BUS = 2'b00; + reg m_valid, m_last, r_last, r_next; + reg [DW-1:0] sreg; + reg [WBLSB:0] m_bytes, fill; + // }}} + + // sreg, fill + // {{{ + initial {sreg, fill } = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + begin + sreg <= 0; + fill <= 0; + end else if (S_VALID && S_READY) + begin + sreg <= S_DATA; + fill <= S_BYTES; + end else if (M_VALID && M_READY) + begin + if (M_LAST) + { sreg, fill } <= 0; + else if (OPT_LITTLE_ENDIAN) + begin + case(i_size) + SZ_BYTE: begin sreg <= sreg >> 8; fill <= fill - 1; end + SZ_16B: begin sreg <= sreg >> 16; fill <= fill - 2; end + SZ_32B: begin sreg <= sreg >> 32; fill <= fill - 4; end + SZ_BUS: begin sreg <= 0; fill <= 0; end + endcase + end else begin + case(i_size) + SZ_BYTE: begin sreg <= sreg << 8; fill <= fill - 1; end + SZ_16B: begin sreg <= sreg << 16; fill <= fill - 2; end + SZ_32B: begin sreg <= sreg << 32; fill <= fill - 4; end + SZ_BUS: begin sreg <= 0; fill <= 0; end + endcase + end + end +`ifdef FORMAL + always @(*) + if (!i_reset) + assert(fill <= (DW/8)); +`endif + // }}} + + // m_valid + // {{{ + initial m_valid = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + m_valid <= 0; + else if (!M_VALID || M_READY) + begin + if (S_VALID && S_READY) + m_valid <= 1'b1; + else case(i_size) + SZ_BYTE: m_valid <= (fill > 1); + SZ_16B: m_valid <= (fill > 2); + SZ_32B: m_valid <= (fill > 4); + SZ_BUS: m_valid <= 0; + endcase + end +`ifdef FORMAL + always @(*) + if (m_valid) + assert(m_bytes <= fill); +`endif + // }}} + + // m_bytes + // {{{ + generate if (BUS_WIDTH > 32) + begin : GEN_MBYTES + // {{{ + initial m_bytes = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + m_bytes <= 0; + else if (S_VALID && S_READY) + begin + case(i_size) + SZ_BYTE: m_bytes <= 1; + SZ_16B: m_bytes <= (S_BYTES > 2) ? 2 : S_BYTES; + SZ_32B: m_bytes <= (S_BYTES > 4) ? 4 : S_BYTES; + SZ_BUS: m_bytes <= S_BYTES; + endcase + end else if (!M_VALID || M_READY) + begin + case(i_size) + SZ_BYTE: m_bytes <= 1; + SZ_16B: m_bytes <= (fill >= 4) ? 2 + : (&fill[1:0]) ? 1 : 0; + SZ_32B: m_bytes <= (fill >= 8) ? 4 + : (fill[2]) ? ({ {(WBLSB-1){1'b0}}, fill[1:0] }) + : 0; + SZ_BUS: m_bytes <= 0; + endcase + end + // }}} + end else begin : MIN_MBYTES + // {{{ + initial m_bytes = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + m_bytes <= 0; + else if (S_VALID && S_READY) + begin + casez(i_size) + SZ_BYTE: m_bytes <= 1; + SZ_16B: m_bytes <= (S_BYTES > 2) ? 2 : S_BYTES; + default: m_bytes <= S_BYTES; + endcase + end else if (!M_VALID || M_READY) + begin + casez(i_size) + SZ_BYTE: m_bytes <= 1; + SZ_16B: m_bytes <= (fill >= 4) ? 2 + : (&fill[1:0]) ? 1 : 0; + default: m_bytes <= 0; + endcase + end + // }}} + end endgenerate +`ifdef FORMAL + // {{{ + always @(*) + if (!i_reset && M_VALID) + begin + assert(m_bytes > 0); + assert(m_bytes <= fill); + if (M_LAST) + assert(m_bytes == fill); + + case(i_size) + SZ_BYTE: assert(m_bytes == 1); + SZ_16B: begin + assert(m_bytes <= 2); + if (m_bytes < 2) + assert(M_LAST && m_bytes == fill); + end + SZ_32B: begin + assert(m_bytes <= 4); + if (m_bytes < 4) + assert(M_LAST && m_bytes == fill); + end + SZ_BUS: begin + assert(m_bytes <= DW/8); + if (m_bytes < (DW/8)) + assert(M_LAST && m_bytes == fill); + end + endcase + end + // }}} +`endif + // }}} + + // r_next -- Are we on our last word? + // {{{ + // Only allow S_READY if r_next is true, so r_next must be true when + // we output the last word of the shift register. + generate if (BUS_WIDTH > 32) + begin : GEN_NEXT + // {{{ + initial r_next = 1; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + r_next <= 1; + else if (M_VALID && M_READY && M_LAST) + r_next <= 1; + else if (S_VALID && S_READY) + begin + case(i_size) + SZ_BYTE:r_next <= (S_BYTES == 1); + SZ_16B: r_next <= (S_BYTES <= 2); + SZ_32B: r_next <= (S_BYTES <= 4); + SZ_BUS: r_next <= 1; + endcase + + if (S_LAST) + r_next <= 0; + end else if (M_VALID && M_READY) + begin + case(i_size) + SZ_BYTE:r_next <= (fill <= 2); + SZ_16B: r_next <= (fill <= 4); + SZ_32B: r_next <= (fill <= 8); + SZ_BUS: r_next <= 1; + endcase + + if (r_last) + r_next <= 0; + end + // }}} + end else begin : MIN_NEXT + // {{{ + initial r_next = 1; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + r_next <= 1; + else if (M_VALID && M_READY && M_LAST) + r_next <= 1; + else if (S_VALID && S_READY) + begin + casez(i_size) + SZ_BYTE: r_next <= (S_BYTES == 1); + SZ_16B: r_next <= (S_BYTES <= 2); + default: r_next <= 1; + endcase + + if (S_LAST) + r_next <= 0; + end else if (M_VALID && M_READY) + begin + casez(i_size) + SZ_BYTE: r_next <= (fill <= 2); + SZ_16B: r_next <= (fill <= 4); + default: r_next <= 1; + endcase + + if (r_last) + r_next <= 0; + end + // }}} + end endgenerate + +`ifdef FORMAL + // {{{ + reg [1:0] f_mid_packet; + + // f_mid_packet + // {{{ + initial f_mid_packet = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + f_mid_packet <= 0; + else if (S_VALID && S_READY) + f_mid_packet <= (S_LAST) ? 2'b10 : 2'b01; + else if (M_VALID && M_READY && M_LAST) + f_mid_packet <= 2'b00; + + always @(*) + assert(f_mid_packet != 2'b11); + // }}} + + always @(*) + if (!i_reset) begin + + assert(f_mid_packet[1] == (r_last || m_last)); + + if (f_mid_packet[1]) + begin + assert(!r_next); + end else case(i_size) + SZ_BYTE:assert(r_next == (fill <= 1)); + SZ_16B: assert(r_next == (fill <= 2)); + SZ_32B: assert(r_next == (fill <= 4)); + SZ_BUS: assert(r_next); + endcase + end + // }}} +`endif + // }}} + + // r_last, m_last + // {{{ + generate if (BUS_WIDTH > 32) + begin : GEN_LAST + // {{{ + initial { r_last, m_last } = 2'b00; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + { r_last, m_last } <= 0; + else if (S_VALID && S_READY) + begin + case(i_size) + SZ_BYTE: { r_last, m_last } <= { (S_BYTES > 1), (S_BYTES == 1) }; + SZ_16B: { r_last, m_last } <= { (S_BYTES > 2), (S_BYTES <= 2) }; + SZ_32B: { r_last, m_last } <= { (S_BYTES > 4), (S_BYTES <= 4) }; + SZ_BUS: { r_last, m_last } <= { 1'b0, S_LAST }; + endcase + + if (!S_LAST) + { r_last, m_last } <= 2'b00; + end else if (M_VALID && M_READY) + begin + case(i_size) + SZ_BYTE:m_last <= r_last && (fill <= 2); + SZ_16B: m_last <= r_last && (fill <= 4); + SZ_32B: m_last <= r_last && (fill <= 8); + SZ_BUS: m_last <= 0; + endcase + + case(i_size) + SZ_BYTE:r_last <= r_last && (fill > 2); + SZ_16B: r_last <= r_last && (fill > 4); + SZ_32B: r_last <= r_last && (fill > 8); + SZ_BUS: r_last <= 0; + endcase + + // if (M_LAST) + // { r_last, m_last } <= 2'b00; + end + // }}} + end else begin : MIN_LAST + // {{{ + initial { r_last, m_last } = 2'b00; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + { r_last, m_last } <= 0; + else if (S_VALID && S_READY) + begin + casez(i_size) + SZ_BYTE: { r_last, m_last } <= { (S_BYTES > 1), (S_BYTES == 1) }; + SZ_16B: { r_last, m_last } <= { (S_BYTES > 2), (S_BYTES <= 2) }; + default: { r_last, m_last } <= { (S_BYTES > 4), (S_BYTES <= 4) }; + endcase + + if (!S_LAST) + { r_last, m_last } <= 2'b00; + end else if (!M_VALID || M_READY) + begin + casez(i_size) + SZ_BYTE: m_last <= r_last && (fill <= 2); + SZ_16B: m_last <= r_last && (fill <= 4); + default: m_last <= 0; + endcase + + casez(i_size) + SZ_BYTE: r_last <= r_last && (fill > 2); + SZ_16B: r_last <= r_last && (fill > 4); + default: r_last <= 0; + endcase + + // if (M_LAST) + // { r_last, m_last } <= 2'b00; + end + // }}} + end endgenerate + +`ifdef FORMAL + always @(posedge i_clk) + if (f_past_valid && $past(!i_reset && M_VALID && M_READY && M_LAST)) + assert({ r_last, m_last } == 2'b00); +`endif + // }}} + + assign M_VALID = m_valid; + assign M_DATA = sreg; + assign M_BYTES = m_bytes; + assign M_LAST = m_last; + + assign S_READY = !M_VALID || (M_READY && r_next); +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL + localparam F_LGCOUNT = 16; + reg f_past_valid; + (* anyconst *) reg [1:0] f_cfg_size; + reg [F_LGCOUNT-1:0] f_rcvd, f_sent; + + initial f_past_valid = 0; + always @(posedge i_clk) + f_past_valid <= 1; + + always @(*) + if (!f_past_valid) + assume(i_reset); + + always @(*) + if (!i_reset) + assume(i_size == f_cfg_size); + + //////////////////////////////////////////////////////////////////////// + // + // Incoming stream properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // Basic stream properties + // {{{ + always @(posedge i_clk) + if (!f_past_valid || $past(i_reset || i_soft_reset)) + assume(!S_VALID); + else if ($past(S_VALID && !S_READY)) + begin + assume(S_VALID); + assume($stable(S_DATA)); + assume($stable(S_BYTES)); + assume($stable(S_LAST)); + end + // }}} + + // Properties of S_BYTES: incoming words are packed + // {{{ + always @(*) + if (!i_reset && S_VALID) + begin + assume(S_BYTES > 0); + assume(S_BYTES <= (DW/8)); + if (!S_LAST) + assume(S_BYTES == (DW/8)); + end + // }}} + + // f_rcvd + // {{{ + initial f_rcvd = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + f_rcvd = 0; + else if (S_VALID && S_READY) + begin + if (S_LAST) + f_rcvd <= 0; + else + f_rcvd <= f_rcvd + S_BYTES; + end + + always @(*) + assume(!f_rcvd[F_LGCOUNT-1]); + + always @(*) + if (f_mid_packet == 2'b00 || f_mid_packet == 2'b10) + begin + assert(f_rcvd == 0); + end else + assert(f_rcvd > 0); + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Outgoing stream properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // Basic stream property + // {{{ + always @(posedge i_clk) + if (!f_past_valid || $past(i_reset || i_soft_reset)) + assert(!M_VALID); + else if ($past(M_VALID && !M_READY)) + begin + assert(M_VALID); + assert($stable(M_DATA)); + assert($stable(M_BYTES)); + assert($stable(M_LAST)); + end + // }}} + + // Bounding/checking M_LAST + // {{{ + always @(*) + if (!i_reset && M_VALID) + begin + assert(M_BYTES > 0); + assert(M_BYTES <= (DW/8)); + + if (!M_LAST) + case(f_cfg_size) + SZ_BYTE: assert(M_BYTES == 1); + SZ_16B: assert(M_BYTES == 2); + SZ_32B: assert(M_BYTES == 4); + SZ_BUS: assert(M_BYTES == (DW/8)); + endcase + else + case(f_cfg_size) + SZ_BYTE: assert(M_BYTES == 1); + SZ_16B: assert(M_BYTES <= 2); + SZ_32B: assert(M_BYTES <= 4); + SZ_BUS: assert(M_BYTES <= (DW/8)); + endcase + end + // }}} + + // Bounding/checking fill + // {{{ + always @(*) + if (!i_reset) + begin + assert(!r_last || !M_LAST); + if (r_last || M_LAST) + assert(M_VALID); + + if (r_last) + case(f_cfg_size) + SZ_BYTE: assert(fill > 1); + SZ_16B: assert(fill > 2); + SZ_32B: assert(fill > 4); + SZ_BUS: assert(0); + endcase + + if (M_LAST) + begin + assert(fill > 0); + + case(f_cfg_size) + 2'b11: assert(fill == 1); + SZ_16B: assert(fill <= 2); + SZ_32B: assert(fill <= 4); + SZ_BUS: begin end + endcase + end + end + + always @(*) + if (!i_reset) + begin + assert(fill <= (DW/8)); + assert(M_VALID == (fill > 0)); + + if (M_LAST) + assert(fill == M_BYTES); + + if (!M_LAST && !r_last) + case(f_cfg_size) + SZ_BYTE: begin end // assert(fill > 0); + SZ_16B: assert(fill[0] == 1'b0); + SZ_32B: assert(fill[1:0] == 2'b00); + SZ_BUS: assert(fill[WBLSB-1:0] == 0); + endcase + end + // }}} + + // f_sent + // {{{ + initial f_sent = 0; + always @(posedge i_clk) + if (i_reset || i_soft_reset) + f_sent <= 0; + else if (M_VALID && M_READY) + begin + if (M_LAST) + f_sent <= 0; + else + f_sent <= f_sent + M_BYTES; + end + + always @(*) + begin + assume(!f_sent[F_LGCOUNT-1]); + + if (!r_last && !m_last) + begin + assert(f_sent + fill == f_rcvd); + assert(f_sent <= f_rcvd); + end + end + + always @(*) + if (f_mid_packet == 2'b00) + begin + assert(f_sent == 0); + end else if (f_mid_packet == 2'b10) + assert(M_VALID || f_sent > 0); + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // "Contract" properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + (* anyconst *) reg fc_check; + (* anyconst *) reg [F_LGCOUNT-1:0] fc_posn; + (* anyconst *) reg [7:0] fc_byte; + + wire fs_check, fm_check; + reg [WBLSB-1:0] fs_shift, fm_shift; + reg [DW-1:0] fs_shifted, fm_shifted; + + // Slave assumption + // {{{ + assign fs_check = fc_check && S_VALID && f_rcvd <= fc_posn + && (fc_posn < f_rcvd + S_BYTES); + + always @(*) + fs_shift = fc_posn - f_rcvd; + + always @(*) + if (OPT_LITTLE_ENDIAN) + fs_shifted = S_DATA >> (8*fs_shift); + else + fs_shifted = S_DATA << (8*fs_shift); + + always @(*) + if (!i_reset && fs_check) + begin + if (OPT_LITTLE_ENDIAN) + assume(fs_shifted[7:0] == fc_byte); + else + assume(fs_shifted[DW-1:DW-8] == fc_byte); + end + // }}} + + // Master assertion + // {{{ + assign fm_check = fc_check && f_sent <= fc_posn + && (fc_posn < f_sent + fill); + + always @(*) + fm_shift = fc_posn - f_sent; + + always @(*) + if (OPT_LITTLE_ENDIAN) + fm_shifted = sreg >> (8*fm_shift); + else + fm_shifted = sreg << (8*fm_shift); + + always @(*) + if (!i_reset && fm_check) + begin + if (OPT_LITTLE_ENDIAN) + begin + assert(fm_shifted[7:0] == fc_byte); + end else begin + assert(fm_shifted[DW-1:DW-8] == fc_byte); + end + end + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Cover properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + always @(posedge i_clk) + if (!i_reset && M_VALID && M_READY && M_LAST) + begin + cover(i_size == SZ_BYTE && f_sent > DW/8); + cover(i_size == SZ_16B && f_sent > DW/8); + cover(i_size == SZ_32B && f_sent > DW/8); + cover(i_size == SZ_BUS && f_sent > DW/8); + + cover(i_size == SZ_BYTE && f_sent > 2*DW/8+1); + cover(i_size == SZ_16B && f_sent > 2*DW/8+1); + cover(i_size == SZ_32B && f_sent > 2*DW/8+1); + cover(i_size == SZ_BUS && f_sent > 2*DW/8+1); + + cover(i_size == SZ_BUS && f_sent > 2*DW/8+2); + cover(i_size == SZ_BUS && f_sent > 2*DW/8+3); + cover(i_size == SZ_BUS && f_sent > 2*DW/8+4); + end + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // "Careless" assumptions + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // }}} +`endif +// }}} +endmodule diff --git a/rtl/cpu/zipjiffies.v b/rtl/cpu/zipjiffies.v index f97cd99..3ffdabf 100644 --- a/rtl/cpu/zipjiffies.v +++ b/rtl/cpu/zipjiffies.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zipjiffies.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: This peripheral is motivated by the Linux use of 'jiffies'. @@ -26,7 +26,7 @@ // register return the current value of the counter. Writes within // the (N-1) bit space following the current time set an interrupt. // Writes of values that occurred in the last 2^(N-1) ticks will be -// ignored. The timer then interrupts when it's value equals that time. +// ignored. The timer then interrupts when its value equals that time. // Multiple writes cause the jiffies timer to select the nearest possible // interrupt. Upon an interrupt, the next interrupt time/value is cleared // and will need to be reset if the CPU wants to get notified again. With @@ -44,11 +44,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -61,40 +61,44 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module zipjiffies(i_clk, i_reset, i_ce, - i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, - o_wb_stall, o_wb_ack, o_wb_data, - o_int); - parameter BW = 32; - input wire i_clk, i_reset, i_ce; - // Wishbone inputs - input wire i_wb_cyc, i_wb_stb, i_wb_we; - input wire [(BW-1):0] i_wb_data; - // Wishbone outputs - output wire o_wb_stall; - output reg o_wb_ack; - output wire [(BW-1):0] o_wb_data; - // Interrupt line - output reg o_int; - +// }}} +module zipjiffies #( + parameter BW = 32 + ) ( + // {{{ + input wire i_clk, i_reset, i_ce, + // Wishbone inputs + input wire i_wb_cyc, i_wb_stb, i_wb_we, + input wire [(BW-1):0] i_wb_data, + input wire [BW/8-1:0] i_wb_sel, + // Wishbone outputs + output wire o_wb_stall, + output reg o_wb_ack, + output wire [(BW-1):0] o_wb_data, + // Interrupt line + output reg o_int + // }}} + ); + + // Local declarations + // {{{ reg [(BW-1):0] r_counter; // - reg int_set, new_set; + reg int_set, new_set, int_now; reg [(BW-1):0] int_when, new_when; - wire signed [(BW-1):0] till_when; - reg signed [(BW-1):0] till_wb; + reg signed [(BW-1):0] till_wb, till_when; + // }}} - // + // r_counter + // {{{ // Our counter logic: The counter is always counting up--it cannot // be stopped or altered. It's really quite simple. Okay, not quite. // We still support the clock enable line. We do this in order to @@ -102,19 +106,32 @@ module zipjiffies(i_clk, i_reset, i_ce, // debugger, the timer's all slow down so that everything can be stepped // together, one clock at a time. // - initial r_counter = 0; always @(posedge i_clk) if (i_reset) r_counter <= 0; else if (i_ce) r_counter <= r_counter+1; + // }}} - // + // int_now + // {{{ + initial int_now = 0; + always @(posedge i_clk) + if (i_reset) + int_now <= 0; + else if (i_ce) + int_now <= ((r_counter + 1) == (int_when)); + else + int_now <= 1'b0; + // }}} + + // new_set, new_when + // {{{ // Writes to the counter set an interrupt--but only if they are in the // future as determined by the signed result of an unsigned subtract. // - assign till_when = int_when-r_counter; + // assign till_when = int_when-r_counter; // assign till_wb = new_when-r_counter; initial new_set = 1'b0; @@ -128,18 +145,26 @@ module zipjiffies(i_clk, i_reset, i_ce, till_wb <= (i_wb_data - r_counter - (i_ce ? 1:0)); + till_when <= (int_when - i_wb_data); + if (i_reset) new_set <= 1'b0; end + // }}} + // o_int, int_set + // {{{ initial o_int = 1'b0; initial int_set = 1'b0; always @(posedge i_clk) if (i_reset) begin + // {{{ o_int <= 0; int_set <= 0; + // }}} end else begin + // {{{ o_int <= 1'b0; if ((i_ce)&&(int_set)&&(r_counter == int_when)) // Interrupts are self-clearing @@ -149,15 +174,21 @@ module zipjiffies(i_clk, i_reset, i_ce, if ((new_set)&&(till_wb > 0)) int_set <= 1'b1; - else if ((i_ce)&&(r_counter == int_when)) + else if (int_now) int_set <= 1'b0; + // }}} end + // }}} + // int_when + // {{{ always @(posedge i_clk) - if ((new_set)&&(till_wb > 0)&&((till_wb 0)&&((till_when[BW-1])||(!int_set))) int_when <= new_when; + // }}} - // + // o_wb_ack + // {{{ // Acknowledge any wishbone accesses -- everything we did took only // one clock anyway. // @@ -167,135 +198,31 @@ module zipjiffies(i_clk, i_reset, i_ce, o_wb_ack <= 1'b0; else o_wb_ack <= i_wb_stb; + // }}} assign o_wb_data = r_counter; assign o_wb_stall = 1'b0; // Make verilator happy + // {{{ + // verilator coverage_off // verilator lint_off UNUSED wire unused; - assign unused = i_wb_cyc; + assign unused = &{ 1'b0, i_wb_cyc, i_wb_sel }; // verilator lint_on UNUSED + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL - reg f_past_valid; - initial f_past_valid = 1'b0; - always @(posedge i_clk) - f_past_valid <= 1'b1; - - //////////////////////////////////////////////// - // - // - // Assumptions about our inputs - // - // - //////////////////////////////////////////////// - // - // Some basic WB assumtions - - // We will not start out in a wishbone cycle - initial assume(!i_wb_cyc); - - // Following any reset the cycle line will be low - always @(posedge i_clk) - if ((f_past_valid)&&($past(i_reset))) - assume(!i_wb_cyc); - - // Anytime the stb is high, the cycle line must also be high - always @(posedge i_clk) - assume((!i_wb_stb)||(i_wb_cyc)); - - - //////////////////////////////////////////////// - // - // - // Assumptions about our bus outputs - // - // - //////////////////////////////////////////////// - // - - // We never stall the bus - always @(*) - assert(!o_wb_stall); - // We always ack every transaction on the following clock - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset))&&($past(i_wb_stb))) - assert(o_wb_ack); - else - assert(!o_wb_ack); - - - //////////////////////////////////////////////// - // - // - // Assumptions about our internal state and our outputs - // - // - //////////////////////////////////////////////// - // - always @(posedge i_clk) - if ((f_past_valid)&&($past(i_reset))) - begin - assert(!o_wb_ack); - end - - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset))&&($past(i_wb_stb)) - &&($past(i_wb_we))) - assert(new_when == $past(i_wb_data)); - - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset))&&($past(i_wb_stb)) - &&($past(i_wb_we))) - assert(new_set); - else - assert(!new_set); - - // - // - // - - always @(posedge i_clk) - if ((f_past_valid)&&($past(i_reset))) - assert(!o_int); - - always @(posedge i_clk) - if ((f_past_valid)&&($past(i_reset))) - begin - assert(!int_set); - assert(!new_set); - end - - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset))&&($past(new_set)) - &&(!$past(till_wb[BW-1])) - &&($past(till_wb) > 0)) - assert(int_set); - - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset))&&($past(i_ce)) - &&($past(r_counter)==$past(int_when))) - begin - assert((o_int)||(!$past(int_set))); - assert((!int_set)||($past(new_set))); - end - - always @(posedge i_clk) - if ((f_past_valid)&&(!$past(i_reset))&&(!$past(new_set))&&(!$past(int_set))) - assert(!int_set); - - always @(posedge i_clk) - if ((!f_past_valid)||($past(i_reset))) - assert(!o_int); - else if (($past(new_set))&&($past(till_wb) < 0)) - assert(o_int); - - always @(posedge i_clk) - if ((f_past_valid)&& - ((!$past(new_set)) - ||($past(till_wb[BW-1])) - ||($past(till_wb == 0)))) - assert(int_when == $past(int_when)); - // +// Formal properties are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/zipsystem.v b/rtl/cpu/zipsystem.v index 06b4f15..cd5095f 100644 --- a/rtl/cpu/zipsystem.v +++ b/rtl/cpu/zipsystem.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zipsystem.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: This portion of the ZIP CPU implements a number of soft @@ -62,11 +62,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -79,407 +79,744 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none +// }}} // -`include "cpudefs.v" -// -`define RESET_BIT 6 -`define STEP_BIT 8 -`define HALT_BIT 10 -`define CLEAR_CACHE_BIT 11 -// -// While I hate adding delays to any bus access, this next delay is required -// to make timing close in my Basys-3 design. -`define DELAY_DBG_BUS -// -// `define DELAY_EXT_BUS -// -// -// If space is tight, you might not wish to have your performance and -// accounting counters, so let's make those optional here -// Without this flag, Slice LUT count is 3315 (ZipSystem),2432 (ZipCPU) -// When including counters, -// Slice LUTs ZipSystem ZipCPU -// With Counters 3315 2432 -// Without Counters 2796 2046 - -// -// Now, where am I placing all of my peripherals? -`define PERIPHBASE 32'hc0000000 -`define INTCTRL 8'h0 // -`define WATCHDOG 8'h1 // Interrupt generates reset signal -`define BUSWATCHDOG 8'h2 // Sets IVEC[0] -`define CTRINT 8'h3 // Sets IVEC[5] -`define TIMER_A 8'h4 // Sets IVEC[4] -`define TIMER_B 8'h5 // Sets IVEC[3] -`define TIMER_C 8'h6 // Sets IVEC[2] -`define JIFFIES 8'h7 // Sets IVEC[1] - - -`ifdef INCLUDE_ACCOUNTING_COUNTERS -`define MSTR_TASK_CTR 8'h08 -`define MSTR_MSTL_CTR 8'h09 -`define MSTR_PSTL_CTR 8'h0a -`define MSTR_INST_CTR 8'h0b -`define USER_TASK_CTR 8'h0c -`define USER_MSTL_CTR 8'h0d -`define USER_PSTL_CTR 8'h0e -`define USER_INST_CTR 8'h0f -`endif - -`ifdef OPT_MMU -`define MMU_ADDR 8'h80 -`endif - -// Although I have a hole at 5'h2, the DMA controller requires four wishbone -// addresses, therefore we place it by itself and expand our address bus -// width here by another bit. -`define DMAC 5'h10 - -// `define RTC_CLOCK 32'hc0000008 // A global something -// `define BITREV 32'hc0000003 -// -// DBGCTRL -// 10 HALT -// 9 HALT(ED) -// 8 STEP (W=1 steps, and returns to halted) -// 7 INTERRUPT-FLAG -// 6 RESET_FLAG -// ADDRESS: -// 5 PERIPHERAL-BIT -// [4:0] REGISTER-ADDR -// DBGDATA -// read/writes internal registers +// Debug address space: +// {{{ +// 0-15 0x0? Supervisors registers +// 16-31 0x1? User registers +// 32-63 0x2? CPU command register (singular, one register only) +// 64 0x40 Interrupt controller +// 65 0x41 Watchdog +// 66 0x42 Bus watchdog +// 67 0x43 CTRINT +// 68 0x44 Timer A +// 69 0x45 Timer B +// 70 0x46 Timer C +// 71 0x47 Jiffies +// 72 0x48 Master task counter +// 73 0x49 Master task counter +// 74 0x4a Master task counter +// 75 0x4b Master instruction counter +// 76 0x4c User task counter +// 77 0x4d User task counter +// 78 0x4e User task counter +// 79 0x4f User instruction counter +// 80 0x50 DMAC Control/Status register +// 81 0x51 DMAC Length +// 82 0x52 DMAC Read (source) address +// 83 0x53 DMAC Write (destination) address // +/////// /////// /////// /////// // -// -`ifdef OPT_TRADITIONAL_PFCACHE -`define LGICACHE_DEFAULT 10 +// (MMU ... is not available via debug bus) +// }}} +module zipsystem #( + // {{{ + parameter RESET_ADDRESS=32'h1000_0000, + ADDRESS_WIDTH=32, + parameter BUS_WIDTH=32, // Bus data width + localparam DBG_WIDTH=32, + // CPU options + // {{{ + parameter [0:0] OPT_PIPELINED=1, + parameter [0:0] OPT_EARLY_BRANCHING=OPT_PIPELINED, + // OPT_LGICACHE + // {{{ + parameter OPT_LGICACHE=10, + // }}} + // OPT_LGDCACHE + // {{{ + // Set to zero for no data cache + parameter OPT_LGDCACHE=10, + // }}} + parameter [0:0] START_HALTED=1, + parameter [0:0] OPT_DISTRIBUTED_REGS=1, + parameter EXTERNAL_INTERRUPTS=1, + // OPT_MPY + // {{{ + parameter OPT_MPY = 3, + // }}} + // OPT_DIV + // {{{ + parameter [0:0] OPT_DIV=1, + // }}} + // OPT_SHIFTS + // {{{ + parameter [0:0] OPT_SHIFTS = 1, + // }}} + // OPT_FPU + // {{{ + parameter [0:0] OPT_FPU = 0, + // }}} + parameter [0:0] OPT_CIS=1, + parameter [0:0] OPT_LOCK=1, + parameter [0:0] OPT_USERMODE=1, + parameter [0:0] OPT_DBGPORT=START_HALTED, + parameter [0:0] OPT_TRACE_PORT=1, + parameter [0:0] OPT_PROFILER=0, + parameter [0:0] OPT_LOWPOWER=0, + // }}} + // Local bus options + // {{{ + // OPT_DMA + // {{{ + parameter [0:0] OPT_DMA=1, + parameter DMA_LGMEM = 10, + // }}} + // OPT_ACCOUNTING + // {{{ + parameter [0:0] OPT_ACCOUNTING = 1'b1, + // }}} + // Bus delay options + // {{{ + // While I hate adding delays to any bus access, this next + // delay is required to make timing close in my Basys-3 design. + parameter [0:0] DELAY_DBG_BUS = 1'b1, + // + parameter [0:0] DELAY_EXT_BUS = 1'b0, + // }}} +`ifdef VERILATOR + parameter [0:0] OPT_SIM=1'b1, + parameter [0:0] OPT_CLKGATE = OPT_LOWPOWER, `else -`define LGICACHE_DEFAULT 0 + parameter [0:0] OPT_SIM=1'b0, + parameter [0:0] OPT_CLKGATE = 1'b0, `endif -// -`ifdef OPT_DCACHE -`define LGDCACHE_DEFAULT 10 -`else -`define LGDCACHE_DEFAULT 0 -`endif -module zipsystem(i_clk, i_reset, + // }}} + parameter RESET_DURATION = 0, + // Short-cut names + // {{{ + localparam // Derived parameters + // PHYSICAL_ADDRESS_WIDTH=ADDRESS_WIDTH, + PAW=ADDRESS_WIDTH-$clog2(BUS_WIDTH/8) + // }}} + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Debugger interface -- ZERO THESE WIRES FOR SYNTHESIS + // {{{ + input wire i_sim_write, + input wire [6:0] i_sim_addr, + input wire [31:0] i_sim_data, + // }}} // Wishbone master interface from the CPU - o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, o_wb_sel, - i_wb_stall, i_wb_ack, i_wb_data, i_wb_err, + // {{{ + output wire o_wb_cyc, o_wb_stb, o_wb_we, + output wire [PAW-1:0] o_wb_addr, + output wire [BUS_WIDTH-1:0] o_wb_data, + output wire [BUS_WIDTH/8-1:0] o_wb_sel, + input wire i_wb_stall, i_wb_ack, + input wire [BUS_WIDTH-1:0] i_wb_data, + input wire i_wb_err, + // }}} // Incoming interrupts - i_ext_int, + input wire [(EXTERNAL_INTERRUPTS-1):0] i_ext_int, // Our one outgoing interrupt - o_ext_int, + output wire o_ext_int, // Wishbone slave interface for debugging purposes - i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr, i_dbg_data, - i_dbg_sel, - o_dbg_stall, o_dbg_ack, o_dbg_data -`ifdef DEBUG_SCOPE - , o_cpu_debug -`endif - ); - parameter RESET_ADDRESS=32'h1000_0000, ADDRESS_WIDTH=30, - LGICACHE=`LGICACHE_DEFAULT, - LGDCACHE=`LGDCACHE_DEFAULT; // Set to zero for no data cache - parameter [0:0] START_HALTED=1; - parameter EXTERNAL_INTERRUPTS=1, -`ifdef OPT_MULTIPLY - IMPLEMENT_MPY = `OPT_MULTIPLY; -`else - IMPLEMENT_MPY = 0; -`endif - parameter [0:0] -`ifdef OPT_DIVIDE - IMPLEMENT_DIVIDE=1, -`else - IMPLEMENT_DIVIDE=0, -`endif -`ifdef OPT_IMPLEMENT_FPU - IMPLEMENT_FPU=1, -`else - IMPLEMENT_FPU=0, -`endif - IMPLEMENT_LOCK=1; - parameter RESET_DURATION = 0; - localparam // Derived parameters - PHYSICAL_ADDRESS_WIDTH=ADDRESS_WIDTH, - PAW=ADDRESS_WIDTH, + // {{{ + input wire i_dbg_cyc, i_dbg_stb, i_dbg_we, + input wire [6:0] i_dbg_addr, + input wire [DBG_WIDTH-1:0] i_dbg_data, + input wire [DBG_WIDTH/8-1:0] i_dbg_sel, + output wire o_dbg_stall, + output wire o_dbg_ack, + output wire [DBG_WIDTH-1:0] o_dbg_data, + // }}} + output wire [31:0] o_cpu_debug, + // + output wire o_prof_stb, + output wire [ADDRESS_WIDTH-1:0] o_prof_addr, + output wire [31:0] o_prof_ticks + // }}} + ); + + // Local declarations + // {{{ + // Local parameter declarations + // {{{ + localparam DW=BUS_WIDTH; + // Peripheral addresses + // {{{ + // Verilator lint_off UNUSED + // These values may (or may not) be used, depending on whether or not + // the respective peripheral is included in the CPU. + localparam [31:0] PERIPHBASE = 32'hc0000000; + localparam [7:0] INTCTRL = 8'h0, + WATCHDOG = 8'h1, // Interrupt generates reset signal + BUSWATCHDOG = 8'h2, // Sets IVEC[0] + CTRINT = 8'h3, // Sets IVEC[5] + TIMER_A = 8'h4, // Sets IVEC[4] + TIMER_B = 8'h5, // Sets IVEC[3] + TIMER_C = 8'h6, // Sets IVEC[2] + JIFFIES = 8'h7, // Sets IVEC[1] + // Accounting counter addresses + MSTR_TASK_CTR = 8'h08, + MSTR_MSTL_CTR = 8'h09, + MSTR_PSTL_CTR = 8'h0a, + MSTR_INST_CTR = 8'h0b, + USER_TASK_CTR = 8'h0c, + USER_MSTL_CTR = 8'h0d, + USER_PSTL_CTR = 8'h0e, + USER_INST_CTR = 8'h0f, + // The MMU + MMU_ADDR = 8'h80, + // DMA controller (DMAC) + // Although I have a hole at 5'h2, the DMA controller + // requires four wishbone addresses, therefore we place + // it by itself and expand our address bus width here + // by another bit. + DMAC_ADDR = 8'h10; + // Verilator lint_on UNUSED + // }}} + + // Debug bit allocations + // {{{ + // DBGCTRL + // 5 DBG Catch -- Catch exceptions/fautls w/ debugger + // 4 Clear cache + // 3 RESET_FLAG + // 2 STEP (W=1 steps, and returns to halted) + // 1 HALT(ED) + // 0 HALT + // DBGDATA + // read/writes internal registers + // + localparam HALT_BIT = 0, + STEP_BIT = 2, + RESET_BIT = 3, + CLEAR_CACHE_BIT = 4, + CATCH_BIT = 5; + // }}} + + // Virtual address width (unused) + // {{{ + localparam `ifdef OPT_MMU - VIRTUAL_ADDRESS_WIDTH=30, + VIRTUAL_ADDRESS_WIDTH=30; `else - VIRTUAL_ADDRESS_WIDTH=PAW, -`endif - LGTLBSZ = 6, - VAW=VIRTUAL_ADDRESS_WIDTH; - - localparam AW=ADDRESS_WIDTH; - input wire i_clk, i_reset; - // Wishbone master - output wire o_wb_cyc, o_wb_stb, o_wb_we; - output wire [(PAW-1):0] o_wb_addr; - output wire [31:0] o_wb_data; - output wire [3:0] o_wb_sel; - input wire i_wb_stall, i_wb_ack; - input wire [31:0] i_wb_data; - input wire i_wb_err; - // Incoming interrupts - input wire [(EXTERNAL_INTERRUPTS-1):0] i_ext_int; - // Outgoing interrupt - output wire o_ext_int; - // Wishbone slave - input wire i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr; - input wire [31:0] i_dbg_data; - input wire [3:0] i_dbg_sel; - output wire o_dbg_ack; - output wire o_dbg_stall; - output wire [31:0] o_dbg_data; - // -`ifdef DEBUG_SCOPE - output wire [31:0] o_cpu_debug; + VIRTUAL_ADDRESS_WIDTH=PAW; `endif + // LGTLBSZ = 6, // Log TLB size + // VAW=VIRTUAL_ADDRESS_WIDTH, + // }}} - wire [31:0] ext_idata; + localparam [1:0] DBG_ADDR_CTRL= 2'b00, + DBG_ADDR_CPU = 2'b01, + DBG_ADDR_SYS = 2'b10; + // }}} - // Handle our interrupt vector generation/coordination wire [14:0] main_int_vector, alt_int_vector; wire ctri_int, tma_int, tmb_int, tmc_int, jif_int, dmac_int; wire mtc_int, moc_int, mpc_int, mic_int, utc_int, uoc_int, upc_int, uic_int; + wire [DBG_WIDTH-1:0] actr_data; + wire actr_ack, actr_stall; + + wire cpu_clken; + // + // + wire sys_cyc, sys_stb, sys_we; + wire [7:0] sys_addr; + wire [PAW-1:0] cpu_addr; + wire [DBG_WIDTH-1:0] sys_data; + reg [DBG_WIDTH-1:0] sys_idata; + reg sys_ack; + wire sys_stall; + + wire sel_counter, sel_timer, sel_pic, sel_apic, + sel_watchdog, sel_bus_watchdog, sel_dmac, sel_mmus; + + wire dbg_cyc, dbg_stb, dbg_we, dbg_stall; + wire [6:0] dbg_addr; + wire [DBG_WIDTH-1:0] dbg_idata; + reg [DBG_WIDTH-1:0] dbg_odata; + reg dbg_ack; + wire [DBG_WIDTH/8-1:0] dbg_sel; + wire no_dbg_err; + + wire cpu_break, dbg_cmd_write, dbg_cpu_write; + wire [DBG_WIDTH-1:0] dbg_cmd_data; + wire [DBG_WIDTH/8-1:0] dbg_cmd_strb; + wire reset_hold, halt_on_fault, dbg_catch; + wire reset_request, release_request, halt_request, + step_request, clear_cache_request; + reg cmd_reset, cmd_halt, cmd_step, cmd_clear_cache, + cmd_write; + reg [4:0] cmd_waddr; + reg [DBG_WIDTH-1:0] cmd_wdata; + wire [2:0] cpu_dbg_cc; + + wire cpu_reset, cpu_halt; + wire cpu_dbg_stall; + wire [DBG_WIDTH-1:0] pic_data; + wire [DBG_WIDTH-1:0] cpu_status; + wire cpu_gie; + + wire wdt_stall, wdt_ack, wdt_reset; + wire [DBG_WIDTH-1:0] wdt_data; + reg wdbus_ack; + reg [PAW-1:0] r_wdbus_data; + wire [DBG_WIDTH-1:0] wdbus_data; + wire reset_wdbus_timer, wdbus_int; + wire cpu_op_stall, cpu_pf_stall, cpu_i_count; + + wire dmac_stb, dc_err; + wire [DBG_WIDTH-1:0] dmac_data; + wire dmac_stall, dmac_ack; + wire dc_cyc, dc_stb, dc_we, dc_stall, dc_ack; + wire [PAW-1:0] dc_addr; + wire [BUS_WIDTH-1:0] dc_data; + wire [BUS_WIDTH/8-1:0] dc_sel; + wire cpu_gbl_cyc; + wire [31:0] dmac_int_vec; + + wire ctri_sel, ctri_stall, ctri_ack; + wire [DBG_WIDTH-1:0] ctri_data; + + wire tma_stall, tma_ack; + wire tmb_stall, tmb_ack; + wire tmc_stall, tmc_ack; + wire jif_stall, jif_ack; + wire [DBG_WIDTH-1:0] tma_data; + wire [DBG_WIDTH-1:0] tmb_data; + wire [DBG_WIDTH-1:0] tmc_data; + wire [DBG_WIDTH-1:0] jif_data; + + wire pic_interrupt, pic_stall, pic_ack; + + wire cpu_gbl_stb, cpu_lcl_cyc, cpu_lcl_stb, + cpu_we; + wire [BUS_WIDTH-1:0] cpu_data, cpu_idata; + wire [BUS_WIDTH/8-1:0] cpu_sel, mmu_sel; + wire cpu_stall, cpu_ack, cpu_err; + wire [DBG_WIDTH-1:0] cpu_dbg_data; + + wire ext_stall, ext_ack; + wire mmu_cyc, mmu_stb, mmu_we, mmu_stall, mmu_ack, + mmu_err, mmus_stall, mmus_ack; + wire [PAW-1:0] mmu_addr; + wire [BUS_WIDTH-1:0] mmu_data, mmu_idata; + wire [DBG_WIDTH-1:0] mmus_data; + wire cpu_miss; + + wire mmu_cpu_stall, mmu_cpu_ack; + wire [BUS_WIDTH-1:0] mmu_cpu_idata; + + // The wires associated with cache snooping + wire pf_return_stb, pf_return_we, pf_return_cachable; + wire [19:0] pf_return_v, pf_return_p; + + wire ext_cyc, ext_stb, ext_we, ext_err; + wire [PAW-1:0] ext_addr; + wire [BUS_WIDTH-1:0] ext_odata; + wire [BUS_WIDTH/8-1:0] ext_sel; + wire [BUS_WIDTH-1:0] ext_idata; + + reg [DBG_WIDTH-1:0] tmr_data; + reg [2:0] w_ack_idx, ack_idx; + reg last_sys_stb; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Handle our interrupt vector generation/coordination + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // Main interrupt vector + // {{{ assign main_int_vector[5:0] = { ctri_int, tma_int, tmb_int, tmc_int, jif_int, dmac_int }; - - generate - if (EXTERNAL_INTERRUPTS < 9) + generate if (EXTERNAL_INTERRUPTS < 9) assign main_int_vector[14:6] = { {(9-EXTERNAL_INTERRUPTS){1'b0}}, i_ext_int }; else assign main_int_vector[14:6] = i_ext_int[8:0]; endgenerate - generate - if (EXTERNAL_INTERRUPTS <= 9) -`ifdef INCLUDE_ACCOUNTING_COUNTERS + // }}} + + // The alternate interrupt vector + // {{{ + generate if (EXTERNAL_INTERRUPTS <= 9 && OPT_ACCOUNTING) + begin assign alt_int_vector = { 7'h00, mtc_int, moc_int, mpc_int, mic_int, utc_int, uoc_int, upc_int, uic_int }; -`else + end else if (EXTERNAL_INTERRUPTS <= 9) // && !OPT_ACCOUNTING + begin assign alt_int_vector = { 15'h00 }; -`endif - else -`ifdef INCLUDE_ACCOUNTING_COUNTERS - if (EXTERNAL_INTERRUPTS >= 15) + end else if (OPT_ACCOUNTING && EXTERNAL_INTERRUPTS >= 15) + begin assign alt_int_vector = { i_ext_int[14:8], mtc_int, moc_int, mpc_int, mic_int, utc_int, uoc_int, upc_int, uic_int }; - else + end else if (OPT_ACCOUNTING) + begin + assign alt_int_vector = { {(7-(EXTERNAL_INTERRUPTS-9)){1'b0}}, i_ext_int[(EXTERNAL_INTERRUPTS-1):9], mtc_int, moc_int, mpc_int, mic_int, utc_int, uoc_int, upc_int, uic_int }; -`else - if (EXTERNAL_INTERRUPTS >= 24) + end else if (!OPT_ACCOUNTING && EXTERNAL_INTERRUPTS >= 24) + begin + assign alt_int_vector = { i_ext_int[(EXTERNAL_INTERRUPTS-1):9] }; - else + end else begin assign alt_int_vector = { {(15-(EXTERNAL_INTERRUPTS-9)){1'b0}}, i_ext_int[(EXTERNAL_INTERRUPTS-1):9] }; -`endif - endgenerate + end endgenerate + // }}} + + // Make Verilator happy + // {{{ + generate if (!OPT_ACCOUNTING) + begin : UNUSED_ACCOUNTING + // Verilator lint_off UNUSED + wire unused_ctrs; + assign unused_ctrs = &{ 1'b0, + moc_int, mpc_int, mic_int, mtc_int, + uoc_int, upc_int, uic_int, utc_int, + cpu_gie, cpu_op_stall, cpu_pf_stall, cpu_i_count }; + // Verilator lint_on UNUSED + end endgenerate + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // // Delay the debug port by one clock, to meet timing requirements - wire dbg_cyc, dbg_stb, dbg_we, dbg_addr, dbg_stall; - wire [31:0] dbg_idata, dbg_odata; - reg dbg_ack; - wire [3:0] dbg_sel; - wire no_dbg_err; -`ifdef DELAY_DBG_BUS - // Make verilator happy - // verilator lint_off UNUSED - // verilator lint_on UNUSED - wire dbg_err; - assign dbg_err = 1'b0; - busdelay #(1,32) wbdelay(i_clk, i_reset, - i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr, i_dbg_data, 4'hf, - o_dbg_stall, o_dbg_ack, o_dbg_data, no_dbg_err, - dbg_cyc, dbg_stb, dbg_we, dbg_addr, dbg_idata, dbg_sel, - dbg_stall, dbg_ack, dbg_odata, dbg_err); -`else - assign dbg_cyc = i_dbg_cyc; - assign dbg_stb = i_dbg_stb; - assign dbg_we = i_dbg_we; - assign dbg_addr = i_dbg_addr; - assign dbg_idata = i_dbg_data; - assign o_dbg_ack = dbg_ack; - assign o_dbg_stall = dbg_stall; - assign o_dbg_data = dbg_odata; - assign dbg_sel = 4'b1111; - assign no_dbg_err = 1'b0; -`endif - + // {{{ + //////////////////////////////////////////////////////////////////////// + // + generate if (DELAY_DBG_BUS) + begin : DELAY_THE_DEBUG_BUS + // {{{ + wire dbg_err; + assign dbg_err = 1'b0; + busdelay #( + // {{{ + .AW(7),.DW(32) + // }}} + ) wbdelay( + // {{{ + i_clk, i_reset, + i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr, i_dbg_data, + 4'hf, + o_dbg_stall, o_dbg_ack, o_dbg_data, no_dbg_err, + dbg_cyc, dbg_stb, dbg_we, dbg_addr, dbg_idata, dbg_sel, + dbg_stall, dbg_ack, dbg_odata, dbg_err + // }}} + ); + // }}} + end else begin : NO_DEBUG_BUS_DELAY + // {{{ + assign dbg_cyc = i_dbg_cyc; + assign dbg_stb = i_dbg_stb; + assign dbg_we = i_dbg_we; + assign dbg_addr = i_dbg_addr; + assign dbg_idata = i_dbg_data; + assign o_dbg_ack = dbg_ack; + assign o_dbg_stall = dbg_stall; + assign o_dbg_data = dbg_odata; + assign dbg_sel = 4'b1111; + assign no_dbg_err = 1'b0; + // }}} + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// // + // Bus decoding, sel_* + // {{{ + //////////////////////////////////////////////////////////////////////// // // - wire sys_cyc, sys_stb, sys_we; - wire [7:0] sys_addr; - wire [(PAW-1):0] cpu_addr; - wire [31:0] sys_data; - reg [31:0] sys_idata; - reg sys_ack; - wire sys_stall; - wire sel_counter, sel_timer, sel_pic, sel_apic, - sel_watchdog, sel_bus_watchdog, sel_dmac, sel_mmus; - // - assign sel_pic = (sys_stb)&&(sys_addr == `INTCTRL); - assign sel_watchdog = (sys_stb)&&(sys_addr == `WATCHDOG); - assign sel_bus_watchdog= (sys_stb)&&(sys_addr == `BUSWATCHDOG); - assign sel_apic = (sys_stb)&&(sys_addr == `CTRINT); - assign sel_timer = (sys_stb)&&(sys_addr[7:2] == 6'h1); - assign sel_counter = (sys_stb)&&(sys_addr[7:3] == 5'h1); - assign sel_dmac = (sys_stb)&&(sys_addr[7:4] == 4'h1); + assign sel_pic = (sys_stb)&&(sys_addr == INTCTRL); + assign sel_watchdog = (sys_stb)&&(sys_addr == WATCHDOG); + assign sel_bus_watchdog= (sys_stb)&&(sys_addr == BUSWATCHDOG); + assign sel_apic = (sys_stb)&&(sys_addr == CTRINT); + assign sel_timer = (sys_stb)&&(sys_addr[7:2]==TIMER_A[7:2]); + assign sel_counter = (sys_stb)&&(sys_addr[7:3]==MSTR_TASK_CTR[7:3]); + assign sel_dmac = (sys_stb)&&(sys_addr[7:4] ==DMAC_ADDR[7:4]); assign sel_mmus = (sys_stb)&&(sys_addr[7]); + // }}} + //////////////////////////////////////////////////////////////////////// // // The external debug interface + // {{{ + //////////////////////////////////////////////////////////////////////// // - // We offer only a limited interface here, requiring a pre-register - // write to set the local address. This interface allows access to - // the Zip System on a debug basis only, and not to the rest of the - // wishbone bus. Further, to access these registers, the control - // register must first be accessed to both stop the CPU and to - // set the following address in question. Hence all accesses require - // two accesses: write the address to the control register (and halt - // the CPU if not halted), then read/write the data from the data - // register. - // - wire cpu_break, dbg_cmd_write; - reg cmd_reset, cmd_halt, cmd_step, cmd_clear_pf_cache, - reset_hold, cpu_halt; - reg [5:0] cmd_addr; - wire [3:0] cpu_dbg_cc; - assign dbg_cmd_write = (dbg_stb)&&(dbg_we)&&(!dbg_addr); // - // Always start us off with an initial reset + + assign dbg_cpu_write = (dbg_stb && dbg_we) + &&(dbg_addr[6:5] == DBG_ADDR_CPU) + || (i_sim_write && i_sim_addr[6:5] == DBG_ADDR_CPU); + assign dbg_cmd_write = (dbg_stb)&&(dbg_we) + &&(dbg_addr[6:5] == DBG_ADDR_CTRL) + || (i_sim_write && i_sim_addr[6:5] == DBG_ADDR_CTRL); + assign dbg_cmd_data = (i_sim_write) ? i_sim_data : dbg_idata; + assign dbg_cmd_strb = (i_sim_write) ? 4'hf : dbg_sel; + + assign reset_request = dbg_cmd_write && dbg_cmd_strb[RESET_BIT/8] + && dbg_cmd_data[RESET_BIT]; + assign release_request = dbg_cmd_write && dbg_cmd_strb[HALT_BIT/8] + && !dbg_cmd_data[HALT_BIT]; + assign halt_request = dbg_cmd_write && dbg_cmd_strb[HALT_BIT/8] + && dbg_cmd_data[HALT_BIT]; + assign step_request = dbg_cmd_write && dbg_cmd_strb[STEP_BIT/8] + && dbg_cmd_data[STEP_BIT]; + assign clear_cache_request = dbg_cmd_write + && dbg_cmd_strb[CLEAR_CACHE_BIT/8] + && dbg_cmd_data[CLEAR_CACHE_BIT]; + // + // reset_hold: Always start us off with an initial reset + // {{{ generate if (RESET_DURATION > 0) begin : INITIAL_RESET_HOLD - + // {{{ reg [$clog2(RESET_DURATION)-1:0] reset_counter; + reg r_reset_hold; initial reset_counter = RESET_DURATION; always @(posedge i_clk) if (i_reset) reset_counter <= RESET_DURATION; + else if (i_sim_write) + reset_counter <= 0; else if (reset_counter > 0) reset_counter <= reset_counter - 1; - initial reset_hold = 1; + initial r_reset_hold = 1; always @(posedge i_clk) if (i_reset) - reset_hold <= 1; + r_reset_hold <= 1; else - reset_hold <= (reset_counter != 0); - - initial cmd_halt = START_HALTED; - always @(posedge i_clk) - if (i_reset) - cmd_halt <= START_HALTED; - else if (dbg_cmd_write) - cmd_halt <= ((dbg_idata[`HALT_BIT])&&(!dbg_idata[`STEP_BIT])); - - end else begin + r_reset_hold <= (reset_counter > 1) && !i_sim_write; + assign reset_hold = r_reset_hold; +`ifdef FORMAL always @(*) - reset_hold = 0; + assert(reset_hold == (reset_counter != 0)); +`endif + // }}} + end else begin - always @(*) - cmd_halt = START_HALTED; + assign reset_hold = 0; end endgenerate + // }}} + assign halt_on_fault = dbg_catch; + + // cmd_reset + // {{{ + // Always start us off with an initial reset initial cmd_reset = 1'b1; - always @(posedge i_clk) - cmd_reset <= ((dbg_cmd_write)&&(dbg_idata[`RESET_BIT])) - ||(wdt_reset) || reset_hold; - // - initial cpu_halt = START_HALTED; always @(posedge i_clk) if (i_reset) - cpu_halt <= cmd_halt; - else if (cmd_reset) - cpu_halt <= cmd_halt; - else if (dbg_cmd_write) - cpu_halt <= ((dbg_idata[`HALT_BIT])&&(!dbg_idata[`STEP_BIT])); - else if ((cmd_step)||(cpu_break)) - cpu_halt <= 1'b1; - - initial cmd_clear_pf_cache = 1'b1; + cmd_reset <= 1'b1; + else if (reset_hold || wdt_reset) + cmd_reset <= 1'b1; + else if (cpu_break && !halt_on_fault) + cmd_reset <= 1'b1; + else + cmd_reset <= reset_request; + // }}} + + // cmd_halt + // {{{ + initial cmd_halt = START_HALTED; always @(posedge i_clk) - cmd_clear_pf_cache <= (dbg_cmd_write)&&(dbg_idata[`CLEAR_CACHE_BIT]); - // - initial cmd_step = 1'b0; + if (i_reset) + cmd_halt <= START_HALTED; + else if (cmd_reset && START_HALTED) + cmd_halt <= START_HALTED; + else begin + // {{{ + // When shall we release from a halt? Only if we have come to + // a full and complete stop. Even then, we only release if we + // aren't being given a command to step the CPU. + // + if (!cmd_write && !cpu_dbg_stall && dbg_cmd_write + && (release_request || step_request)) + cmd_halt <= 1'b0; + + // Reasons to halt + + // 1. Halt on any unhandled CPU exception. The cause of the + // exception must be cured before we can (re)start. + // If the CPU is configured to start immediately on power + // up, we leave it to reset on any exception instead. + if (cpu_break && halt_on_fault) + cmd_halt <= 1'b1; + + // 2. Halt on any user request to halt. (Only valid if the + // STEP bit isn't also set) + if (dbg_cmd_write && halt_request && !step_request) + cmd_halt <= 1'b1; + + // 3. Halt on any user request to write to a CPU register + if (dbg_cpu_write && dbg_cmd_strb != 0) + cmd_halt <= 1'b1; + + // 4. Halt following any step command + if (cmd_step) + cmd_halt <= 1'b1; + + // 4. Halt following any clear cache + if (cmd_clear_cache) + cmd_halt <= 1'b1; + + // 5. Halt on any clear cache bit--independent of any step bit + if (clear_cache_request) + cmd_halt <= 1'b1; + // }}} + end + // }}} + + // cmd_clear_cache + // {{{ + initial cmd_clear_cache = 1'b0; always @(posedge i_clk) - cmd_step <= (dbg_cmd_write)&&(dbg_idata[`STEP_BIT]); - // - initial cmd_addr = 6'h0; + if (i_reset || cpu_reset) + cmd_clear_cache <= 1'b0; + else if (dbg_cmd_write && clear_cache_request && halt_request) + cmd_clear_cache <= 1'b1; + else if (cmd_halt && !cpu_dbg_stall) + cmd_clear_cache <= 1'b0; + // }}} + + // cmd_step + // {{{ + initial cmd_step = 1'b0; always @(posedge i_clk) - if (dbg_cmd_write) - cmd_addr <= dbg_idata[5:0]; + if (i_reset) + cmd_step <= 1'b0; + else if (step_request) + cmd_step <= 1'b1; + else if (!cpu_dbg_stall) + cmd_step <= 1'b0; + // }}} + + // dbg_catch + // {{{ + generate if (!OPT_DBGPORT) + begin : NO_DBG_CATCH + assign dbg_catch = START_HALTED; + end else begin : GEN_DBG_CATCH + reg r_dbg_catch; + + initial r_dbg_catch = START_HALTED; + always @(posedge i_clk) + if (i_reset) + r_dbg_catch <= START_HALTED; + else if (dbg_cmd_write && dbg_cmd_strb[CATCH_BIT/8]) + r_dbg_catch <= dbg_cmd_data[CATCH_BIT]; + + assign dbg_catch = r_dbg_catch; + end endgenerate + // }}} - wire cpu_reset; assign cpu_reset = (cmd_reset); + assign cpu_halt = (cmd_halt); - wire cpu_dbg_stall; - wire [31:0] pic_data; - wire [31:0] cmd_data; + // cpu_status + // {{{ // Values: - // 0x0003f -> cmd_addr mask - // 0x00040 -> reset - // 0x00080 -> PIC interrrupt pending - // 0x00100 -> cmd_step - // 0x00200 -> cmd_stall - // 0x00400 -> cmd_halt - // 0x00800 -> cmd_clear_pf_cache - // 0x01000 -> cc.sleep - // 0x02000 -> cc.gie - // 0x04000 -> External (PIC) interrupt line is high - // Other external interrupts follow + // 0xxxxx_0000 -> External interrupt lines + // + // 0xffff_f000 -> (Unused / reserved) + // + // 0x0000_0800 -> cpu_break + // 0x0000_0400 -> Interrupt pending + // 0x0000_0200 -> User mode + // 0x0000_0100 -> Sleep (CPU is sleeping) + // + // 0x0000_00c0 -> (Unused/reserved) + // 0x0000_0020 -> dbg_catch + // 0x0000_0010 -> cmd_clear_cache + // + // 0x0000_0008 -> Reset + // 0x0000_0004 -> Step (auto clearing, write only) + // 0x0000_0002 -> Halt (status) + // 0x0000_0001 -> Halt (request) generate - if (EXTERNAL_INTERRUPTS < 16) - assign cmd_data = { {(16-EXTERNAL_INTERRUPTS){1'b0}}, - i_ext_int, - cpu_dbg_cc, // 4 bits - 1'b0, cmd_halt, (!cpu_dbg_stall), 1'b0, - pic_data[15], cpu_reset, cmd_addr }; - else - assign cmd_data = { i_ext_int[15:0], cpu_dbg_cc, - 1'b0, cmd_halt, (!cpu_dbg_stall), 1'b0, - pic_data[15], cpu_reset, cmd_addr }; - endgenerate + if (EXTERNAL_INTERRUPTS < 20) + begin : CPU_STATUS_NO_EXTRA_INTERRUPTS + assign cpu_status = { {(20-EXTERNAL_INTERRUPTS){1'b0}}, + i_ext_int, + cpu_break, pic_interrupt, cpu_dbg_cc[1:0], + 2'h0, dbg_catch, 1'b0, + cmd_reset, 1'b0, !cpu_dbg_stall, cmd_halt + }; + end else begin : CPU_STATUS_MAX_INTERRUPTS + assign cpu_status = { i_ext_int[19:0], + cpu_break, pic_interrupt, cpu_dbg_cc[1:0], + 2'h0, dbg_catch, 1'b0, + cmd_reset, 1'b0, !cpu_dbg_stall, cmd_halt + }; + end endgenerate + // }}} - wire cpu_gie; assign cpu_gie = cpu_dbg_cc[1]; + // cmd_write + // {{{ + initial cmd_write = 0; + always @(posedge i_clk) + if (i_reset || cpu_reset) + cmd_write <= 1'b0; + else if (!cmd_write || !cpu_dbg_stall) + cmd_write <= (dbg_cpu_write && dbg_cmd_strb != 0); + // }}} + + // cmd_waddr, cmd_wdata + // {{{ + always @(posedge i_clk) + if ((!cmd_write || (!cpu_dbg_stall)) && dbg_cpu_write) + begin + cmd_waddr <= (i_sim_write) ? i_sim_addr[4:0] : dbg_addr[4:0]; + cmd_wdata <= (i_sim_write) ? i_sim_data : dbg_idata; + end + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// // // The WATCHDOG Timer + // {{{ + //////////////////////////////////////////////////////////////////////// + // // - wire wdt_stall, wdt_ack, wdt_reset; - wire [31:0] wdt_data; ziptimer #(32,31,0) - watchdog(i_clk, cpu_reset, !cmd_halt, - sys_cyc, (sys_stb)&&(sel_watchdog), sys_we, - sys_data, - wdt_stall, wdt_ack, wdt_data, wdt_reset); + watchdog( + // {{{ + .i_clk(i_clk), .i_reset(cpu_reset), + .i_ce(!cmd_halt), + .i_wb_cyc(sys_cyc), + .i_wb_stb((sys_stb)&&(sel_watchdog)), + .i_wb_we(sys_we), .i_wb_data(sys_data), .i_wb_sel(4'hf), + .o_wb_stall(wdt_stall), + .o_wb_ack(wdt_ack), + .o_wb_data(wdt_data), + .o_int(wdt_reset) + // }}} + ); // // Position two, a second watchdog timer--this time for the wishbone @@ -487,358 +824,572 @@ module zipsystem(i_clk, i_reset, // configuration, it cannot be configured and all bus accesses must // take less than the number written to this register. // - reg wdbus_ack; - reg [(PAW-1):0] r_wdbus_data; - wire [31:0] wdbus_data; - wire reset_wdbus_timer, wdbus_int; assign reset_wdbus_timer = (!o_wb_cyc)||(o_wb_stb)||(i_wb_ack); - wbwatchdog #(14) watchbus(i_clk,(cpu_reset)||(reset_wdbus_timer), - 14'h2000, wdbus_int); + + wbwatchdog #(14) + watchbus( + // {{{ + i_clk,(cpu_reset)||(reset_wdbus_timer), + 14'h2000, wdbus_int + // }}} + ); + initial r_wdbus_data = 0; always @(posedge i_clk) - if ((wdbus_int)||(cpu_err)) - r_wdbus_data <= o_wb_addr; + if ((wdbus_int)||(cpu_err)) + r_wdbus_data <= o_wb_addr; + assign wdbus_data = { {(32-PAW){1'b0}}, r_wdbus_data }; initial wdbus_ack = 1'b0; always @(posedge i_clk) - wdbus_ack <= ((sys_cyc)&&(sys_stb)&&(sel_bus_watchdog)); - - // Counters -- for performance measurement and accounting - // - // Here's the stuff we'll be counting .... + if (i_reset || !sys_cyc) + wdbus_ack <= 1'b0; + else + wdbus_ack <= (sys_stb)&&(sel_bus_watchdog); + // }}} + //////////////////////////////////////////////////////////////////////// // - wire cpu_op_stall, cpu_pf_stall, cpu_i_count; - -`ifdef INCLUDE_ACCOUNTING_COUNTERS + // Performance counters + // {{{ + //////////////////////////////////////////////////////////////////////// // - // The master counters will, in general, not be reset. They'll be used - // for an overall counter. // - // Master task counter - wire mtc_stall, mtc_ack; - wire [31:0] mtc_data; - zipcounter mtask_ctr(i_clk, 1'b0, (!cpu_halt), sys_cyc, - (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b000), - sys_we, sys_data, - mtc_stall, mtc_ack, mtc_data, mtc_int); - // Master Operand Stall counter - wire moc_stall, moc_ack; - wire [31:0] moc_data; - zipcounter mmstall_ctr(i_clk,1'b0, (cpu_op_stall), sys_cyc, - (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b001), - sys_we, sys_data, - moc_stall, moc_ack, moc_data, moc_int); + // Here's the stuff we'll be counting .... + // + generate if (OPT_ACCOUNTING) + begin : ACCOUNTING_COUNTERS + // {{{ + // Local definitions + // {{{ + // Verilator lint_off UNUSED + wire mtc_stall, mtc_ack; + wire moc_stall, moc_ack; + wire mpc_stall, mpc_ack; + wire mic_stall, mic_ack; + wire utc_stall, utc_ack; + wire uoc_stall, uoc_ack; + wire upc_stall, upc_ack; + wire uic_stall, uic_ack; + // Verilator lint_on UNUSED + wire [DBG_WIDTH-1:0] mtc_data; + wire [DBG_WIDTH-1:0] moc_data; + wire [DBG_WIDTH-1:0] mpc_data; + wire [DBG_WIDTH-1:0] mic_data; + wire [DBG_WIDTH-1:0] utc_data; + wire [DBG_WIDTH-1:0] uoc_data; + wire [DBG_WIDTH-1:0] upc_data; + wire [DBG_WIDTH-1:0] uic_data; + reg [DBG_WIDTH-1:0] r_actr_data; + // }}} + + // Master counters + // {{{ + // The master counters will, in general, not be reset. They'll + // be used for an overall counter. + // + // Master task counter + zipcounter + mtask_ctr( + // {{{ + i_clk, 1'b0, (!cmd_halt), sys_cyc, + (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b000), + sys_we, sys_data, + mtc_stall, mtc_ack, mtc_data, mtc_int + // }}} + ); - // Master PreFetch-Stall counter - wire mpc_stall, mpc_ack; - wire [31:0] mpc_data; - zipcounter mpstall_ctr(i_clk,1'b0, (cpu_pf_stall), sys_cyc, - (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b010), - sys_we, sys_data, - mpc_stall, mpc_ack, mpc_data, mpc_int); + // Master Operand Stall counter + zipcounter + mmstall_ctr( + // {{{ + i_clk,1'b0, (cpu_op_stall), sys_cyc, + (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b001), + sys_we, sys_data, + moc_stall, moc_ack, moc_data, moc_int + // }}} + ); - // Master Instruction counter - wire mic_stall, mic_ack; - wire [31:0] mic_data; - zipcounter mins_ctr(i_clk,1'b0, (cpu_i_count), sys_cyc, - (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b011), + // Master PreFetch-Stall counter + zipcounter + mpstall_ctr( + // {{{ + i_clk,1'b0, (cpu_pf_stall), sys_cyc, + (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b010), sys_we, sys_data, - mic_stall, mic_ack, mic_data, mic_int); + mpc_stall, mpc_ack, mpc_data, mpc_int + // }}} + ); - // - // The user counters are different from those of the master. They will - // be reset any time a task is given control of the CPU. - // - // User task counter - wire utc_stall, utc_ack; - wire [31:0] utc_data; - zipcounter utask_ctr(i_clk,1'b0, (!cpu_halt)&&(cpu_gie), sys_cyc, - (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b100), - sys_we, sys_data, - utc_stall, utc_ack, utc_data, utc_int); + // Master Instruction counter + zipcounter + mins_ctr( + // {{{ + i_clk,1'b0, (cpu_i_count), sys_cyc, + (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b011), + sys_we, sys_data, + mic_stall, mic_ack, mic_data, mic_int + // }}} + ); + // }}} + // User counters + // {{{ + // The user counters are different from those of the master. + // They will be reset any time a task is given control of the + // CPU. + // + // User task counter + zipcounter + utask_ctr( + // {{{ + i_clk,1'b0, (!cmd_halt)&&(cpu_gie), sys_cyc, + (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b100), + sys_we, sys_data, + utc_stall, utc_ack, utc_data, utc_int + // }}} + ); - // User Op-Stall counter - wire uoc_stall, uoc_ack; - wire [31:0] uoc_data; - zipcounter umstall_ctr(i_clk,1'b0, (cpu_op_stall)&&(cpu_gie), sys_cyc, + // User Op-Stall counter + zipcounter + umstall_ctr( + // {{{ + i_clk,1'b0, (cpu_op_stall)&&(cpu_gie), sys_cyc, (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b101), sys_we, sys_data, - uoc_stall, uoc_ack, uoc_data, uoc_int); + uoc_stall, uoc_ack, uoc_data, uoc_int + // }}} + ); - // User PreFetch-Stall counter - wire upc_stall, upc_ack; - wire [31:0] upc_data; - zipcounter upstall_ctr(i_clk,1'b0, (cpu_pf_stall)&&(cpu_gie), sys_cyc, + // User PreFetch-Stall counter + zipcounter + upstall_ctr( + // {{{ + i_clk,1'b0, (cpu_pf_stall)&&(cpu_gie), sys_cyc, (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b110), sys_we, sys_data, - upc_stall, upc_ack, upc_data, upc_int); + upc_stall, upc_ack, upc_data, upc_int + // }}} + ); - // User instruction counter - wire uic_stall, uic_ack; - wire [31:0] uic_data; - zipcounter uins_ctr(i_clk,1'b0, (cpu_i_count)&&(cpu_gie), sys_cyc, + // User instruction counter + zipcounter + uins_ctr( + // {{{ + i_clk,1'b0, (cpu_i_count)&&(cpu_gie), sys_cyc, (sys_stb)&&(sel_counter)&&(sys_addr[2:0] == 3'b111), sys_we, sys_data, - uic_stall, uic_ack, uic_data, uic_int); - - // A little bit of pre-cleanup (actr = accounting counters) - wire actr_stall, actr_ack; - reg [31:0] actr_data; - assign actr_ack = sel_counter; - assign actr_stall = 1'b0; - always @(*) - begin - case(sys_addr[2:0]) - 3'h0: actr_data = mtc_data; - 3'h1: actr_data = moc_data; - 3'h2: actr_data = mpc_data; - 3'h3: actr_data = mic_data; - 3'h4: actr_data = utc_data; - 3'h5: actr_data = uoc_data; - 3'h6: actr_data = upc_data; - 3'h7: actr_data = uic_data; - endcase - end -`else // INCLUDE_ACCOUNTING_COUNTERS - reg actr_ack; - wire actr_stall; - wire [31:0] actr_data; - assign actr_stall = 1'b0; - assign actr_data = 32'h0000; - - assign mtc_int = 1'b0; - assign moc_int = 1'b0; - assign mpc_int = 1'b0; - assign mic_int = 1'b0; - assign utc_int = 1'b0; - assign uoc_int = 1'b0; - assign upc_int = 1'b0; - assign uic_int = 1'b0; + uic_stall, uic_ack, uic_data, uic_int + // }}} + ); + // }}} - always @(posedge i_clk) - actr_ack <= sel_counter; -`endif // INCLUDE_ACCOUNTING_COUNTERS + // A little bit of pre-cleanup (actr = accounting counters) + assign actr_ack = sel_counter; + assign actr_stall = 1'b0; + // actr_data + // {{{ + always @(*) + begin + case(sys_addr[2:0]) + 3'h0: r_actr_data = mtc_data; + 3'h1: r_actr_data = moc_data; + 3'h2: r_actr_data = mpc_data; + 3'h3: r_actr_data = mic_data; + 3'h4: r_actr_data = utc_data; + 3'h5: r_actr_data = uoc_data; + 3'h6: r_actr_data = upc_data; + 3'h7: r_actr_data = uic_data; + endcase + end + + assign actr_data = r_actr_data; + // }}} + // }}} + end else begin : NO_ACCOUNTING_COUNTERS + // {{{ + + assign actr_stall = 1'b0; + assign actr_data = 32'h0000; + + assign mtc_int = 1'b0; + assign moc_int = 1'b0; + assign mpc_int = 1'b0; + assign mic_int = 1'b0; + assign utc_int = 1'b0; + assign uoc_int = 1'b0; + assign upc_int = 1'b0; + assign uic_int = 1'b0; + + assign actr_ack = sel_counter; + // }}} + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// // // The DMA Controller + // {{{ + //////////////////////////////////////////////////////////////////////// + // // - wire dmac_stb, dc_err; - wire [31:0] dmac_data; - wire dmac_stall, dmac_ack; - wire dc_cyc, dc_stb, dc_we, dc_stall, dc_ack; - wire [31:0] dc_data; - wire [(PAW-1):0] dc_addr; - wire cpu_gbl_cyc; - wire [31:0] dmac_int_vec; assign dmac_int_vec = { 1'b0, alt_int_vector, 1'b0, main_int_vector[14:1], 1'b0 }; assign dmac_stb = (sys_stb)&&(sel_dmac); -`ifdef INCLUDE_DMA_CONTROLLER - wbdmac #(PAW) dma_controller(i_clk, cpu_reset, - sys_cyc, dmac_stb, sys_we, - sys_addr[1:0], sys_data, - dmac_stall, dmac_ack, dmac_data, - // Need the outgoing DMAC wishbone bus - dc_cyc, dc_stb, dc_we, dc_addr, dc_data, - dc_stall, dc_ack, ext_idata, dc_err, - // External device interrupts - dmac_int_vec, - // DMAC interrupt, for upon completion - dmac_int); -`else - reg r_dmac_ack; - initial r_dmac_ack = 1'b0; - always @(posedge i_clk) - r_dmac_ack <= (sys_cyc)&&(dmac_stb); - assign dmac_ack = r_dmac_ack; - assign dmac_data = 32'h000; - assign dmac_stall = 1'b0; - - assign dc_cyc = 1'b0; - assign dc_stb = 1'b0; - assign dc_we = 1'b0; - assign dc_addr = { (PAW) {1'b0} }; - assign dc_data = 32'h00; - - assign dmac_int = 1'b0; -`endif - wire ctri_sel, ctri_stall, ctri_ack; - wire [31:0] ctri_data; - assign ctri_sel = (sys_stb)&&(sel_apic); -`ifdef INCLUDE_ACCOUNTING_COUNTERS - // - // Counter Interrupt controller - // - generate if (EXTERNAL_INTERRUPTS <= 9) - begin : ALT_PIC - icontrol #(8) ctri(i_clk, cpu_reset, sys_cyc, (ctri_sel), sys_we, - sys_data, 4'hf, ctri_stall, ctri_ack, ctri_data, - alt_int_vector[7:0], ctri_int); - end else begin : ALT_PIC - icontrol #(8+(EXTERNAL_INTERRUPTS-9)) - ctri(i_clk, cpu_reset, sys_cyc, (ctri_sel), sys_we, - sys_data, 4'hf, ctri_stall, ctri_ack, ctri_data, - alt_int_vector[(EXTERNAL_INTERRUPTS-2):0], - ctri_int); - end endgenerate + generate if (OPT_DMA) + begin : DMA + // {{{ + zipdma #( + // {{{ + .ADDRESS_WIDTH(ADDRESS_WIDTH), .LGMEMLEN(DMA_LGMEM), + .BUS_WIDTH(DW), .OPT_LITTLE_ENDIAN(1'b0) + // }}} + ) dma_controller( + // {{{ + .i_clk(i_clk), .i_reset(cpu_reset), + .i_swb_cyc(sys_cyc), .i_swb_stb(dmac_stb), + .i_swb_we(sys_we), .i_swb_addr(sys_addr[1:0]), + .i_swb_data(sys_data), .i_swb_sel(4'hf), + .o_swb_stall(dmac_stall), .o_swb_ack(dmac_ack), + .o_swb_data(dmac_data), + // Need the outgoing DMAC wishbone bus + .o_mwb_cyc(dc_cyc), .o_mwb_stb(dc_stb), + .o_mwb_we(dc_we), .o_mwb_addr(dc_addr), + .o_mwb_data(dc_data), .o_mwb_sel(dc_sel), + .i_mwb_stall(dc_stall), + .i_mwb_ack(dc_ack), .i_mwb_data(ext_idata), + .i_mwb_err(dc_err), + // External device interrupts + .i_dev_ints(dmac_int_vec), + // DMAC interrupt, for upon completion + .o_interrupt(dmac_int) + // }}} + ); + // }}} + end else begin : NO_DMA + // {{{ + reg r_dmac_ack; -`else // INCLUDE_ACCOUNTING_COUNTERS - - generate if (EXTERNAL_INTERRUPTS <= 9) - begin : ALT_PIC - assign ctri_stall = 1'b0; - assign ctri_data = 32'h0000; - assign ctri_int = 1'b0; - end else begin : ALT_PIC - icontrol #(EXTERNAL_INTERRUPTS-9) - ctri(i_clk, cpu_reset, (ctri_sel), sys_we, - sys_data, ctri_stall, ctri_ack, ctri_data, - alt_int_vector[(EXTERNAL_INTERRUPTS-10):0], - ctri_int); + initial r_dmac_ack = 1'b0; + always @(posedge i_clk) + if (i_reset) + r_dmac_ack <= 1'b0; + else + r_dmac_ack <= (sys_cyc)&&(dmac_stb); + assign dmac_ack = r_dmac_ack; + assign dmac_data = 32'h000; + assign dmac_stall = 1'b0; + + assign dc_cyc = 1'b0; + assign dc_stb = 1'b0; + assign dc_we = 1'b0; + assign dc_addr = { (PAW) {1'b0} }; + assign dc_data = 32'h00; + assign dc_sel = 4'h0; + + assign dmac_int = 1'b0; + + // Make Verilator happy + // {{{ + // Verilator lint_off UNUSED + wire unused_dmac; + assign unused_dmac = &{ 1'b0, dc_err, dc_ack, + dc_stall, dmac_int_vec }; + // Verilator lint_on UNUSED + // }}} + // }}} end endgenerate -`endif // INCLUDE_ACCOUNTING_COUNTERS + // }}} + //////////////////////////////////////////////////////////////////////// + // + // The alternate interrupt controller + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + assign ctri_sel = (sys_stb)&&(sel_apic); + generate if (OPT_ACCOUNTING) + begin : PIC_WITH_ACCOUNTING + // + // Interrupt controller + // + if (EXTERNAL_INTERRUPTS <= 9) + begin : ALT_PIC + icontrol #(8) + ctri( + // {{{ + .i_clk(i_clk), .i_reset(cpu_reset), + .i_wb_cyc(sys_cyc), .i_wb_stb(ctri_sel), + .i_wb_we(sys_we), .i_wb_data(sys_data), .i_wb_sel(4'hf), + .o_wb_stall(ctri_stall), .o_wb_ack(ctri_ack), + .o_wb_data(ctri_data), + .i_brd_ints(alt_int_vector[7:0]), .o_interrupt(ctri_int) + // }}} + ); + end else begin : ALT_PIC + icontrol #(8+(EXTERNAL_INTERRUPTS-9)) + ctri( + // {{{ + .i_clk(i_clk), .i_reset(cpu_reset), + .i_wb_cyc(sys_cyc), .i_wb_stb(ctri_sel), + .i_wb_we(sys_we), .i_wb_data(sys_data), + .i_wb_sel(4'hf), + .o_wb_stall(ctri_stall), + .o_wb_ack(ctri_ack), + .o_wb_data(ctri_data), + .i_brd_ints(alt_int_vector[(EXTERNAL_INTERRUPTS-2):0]), + .o_interrupt(ctri_int) + // }}} + ); + end + end else begin : PIC_WITHOUT_ACCOUNTING + + if (EXTERNAL_INTERRUPTS <= 9) + begin : ALT_PIC + assign ctri_stall = 1'b0; + assign ctri_data = 32'h0000; + assign ctri_int = 1'b0; + end else begin : ALT_PIC + icontrol #(EXTERNAL_INTERRUPTS-9) + ctri( + // {{{ + .i_clk(i_clk), .i_reset(cpu_reset), + .i_wb_cyc(sys_cyc), .i_wb_stb(ctri_sel), + .i_wb_we(sys_we), .i_wb_data(sys_data), + .i_wb_sel(4'hf), + .o_wb_stall(ctri_stall), + .o_wb_ack(ctri_ack), + .o_wb_data(ctri_data), + .i_brd_ints(alt_int_vector[(EXTERNAL_INTERRUPTS-10):0]), + .o_interrupt(ctri_int) + // }}} + ); + end + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Timers + // {{{ + //////////////////////////////////////////////////////////////////////// + // // // Timer A // - wire tma_stall, tma_ack; - wire [31:0] tma_data; - ziptimer timer_a(i_clk, cpu_reset, !cmd_halt, - sys_cyc, (sys_stb)&&(sel_timer)&&(sys_addr[1:0] == 2'b00), - sys_we, sys_data, - tma_stall, tma_ack, tma_data, tma_int); + ziptimer + timer_a( + // {{{ + .i_clk(i_clk), .i_reset(cpu_reset), .i_ce(!cmd_halt), + .i_wb_cyc(sys_cyc), + .i_wb_stb((sys_stb)&&(sel_timer)&&(sys_addr[1:0] == 2'b00)), + .i_wb_we(sys_we), .i_wb_data(sys_data), .i_wb_sel(4'hf), + .o_wb_stall(tma_stall), .o_wb_ack(tma_ack), + .o_wb_data(tma_data), + .o_int(tma_int) + // }}} + ); // // Timer B // - wire tmb_stall, tmb_ack; - wire [31:0] tmb_data; - ziptimer timer_b(i_clk, cpu_reset, !cmd_halt, - sys_cyc, (sys_stb)&&(sel_timer)&&(sys_addr[1:0] == 2'b01), - sys_we, sys_data, - tmb_stall, tmb_ack, tmb_data, tmb_int); + ziptimer timer_b( + // {{{ + .i_clk(i_clk), .i_reset(cpu_reset), .i_ce(!cmd_halt), + .i_wb_cyc(sys_cyc), + .i_wb_stb((sys_stb)&&(sel_timer)&&(sys_addr[1:0] == 2'b01)), + .i_wb_we(sys_we), .i_wb_data(sys_data), .i_wb_sel(4'hf), + .o_wb_stall(tmb_stall), .o_wb_ack(tmb_ack), + .o_wb_data(tmb_data), + .o_int(tmb_int) + // }}} + ); // // Timer C // - wire tmc_stall, tmc_ack; - wire [31:0] tmc_data; - ziptimer timer_c(i_clk, cpu_reset, !cmd_halt, - sys_cyc, (sys_stb)&&(sel_timer)&&(sys_addr[1:0]==2'b10), - sys_we, sys_data, - tmc_stall, tmc_ack, tmc_data, tmc_int); + ziptimer timer_c( + // {{{ + .i_clk(i_clk), .i_reset(cpu_reset), .i_ce(!cmd_halt), + .i_wb_cyc(sys_cyc), + .i_wb_stb((sys_stb)&&(sel_timer)&&(sys_addr[1:0] == 2'b10)), + .i_wb_we(sys_we), .i_wb_data(sys_data), .i_wb_sel(4'hf), + .o_wb_stall(tmc_stall), .o_wb_ack(tmc_ack), + .o_wb_data(tmc_data), + .o_int(tmc_int) + // }}} + ); // // JIFFIES // - wire jif_stall, jif_ack; - wire [31:0] jif_data; - zipjiffies jiffies(i_clk, cpu_reset, !cmd_halt, - sys_cyc, (sys_stb)&&(sel_timer)&&(sys_addr[1:0] == 2'b11), sys_we, - sys_data, - jif_stall, jif_ack, jif_data, jif_int); - + zipjiffies jiffies( + // {{{ + .i_clk(i_clk), .i_reset(cpu_reset), .i_ce(!cmd_halt), + .i_wb_cyc(sys_cyc), + .i_wb_stb((sys_stb)&&(sel_timer)&&(sys_addr[1:0] == 2'b11)), + .i_wb_we(sys_we), .i_wb_data(sys_data), .i_wb_sel(4'hf), + .o_wb_stall(jif_stall), .o_wb_ack(jif_ack), + .o_wb_data(jif_data), + .o_int(jif_int) + // }}} + ); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // The main (programmable) interrupt controller peripheral + // {{{ + //////////////////////////////////////////////////////////////////////// // - // The programmable interrupt controller peripheral // - wire pic_interrupt, pic_stall, pic_ack; generate if (EXTERNAL_INTERRUPTS < 9) begin : MAIN_PIC - icontrol #(6+EXTERNAL_INTERRUPTS) pic(i_clk, cpu_reset, - sys_cyc, (sys_cyc)&&(sys_stb)&&(sel_pic),sys_we, - sys_data, 4'hf, pic_stall, pic_ack, pic_data, - main_int_vector[(6+EXTERNAL_INTERRUPTS-1):0], pic_interrupt); + icontrol #(6+EXTERNAL_INTERRUPTS) + pic( + // {{{ + i_clk, cpu_reset, + sys_cyc, (sys_cyc)&&(sys_stb)&&(sel_pic),sys_we, + sys_data, 4'hf, pic_stall, pic_ack, pic_data, + main_int_vector[(6+EXTERNAL_INTERRUPTS-1):0], + pic_interrupt + // }}} + ); end else begin : MAIN_PIC - icontrol #(15) pic(i_clk, cpu_reset, - sys_cyc, (sys_cyc)&&(sys_stb)&&(sel_pic),sys_we, - sys_data, 4'hf, pic_stall, pic_ack, pic_data, - main_int_vector[14:0], pic_interrupt); + icontrol #(15) + pic( + // {{{ + i_clk, cpu_reset, + sys_cyc, (sys_cyc)&&(sys_stb)&&(sel_pic),sys_we, + sys_data, 4'hf, pic_stall, pic_ack, pic_data, + main_int_vector[14:0], pic_interrupt + // }}} + ); end endgenerate - + // }}} + //////////////////////////////////////////////////////////////////////// // // The CPU itself + // {{{ + //////////////////////////////////////////////////////////////////////// + // // - wire cpu_gbl_stb, cpu_lcl_cyc, cpu_lcl_stb, - cpu_we, cpu_dbg_we; - wire [31:0] cpu_data, cpu_idata; - wire [3:0] cpu_sel, mmu_sel; - wire cpu_stall, cpu_ack, cpu_err; - wire [31:0] cpu_dbg_data; - assign cpu_dbg_we = ((dbg_cyc)&&(dbg_stb)&&(!cmd_addr[5]) - &&(dbg_we)&&(dbg_addr)); - zipcpu #( .RESET_ADDRESS(RESET_ADDRESS), - .ADDRESS_WIDTH(VIRTUAL_ADDRESS_WIDTH), - .LGICACHE(LGICACHE), - .OPT_LGDCACHE(LGDCACHE), - .IMPLEMENT_MPY(IMPLEMENT_MPY), - .IMPLEMENT_DIVIDE(IMPLEMENT_DIVIDE), - .IMPLEMENT_FPU(IMPLEMENT_FPU), - .IMPLEMENT_LOCK(IMPLEMENT_LOCK), - .WITH_LOCAL_BUS(1'b1) - ) - thecpu(i_clk, cpu_reset, pic_interrupt, - cpu_halt, cmd_clear_pf_cache, cmd_addr[4:0], cpu_dbg_we, - dbg_idata, cpu_dbg_stall, cpu_dbg_data, - cpu_dbg_cc, cpu_break, - cpu_gbl_cyc, cpu_gbl_stb, - cpu_lcl_cyc, cpu_lcl_stb, - cpu_we, cpu_addr, cpu_data, cpu_sel, - // Return values from the Wishbone bus - cpu_stall, cpu_ack, cpu_idata, cpu_err, - cpu_op_stall, cpu_pf_stall, cpu_i_count -`ifdef DEBUG_SCOPE - , o_cpu_debug -`endif - ); - - wire ext_stall, ext_ack; - wire mmu_cyc, mmu_stb, mmu_we, mmu_stall, mmu_ack, mmu_err; - wire mmus_stall, mmus_ack; - wire [PAW-1:0] mmu_addr; - wire [31:0] mmu_data, mmu_idata, mmus_data; - // Specific responses from the MMU - wire cpu_miss; + assign cpu_clken = cmd_write + || (i_sim_write && i_sim_addr[6] == DBG_ADDR_CPU[1]) + || (dbg_stb && dbg_addr[6] == DBG_ADDR_CPU[1]); + + zipwb #( + // {{{ + .RESET_ADDRESS(RESET_ADDRESS), + .ADDRESS_WIDTH(VIRTUAL_ADDRESS_WIDTH), + .BUS_WIDTH(BUS_WIDTH), + .OPT_PIPELINED(OPT_PIPELINED), + .OPT_EARLY_BRANCHING(OPT_EARLY_BRANCHING), + .OPT_LGICACHE(OPT_LGICACHE), + .OPT_LGDCACHE(OPT_LGDCACHE), + .OPT_MPY(OPT_MPY), + .OPT_DIV(OPT_DIV), + .OPT_SHIFTS(OPT_SHIFTS), + .IMPLEMENT_FPU(OPT_FPU), + .OPT_CIS(OPT_CIS), + .OPT_LOCK(OPT_LOCK), + .OPT_LOWPOWER(OPT_LOWPOWER), + .OPT_START_HALTED(START_HALTED), + .OPT_SIM(OPT_SIM), + .OPT_DBGPORT(OPT_DBGPORT), + .OPT_TRACE_PORT(OPT_TRACE_PORT), + .OPT_PROFILER(OPT_PROFILER), + .OPT_CLKGATE(OPT_CLKGATE), + .OPT_DISTRIBUTED_REGS(OPT_DISTRIBUTED_REGS), + .OPT_USERMODE(OPT_USERMODE), + .WITH_LOCAL_BUS(1'b1) + // }}} + ) thecpu( + // {{{ + .i_clk(i_clk), .i_reset(cpu_reset), + .i_interrupt(pic_interrupt), + .i_cpu_clken(cpu_clken), + // Debug interface + // {{{ + .i_halt(cpu_halt), .i_clear_cache(cmd_clear_cache), + .i_dbg_wreg(cmd_waddr), .i_dbg_we(cmd_write), + .i_dbg_data(cmd_wdata), + .i_dbg_rreg((i_sim_write) + ? i_sim_addr[4:0] : dbg_addr[4:0]), + .o_dbg_stall(cpu_dbg_stall), + .o_dbg_reg(cpu_dbg_data), + .o_dbg_cc(cpu_dbg_cc), + .o_break(cpu_break), + // }}} + // Wishbone bus interface + // {{{ + .o_wb_gbl_cyc(cpu_gbl_cyc), .o_wb_gbl_stb(cpu_gbl_stb), + .o_wb_lcl_cyc(cpu_lcl_cyc), + .o_wb_lcl_stb(cpu_lcl_stb), + .o_wb_we(cpu_we), .o_wb_addr(cpu_addr), + .o_wb_data(cpu_data), .o_wb_sel(cpu_sel), + // Return values from the Wishbone bus + .i_wb_stall(cpu_stall), .i_wb_ack(cpu_ack), + .i_wb_data(cpu_idata), .i_wb_err(cpu_err), + // }}} + .o_op_stall(cpu_op_stall), .o_pf_stall(cpu_pf_stall), + .o_i_count(cpu_i_count), + .o_debug(o_cpu_debug), + // + .o_prof_stb(o_prof_stb), + .o_prof_addr(o_prof_addr), + .o_prof_ticks(o_prof_ticks) + // }}} + ); + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // The (unused) MMU + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // // The mmu_cpu_ lines are the return bus lines from the MMU. They // are separate from the cpu_'s lines simply because either the sys_ // (local) bus or the mmu_cpu_ (global) bus might return a response to // the CPU, and the responses haven't been merged back together again // yet. - wire mmu_cpu_stall, mmu_cpu_ack; - wire [31:0] mmu_cpu_idata; - - // The wires associated with cache snooping - wire pf_return_stb, pf_return_we, pf_return_cachable; - wire [19:0] pf_return_v, pf_return_p; `ifdef OPT_MMU // Ok ... here's the MMU - zipmmu #( .LGTBL(LGTLBSZ), - .ADDRESS_WIDTH(PHYSICAL_ADDRESS_WIDTH) - ) - themmu(i_clk, cpu_reset, - // Slave interface - (sys_stb)&&(sel_mmus), - sys_we, sys_addr[7:0], sys_data, - mmus_stall, mmus_ack, mmus_data, - // CPU global bus master lines - cpu_gbl_cyc, cpu_gbl_stb, cpu_we, cpu_addr, - cpu_data, cpu_sel, - // MMU bus master outgoing lines - mmu_cyc, mmu_stb, mmu_we, mmu_addr, mmu_data, mmu_sel, - // .... and the return from the slave(s) - mmu_stall, mmu_ack, mmu_err, mmu_idata, - // CPU gobal bus master return lines - mmu_cpu_stall, mmu_cpu_ack, cpu_err, cpu_miss, mmu_cpu_idata, - pf_return_stb, pf_return_we, pf_return_p, pf_return_v, - pf_return_cachable); + zipmmu #( + // {{{ + .LGTBL(LGTLBSZ), + .ADDRESS_WIDTH(PHYSICAL_ADDRESS_WIDTH) + // }}} + ) themmu( + // {{{ + i_clk, cpu_reset, + // Slave interface + (sys_stb)&&(sel_mmus), + sys_we, sys_addr[7:0], sys_data, + mmus_stall, mmus_ack, mmus_data, + // CPU global bus master lines + cpu_gbl_cyc, cpu_gbl_stb, cpu_we, cpu_addr, + cpu_data, cpu_sel, + // MMU bus master outgoing lines + mmu_cyc, mmu_stb, mmu_we, mmu_addr, mmu_data, mmu_sel, + // .... and the return from the slave(s) + mmu_stall, mmu_ack, mmu_err, mmu_idata, + // CPU gobal bus master return lines + mmu_cpu_stall, mmu_cpu_ack, cpu_err, cpu_miss, mmu_cpu_idata, + pf_return_stb, pf_return_we, pf_return_p, pf_return_v, + pf_return_cachable + // }}} + ); `else + reg r_mmus_ack; + assign mmu_cyc = cpu_gbl_cyc; assign mmu_stb = cpu_gbl_stb; assign mmu_we = cpu_we; @@ -850,10 +1401,14 @@ module zipsystem(i_clk, i_reset, assign mmu_cpu_idata = mmu_idata; assign mmu_cpu_stall = mmu_stall; assign mmu_cpu_ack = mmu_ack; - reg r_mmus_ack; + initial r_mmus_ack = 1'b0; always @(posedge i_clk) + if (i_reset) + r_mmus_ack <= 1'b0; + else r_mmus_ack <= (sys_stb)&&(sys_addr[7]); + assign mmus_ack = r_mmus_ack; assign mmus_stall = 1'b0; assign mmus_data = 32'h0; @@ -864,25 +1419,29 @@ module zipsystem(i_clk, i_reset, assign pf_return_we = 0; assign pf_return_cachable = 0; `endif + // // Responses from the MMU still need to be merged/muxed back together // with the responses from the local bus assign cpu_ack = ((cpu_lcl_cyc)&&(sys_ack)) ||((cpu_gbl_cyc)&&(mmu_cpu_ack)); assign cpu_stall = ((cpu_lcl_cyc)&&(sys_stall)) ||((cpu_gbl_cyc)&&(mmu_cpu_stall)); - assign cpu_idata = (cpu_gbl_cyc)?mmu_cpu_idata : sys_idata; + assign cpu_idata = (cpu_gbl_cyc)?mmu_cpu_idata + : { {(BUS_WIDTH-DBG_WIDTH){1'b0}}, sys_idata }; // The following lines (will be/) are used to allow the prefetch to // snoop on any external interaction. Until this capability is // integrated into the CPU, they are unused. Here we tell Verilator // not to be surprised that these lines are unused: - // verilator lint_off UNUSED - wire [(3+1+20+20-1):0] mmu_unused; - assign mmu_unused = { pf_return_stb, pf_return_we, - pf_return_p, pf_return_v, pf_return_cachable, - cpu_miss }; - // verilator lint_on UNUSED + // }}} + //////////////////////////////////////////////////////////////////////// + // + // The internal sys bus + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // // Now, arbitrate the bus ... first for the local peripherals // For the debugger to have access to the local system bus, the @@ -894,86 +1453,22 @@ module zipsystem(i_clk, i_reset, // and one of two other things. Either // ((cpu_halt)&&(!cpu_dbg_stall)) the CPU is completely halted, // or - // (!cmd_addr[5]) we are trying to read a CPU register + // (dbg_addr[6:5]==2'b01) we are trying to read a CPU register // while in motion. Let the user beware that, // by not waiting for the CPU to fully halt, // his results may not be what he expects. // - wire sys_dbg_cyc = ((dbg_cyc)&&(!cpu_lcl_cyc)&&(dbg_addr)) - &&(cmd_addr[5]); - assign sys_cyc = (cpu_lcl_cyc)||(sys_dbg_cyc); + assign sys_cyc = (cpu_lcl_cyc)||(dbg_cyc); assign sys_stb = (cpu_lcl_cyc) ? (cpu_lcl_stb) - : ((dbg_stb)&&(dbg_addr)&&(cmd_addr[5])); + : ((dbg_stb)&&(dbg_addr[6:5]==DBG_ADDR_SYS)); assign sys_we = (cpu_lcl_cyc) ? cpu_we : dbg_we; - assign sys_addr= (cpu_lcl_cyc) ? cpu_addr[7:0] : { 3'h0, cmd_addr[4:0]}; - assign sys_data= (cpu_lcl_cyc) ? cpu_data : dbg_idata; + assign sys_addr= (cpu_lcl_cyc) ? cpu_addr[7:0] : { 3'h0, dbg_addr[4:0]}; + assign sys_data= (cpu_lcl_cyc) ? cpu_data[DBG_WIDTH-1:0] : dbg_idata; - // Return debug response values - // A return from one of three busses: - // CMD giving command instructions to the CPU (step, halt, etc) - // CPU-DBG-DATA internal register responses from within the CPU - // sys Responses from the front-side bus here in the ZipSystem - assign dbg_odata = (!dbg_addr) ? cmd_data - :((!cmd_addr[5])?cpu_dbg_data : sys_idata); - initial dbg_ack = 1'b0; - always @(posedge i_clk) - dbg_ack <= (dbg_stb)&&(!dbg_stall); - assign dbg_stall=(dbg_cyc)&&( - ((!sys_dbg_cyc)&&(cpu_dbg_stall)) - ||(sys_stall) - )&&(dbg_addr); - - // Now for the external wishbone bus - // Need to arbitrate between the flash cache and the CPU - // The way this works, though, the CPU will stall once the flash - // cache gets access to the bus--the CPU will be stuck until the - // flash cache is finished with the bus. - wire ext_cyc, ext_stb, ext_we, ext_err; - wire [(PAW-1):0] ext_addr; - wire [31:0] ext_odata; - wire [3:0] ext_sel; - wbpriarbiter #(32,PAW) dmacvcpu(i_clk, - mmu_cyc, mmu_stb, mmu_we, mmu_addr, mmu_data, mmu_sel, - mmu_stall, mmu_ack, mmu_err, - dc_cyc, dc_stb, dc_we, dc_addr, dc_data, 4'hf, - dc_stall, dc_ack, dc_err, - ext_cyc, ext_stb, ext_we, ext_addr, ext_odata, ext_sel, - ext_stall, ext_ack, ext_err); - assign mmu_idata = ext_idata; -/* - assign ext_cyc = mmu_cyc; - assign ext_stb = mmu_stb; - assign ext_we = mmu_we; - assign ext_odata= mmu_data; - assign ext_addr = mmu_addr; - assign ext_sel = mmu_sel; - assign mmu_ack = ext_ack; - assign mmu_stall= ext_stall; - assign mmu_err = ext_err; -*/ - -`ifdef DELAY_EXT_BUS - busdelay #(.AW(PAW),.DW(32),.DELAY_STALL(0)) extbus(i_clk, i_reset, - ext_cyc, ext_stb, ext_we, ext_addr, ext_odata, ext_sel, - ext_stall, ext_ack, ext_idata, ext_err, - o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, o_wb_sel, - i_wb_stall, i_wb_ack, i_wb_data, (i_wb_err)||(wdbus_int)); -`else - assign o_wb_cyc = ext_cyc; - assign o_wb_stb = ext_stb; - assign o_wb_we = ext_we; - assign o_wb_addr = ext_addr; - assign o_wb_data = ext_odata; - assign o_wb_sel = ext_sel; - assign ext_stall = i_wb_stall; - assign ext_ack = i_wb_ack; - assign ext_idata = i_wb_data; - assign ext_err = (i_wb_err)||(wdbus_int); -`endif - - reg [31:0] tmr_data; + // tmr_data + // {{{ always @(*) begin case(sys_addr[1:0]) @@ -985,33 +1480,20 @@ module zipsystem(i_clk, i_reset, // tmr_ack == sys_stb && sel_timer end + // }}} - reg [2:0] w_ack_idx, ack_idx; - always @(*) - begin - w_ack_idx = 0; - if (sel_mmus) w_ack_idx = w_ack_idx | 3'h0; - if (sel_watchdog) w_ack_idx = w_ack_idx | 3'h1; - if (sel_bus_watchdog) w_ack_idx = w_ack_idx | 3'h2; - if (sel_apic) w_ack_idx = w_ack_idx | 3'h3; - if (sel_timer) w_ack_idx = w_ack_idx | 3'h4; - if (sel_counter) w_ack_idx = w_ack_idx | 3'h5; - if (sel_dmac) w_ack_idx = w_ack_idx | 3'h6; - if (sel_pic) w_ack_idx = w_ack_idx | 3'h7; - end - - always @(posedge i_clk) - if (sys_stb) - ack_idx <= w_ack_idx; - - reg last_sys_stb; + // last_sys_stb + // {{{ initial last_sys_stb = 0; always @(posedge i_clk) if (i_reset) last_sys_stb <= 0; else last_sys_stb <= sys_stb; + // }}} + // sys_ack, sys_idata + // {{{ always @(posedge i_clk) begin case(ack_idx) @@ -1028,33 +1510,204 @@ module zipsystem(i_clk, i_reset, if (i_reset || !sys_cyc) sys_ack <= 1'b0; end + // }}} + + // w_ack_idx + // {{{ + always @(*) + begin + w_ack_idx = 0; + if (sel_mmus) w_ack_idx = w_ack_idx | 3'h0; + if (sel_watchdog) w_ack_idx = w_ack_idx | 3'h1; + if (sel_bus_watchdog) w_ack_idx = w_ack_idx | 3'h2; + if (sel_apic) w_ack_idx = w_ack_idx | 3'h3; + if (sel_timer) w_ack_idx = w_ack_idx | 3'h4; + if (sel_counter) w_ack_idx = w_ack_idx | 3'h5; + if (sel_dmac) w_ack_idx = w_ack_idx | 3'h6; + if (sel_pic) w_ack_idx = w_ack_idx | 3'h7; + end + // }}} + // ack_idx + // {{{ + always @(posedge i_clk) + if (sys_stb) + ack_idx <= w_ack_idx; + // }}} assign sys_stall = 1'b0; + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Return debug response values + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + reg dbg_pre_ack; + reg [1:0] dbg_pre_addr; + reg [DBG_WIDTH-1:0] dbg_cpu_status; + + always @(posedge i_clk) + dbg_pre_addr <= dbg_addr[6:5]; + + always @(posedge i_clk) + dbg_cpu_status <= cpu_status; + + initial dbg_pre_ack = 1'b0; + always @(posedge i_clk) + if (i_reset || !i_dbg_cyc) + dbg_pre_ack <= 1'b0; + else + dbg_pre_ack <= dbg_stb && !dbg_stall; + + // A return from one of three busses: + // CMD giving command instructions to the CPU (step, halt, etc) + // CPU-DBG-DATA internal register responses from within the CPU + // sys Responses from the front-side bus here in the ZipSystem + // assign dbg_odata = (!dbg_addr) ? cpu_status + // :((!cmd_addr[5])?cpu_dbg_data : sys_idata); + initial dbg_ack = 1'b0; + always @(posedge i_clk) + if (i_reset || !dbg_cyc) + dbg_ack <= 1'b0; + else + dbg_ack <= dbg_pre_ack; + + always @(posedge i_clk) + if (!OPT_LOWPOWER || (dbg_cyc && dbg_pre_ack)) + casez(dbg_pre_addr) + DBG_ADDR_CPU: dbg_odata <= cpu_dbg_data; + DBG_ADDR_CTRL: dbg_odata <= dbg_cpu_status; + // DBG_ADDR_SYS: + default: dbg_odata <= sys_idata; + endcase + + assign dbg_stall = i_sim_write + || (cpu_dbg_stall && dbg_addr[6:5] == DBG_ADDR_CPU + && (!dbg_we || cmd_write)) + ||(dbg_addr[6]==DBG_ADDR_SYS[1] && cpu_lcl_cyc); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Arbitrate between CPU and DMA + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // Now for the external wishbone bus + // Need to arbitrate between the flash cache and the CPU + // The way this works, though, the CPU will stall once the flash + // cache gets access to the bus--the CPU will be stuck until the + // flash cache is finished with the bus. + wbpriarbiter #( + // {{{ + .DW(BUS_WIDTH), + .AW(PAW) + // }}} + ) dmacvcpu( + // {{{ + i_clk, + mmu_cyc, mmu_stb, mmu_we, mmu_addr, mmu_data, mmu_sel, + mmu_stall, mmu_ack, mmu_err, + dc_cyc, dc_stb, dc_we, dc_addr, dc_data, dc_sel, + dc_stall, dc_ack, dc_err, + ext_cyc, ext_stb, ext_we, ext_addr, ext_odata, ext_sel, + ext_stall, ext_ack, ext_err + // }}} + ); + assign mmu_idata = ext_idata; +/* + assign ext_cyc = mmu_cyc; + assign ext_stb = mmu_stb; + assign ext_we = mmu_we; + assign ext_odata= mmu_data; + assign ext_addr = mmu_addr; + assign ext_sel = mmu_sel; + assign mmu_ack = ext_ack; + assign mmu_stall= ext_stall; + assign mmu_err = ext_err; +*/ + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Delay access to the external bus by one clock (if necessary) + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + generate if (DELAY_EXT_BUS) + begin : DELAY_EXTERNAL_BUS + // {{{ + busdelay #( + // {{{ + .AW(PAW), + .DW(BUS_WIDTH), + .DELAY_STALL(0) + // }}} + ) extbus( + // {{{ + i_clk, i_reset, + ext_cyc, ext_stb, ext_we, ext_addr, ext_odata, ext_sel, + ext_stall, ext_ack, ext_idata, ext_err, + o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, + o_wb_sel, + i_wb_stall, i_wb_ack, i_wb_data, (i_wb_err)||(wdbus_int) + // }}} + ); + // }}} + end else begin : NO_EXTERNAL_BUS_DELAY + // {{{ + assign o_wb_cyc = ext_cyc; + assign o_wb_stb = ext_stb; + assign o_wb_we = ext_we; + assign o_wb_addr = ext_addr; + assign o_wb_data = ext_odata; + assign o_wb_sel = ext_sel; + assign ext_stall = i_wb_stall; + assign ext_ack = i_wb_ack; + assign ext_idata = i_wb_data; + assign ext_err = (i_wb_err)||(wdbus_int); + // }}} + end endgenerate + // }}} + assign o_ext_int = (cmd_halt) && (!cpu_stall); - // Make verilator happy + // Make Verilator happy + // {{{ // verilator lint_off UNUSED wire unused; - assign unused = &{ 1'b0, pic_ack, pic_stall, + assign unused = &{ 1'b0, + cpu_dbg_cc[2], + pic_ack, pic_stall, cpu_clken, tma_ack, tma_stall, tmb_ack, tmb_stall, tmc_ack, tmc_stall, jif_ack, jif_stall, no_dbg_err, dbg_sel, sel_mmus, ctri_ack, ctri_stall, mmus_stall, dmac_stall, wdt_ack, wdt_stall, actr_ack, actr_stall, wdbus_ack, i_dbg_sel, - moc_ack, mtc_ack, mic_ack, mpc_ack, - uoc_ack, utc_ack, uic_ack, upc_ack, - moc_stall, mtc_stall, mic_stall, mpc_stall, - uoc_stall, utc_stall, uic_stall, upc_stall }; -`ifndef INCLUDE_ACCOUNTING_COUNTERS - wire [11:0] unused_ctrs; - assign unused_ctrs = { - moc_int, mpc_int, mic_int, mtc_int, - uoc_int, upc_int, uic_int, utc_int, - cpu_gie, cpu_op_stall, cpu_pf_stall, cpu_i_count }; -`endif -`ifndef INCLUDE_DMA_CONTROLLER - wire [34:0] unused_dmac; - assign unused_dmac = { dc_err, dc_ack, dc_stall, dmac_int_vec }; -`endif + // moc_ack, mtc_ack, mic_ack, mpc_ack, + // uoc_ack, utc_ack, uic_ack, upc_ack, + // moc_stall, mtc_stall, mic_stall, mpc_stall, + // uoc_stall, utc_stall, uic_stall, upc_stall, + // Unused MMU pins + pf_return_stb, pf_return_we, pf_return_p, pf_return_v, + pf_return_cachable, cpu_miss }; // verilator lint_on UNUSED + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +`ifdef FORMAL +// Formal properties are maintained elsewhere +`endif +// }}} endmodule diff --git a/rtl/cpu/ziptimer.v b/rtl/cpu/ziptimer.v index b527302..2b66600 100644 --- a/rtl/cpu/ziptimer.v +++ b/rtl/cpu/ziptimer.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: ziptimer.v -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: A lighter weight implementation of the Zip Timer. @@ -44,11 +44,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015,2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -61,135 +61,188 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module ziptimer(i_clk, i_reset, i_ce, - i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, - o_wb_stall, o_wb_ack, o_wb_data, - o_int); - parameter BW = 32, VW = (BW-1), RELOADABLE=1; - input wire i_clk, i_reset, i_ce; - // Wishbone inputs - input wire i_wb_cyc, i_wb_stb, i_wb_we; - input wire [(BW-1):0] i_wb_data; - // Wishbone outputs - output wire o_wb_stall; - output reg o_wb_ack; - output wire [(BW-1):0] o_wb_data; - // Interrupt line - output reg o_int; - +// }}} +module ziptimer #( + // {{{ + parameter BW = 32, VW = (BW-1), + parameter [0:0] RELOADABLE = 1 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, i_ce, + // Wishbone inputs + input wire i_wb_cyc, i_wb_stb, i_wb_we, + input wire [BW-1:0] i_wb_data, + input wire [BW/8-1:0] i_wb_sel, + // Wishbone outputs + output wire o_wb_stall, + output reg o_wb_ack, + output wire [BW-1:0] o_wb_data, + // Interrupt line + output reg o_int + // }}} + ); + + // Local declarations + // {{{ reg r_running; reg r_zero = 1'b1; reg [(VW-1):0] r_value; wire wb_write; - assign wb_write = ((i_wb_stb)&&(i_wb_we)); wire auto_reload; wire [(VW-1):0] interval_count; + // }}} + assign wb_write = ((i_wb_stb)&&(i_wb_we)); + + // r_running + // {{{ initial r_running = 1'b0; always @(posedge i_clk) - if (i_reset) - r_running <= 1'b0; - else if (wb_write) - r_running <= (|i_wb_data[(VW-1):0]); - else if ((r_zero)&&(!auto_reload)) - r_running <= 1'b0; - + if (i_reset) + r_running <= 1'b0; + else if (wb_write) + r_running <= (|i_wb_data[(VW-1):0]); + else if ((r_zero)&&(!auto_reload)) + r_running <= 1'b0; + // }}} + + // r_auto_reload, r_interval_count + // {{{ generate if (RELOADABLE != 0) begin + // {{{ reg r_auto_reload; reg [(VW-1):0] r_interval_count; + // r_auto_reload + // {{{ initial r_auto_reload = 1'b0; - always @(posedge i_clk) - if (i_reset) - r_auto_reload <= 1'b0; - else if (wb_write) - r_auto_reload <= (i_wb_data[(BW-1)]) + if (i_reset) + r_auto_reload <= 1'b0; + else if (wb_write) + r_auto_reload <= (i_wb_data[(BW-1)]) &&(|i_wb_data[(VW-1):0]); + // }}} assign auto_reload = r_auto_reload; + // r_interval_count + // {{{ // If setting auto-reload mode, and the value to other // than zero, set the auto-reload value always @(posedge i_clk) - if (wb_write) + if (i_reset) + r_interval_count <= 0; + else if (wb_write) r_interval_count <= i_wb_data[(VW-1):0]; + // }}} + assign interval_count = r_interval_count; - end else begin + // }}} + end else begin : NO_AUTO_RELOAD + // {{{ assign auto_reload = 1'b0; assign interval_count = 0; + // }}} end endgenerate + // }}} - + // r_value + // {{{ initial r_value = 0; always @(posedge i_clk) - if (i_reset) - r_value <= 0; - else if (wb_write) - r_value <= i_wb_data[(VW-1):0]; - else if ((i_ce)&&(r_running)) - begin - if (!r_zero) - r_value <= r_value - 1'b1; - else if (auto_reload) - r_value <= interval_count; - end - + if (i_reset) + r_value <= 0; + else if (wb_write) + r_value <= i_wb_data[(VW-1):0]; + else if ((i_ce)&&(r_running)) + begin + if (!r_zero) + r_value <= r_value - 1'b1; + else if (auto_reload) + r_value <= interval_count; + end + // }}} + + // r_zero + // {{{ always @(posedge i_clk) - if (i_reset) + if (i_reset) + r_zero <= 1'b1; + else if (wb_write) + r_zero <= (i_wb_data[(VW-1):0] == 0); + else if ((r_running)&&(i_ce)) + begin + if (r_value == { {(VW-1){1'b0}}, 1'b1 }) r_zero <= 1'b1; - else if (wb_write) - r_zero <= (i_wb_data[(VW-1):0] == 0); - else if ((r_running)&&(i_ce)) - begin - if (r_value == { {(VW-1){1'b0}}, 1'b1 }) - r_zero <= 1'b1; - else if ((r_zero)&&(auto_reload)) - r_zero <= 1'b0; - end + else if ((r_zero)&&(auto_reload)) + r_zero <= 1'b0; + end + // }}} + // o_int + // {{{ // Set the interrupt on our last tick, as we transition from one to // zero. initial o_int = 1'b0; always @(posedge i_clk) - if ((i_reset)||(wb_write)||(!i_ce)) - o_int <= 1'b0; - else // if (i_ce) - o_int <= (r_value == { {(VW-1){1'b0}}, 1'b1 }); - + if ((i_reset)||(wb_write)||(!i_ce)) + o_int <= 1'b0; + else // if (i_ce) + o_int <= (r_value == { {(VW-1){1'b0}}, 1'b1 }); + // }}} + + // o_wb_ack + // {{{ initial o_wb_ack = 1'b0; always @(posedge i_clk) o_wb_ack <= (!i_reset)&&(i_wb_stb); + // }}} assign o_wb_stall = 1'b0; + // o_wb_data + // {{{ generate if (VW < BW-1) assign o_wb_data = { auto_reload, {(BW-1-VW){1'b0}}, r_value }; else assign o_wb_data = { auto_reload, r_value }; endgenerate + // }}} // Make verilator happy + // {{{ + // verilator coverage_off // verilator lint_off UNUSED - wire [32:0] unused; - assign unused = { i_wb_cyc, i_wb_data }; + wire unused; + assign unused = &{ 1'b0, i_wb_cyc, i_wb_data, i_wb_sel }; // verilator lint_on UNUSED - + // verilator coverage_on + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // The formal properties for this module are maintained elsewhere `endif +// }}} endmodule diff --git a/rtl/cpu/zipwb.v b/rtl/cpu/zipwb.v new file mode 100644 index 0000000..19ada97 --- /dev/null +++ b/rtl/cpu/zipwb.v @@ -0,0 +1,679 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: zipwb.v +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core +// +// Purpose: This is the top level module holding the core of the Zip CPU +// together. The Zip CPU is designed to be as simple as possible. +// (actual implementation aside ...) The instruction set is about as +// RISC as you can get, with only 26 instruction types currently supported. +// (There are still 8-instruction Op-Codes reserved for floating point, +// and 5 which can be used for transactions not requiring registers.) +// Please see the accompanying spec.pdf file for a description of these +// instructions. +// +// All instructions are 32-bits wide. All bus accesses, both address and +// data, are 32-bits over a wishbone bus. +// +// The Zip CPU is fully pipelined with the following pipeline stages: +// +// 1. Prefetch, returns the instruction from memory. +// +// 2. Instruction Decode +// +// 3. Read Operands +// +// 4. Apply Instruction +// +// 4. Write-back Results +// +// Further information about the inner workings of this CPU, such as +// what causes pipeline stalls, may be found in the spec.pdf file. (The +// documentation within this file had become out of date and out of sync +// with the spec.pdf, so look to the spec.pdf for accurate and up to date +// information.) +// +// +// In general, the pipelining is controlled by three pieces of logic +// per stage: _ce, _stall, and _valid. _valid means that the stage +// holds a valid instruction. _ce means that the instruction from the +// previous stage is to move into this one, and _stall means that the +// instruction from the previous stage may not move into this one. +// The difference between these control signals allows individual stages +// to propagate instructions independently. In general, the logic works +// as: +// +// +// assign (n)_ce = (n-1)_valid && (!(n)_stall) +// +// +// always @(posedge i_clk) +// if ((i_reset)||(clear_pipeline)) +// (n)_valid = 0 +// else if (n)_ce +// (n)_valid = 1 +// else if (n+1)_ce +// (n)_valid = 0 +// +// assign (n)_stall = ( (n-1)_valid && ( pipeline hazard detection ) ) +// || ( (n)_valid && (n+1)_stall ); +// +// and ... +// +// always @(posedge i_clk) +// if (n)_ce +// (n)_variable = ... whatever logic for this stage +// +// Note that a stage can stall even if no instruction is loaded into +// it. +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +// +//////////////////////////////////////////////////////////////////////////////// +// +`default_nettype none +// }}} +module zipwb #( + // {{{ + parameter [31:0] RESET_ADDRESS=32'h010_0000, + parameter ADDRESS_WIDTH=30, + BUS_WIDTH = 32, // Bus width + OPT_LGICACHE=12, + localparam DATA_WIDTH = 32, // CPU data width + parameter OPT_MPY = 3, + parameter [0:0] OPT_DIV = 1, + parameter [0:0] OPT_SHIFTS = 1, + parameter [0:0] IMPLEMENT_FPU = 0, + parameter [0:0] OPT_EARLY_BRANCHING = 1, + parameter [0:0] OPT_CIS = 1'b1, + parameter [0:0] OPT_DISTRIBUTED_REGS = 1'b1, + parameter [0:0] OPT_PIPELINED = 1'b1, + parameter [0:0] OPT_START_HALTED=1, + parameter [0:0] OPT_LOCK=1, + parameter [0:0] OPT_LOWPOWER = 1'b0, + parameter OPT_LGDCACHE = 10, + parameter [0:0] OPT_SIM = 1'b1, + parameter [0:0] OPT_CLKGATE = 1'b0, + parameter [0:0] WITH_LOCAL_BUS = 1'b1, + parameter [0:0] OPT_DBGPORT = 1'b1, + parameter [0:0] OPT_TRACE_PORT = 1'b0, + parameter [0:0] OPT_PROFILER = 1'b0, + parameter [0:0] OPT_USERMODE = 1'b1, + localparam AW=ADDRESS_WIDTH, + localparam WBLSB = $clog2(BUS_WIDTH/8) +`ifdef FORMAL + , parameter F_LGDEPTH=8 +`endif + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, i_interrupt, + input wire i_cpu_clken, + // Debug interface -- inputs + input wire i_halt, i_clear_cache, + input wire [4:0] i_dbg_wreg, + input wire i_dbg_we, + input wire [DATA_WIDTH-1:0] i_dbg_data, + input wire [4:0] i_dbg_rreg, + // Debug interface -- outputs + output wire o_dbg_stall, + output wire [DATA_WIDTH-1:0] o_dbg_reg, + output wire [2:0] o_dbg_cc, + output wire o_break, + // CPU interface to the wishbone bus + // Wishbone interface -- outputs + output wire o_wb_gbl_cyc, o_wb_gbl_stb, + output wire o_wb_lcl_cyc, o_wb_lcl_stb, + o_wb_we, + output wire [AW-1:0] o_wb_addr, + output wire [BUS_WIDTH-1:0] o_wb_data, + output wire [BUS_WIDTH/8-1:0] o_wb_sel, + // Wishbone interface -- inputs + input wire i_wb_stall, i_wb_ack, + input wire [BUS_WIDTH-1:0] i_wb_data, + input wire i_wb_err, + // Accounting outputs ... to help us count stalls and usage + output wire o_op_stall, + output wire o_pf_stall, + output wire o_i_count, + // + output wire [31:0] o_debug, + output wire o_prof_stb, + output wire [AW+WBLSB-1:0] o_prof_addr, + output wire [31:0] o_prof_ticks + // }}} + ); + + // Declarations + // {{{ + localparam [0:0] OPT_DCACHE = (OPT_LGDCACHE > 2); + localparam [0:0] OPT_PIPELINED_BUS_ACCESS = (OPT_PIPELINED); + localparam [0:0] OPT_MEMPIPE = OPT_PIPELINED_BUS_ACCESS; + localparam INSN_WIDTH = 32; + + wire cpu_clken, cpu_clock, clk_gate; + wire [31:0] cpu_debug; + + // Fetch + // {{{ + wire pf_new_pc, clear_icache, pf_ready; + wire [AW+WBLSB-1:0] pf_request_address; + wire [INSN_WIDTH-1:0] pf_instruction; + wire [AW+WBLSB-1:0] pf_instruction_pc; + wire pf_valid, pf_illegal; + // + wire pf_cyc, pf_stb, pf_stall, pf_ack, pf_err; + wire [AW-1:0] pf_addr; + // verilator coverage_off + // Since we aren't writing, these values will be constants + wire pf_we; + wire [BUS_WIDTH-1:0] pf_data; + // verilator coverage_on + // }}} + // Memory + // {{{ + wire clear_dcache, mem_ce, bus_lock; + wire [2:0] mem_op; + wire [31:0] mem_cpu_addr; + wire [AW+WBLSB-1:0] mem_lock_pc; // Byte address + wire [DATA_WIDTH-1:0] mem_wdata; + wire [BUS_WIDTH-1:0] mem_data; + wire [4:0] mem_reg; + wire mem_busy, mem_rdbusy, mem_pipe_stalled, mem_valid, + mem_bus_err; + wire [4:0] mem_wreg; + wire [DATA_WIDTH-1:0] mem_result; + // + wire mem_stb_lcl, mem_stb_gbl, mem_cyc_lcl, mem_cyc_gbl; + wire [AW-1:0] mem_bus_addr; + wire mem_we, mem_stall, mem_ack, mem_err; + wire [BUS_WIDTH/8-1:0] mem_sel; + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // + // The ZipCPU Core + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + wire w_dbg_stall; + + zipcore #( + // {{{ + .RESET_ADDRESS(RESET_ADDRESS), + .ADDRESS_WIDTH(AW+WBLSB-$clog2(DATA_WIDTH/8)), + .OPT_MPY(OPT_MPY), + .OPT_DIV(OPT_DIV), + .OPT_SHIFTS(OPT_SHIFTS), + .IMPLEMENT_FPU(IMPLEMENT_FPU), + .OPT_EARLY_BRANCHING(OPT_EARLY_BRANCHING), + .OPT_START_HALTED(OPT_START_HALTED), + .OPT_CIS(OPT_CIS), + .OPT_SIM(OPT_SIM), + .OPT_CLKGATE(OPT_CLKGATE), + .OPT_PIPELINED(OPT_PIPELINED), + .OPT_PIPELINED_BUS_ACCESS(OPT_MEMPIPE), + .OPT_DISTRIBUTED_REGS(OPT_DISTRIBUTED_REGS), + .OPT_USERMODE(OPT_USERMODE), + .OPT_LOCK(OPT_LOCK), + .OPT_LOWPOWER(OPT_LOWPOWER), + .OPT_DBGPORT(OPT_DBGPORT), + .OPT_TRACE_PORT(OPT_TRACE_PORT), + .OPT_PROFILER(OPT_PROFILER) +`ifdef FORMAL + , .F_LGDEPTH(F_LGDEPTH) +`endif + // }}} + ) core ( + // {{{ + .i_clk(cpu_clock), .i_reset(i_reset), .i_interrupt(i_interrupt), + .o_clken(cpu_clken), + // Debug interface + // {{{ + .i_halt(i_halt), .i_clear_cache(i_clear_cache), + .i_dbg_wreg(i_dbg_wreg), .i_dbg_we(i_dbg_we), + .i_dbg_data(i_dbg_data), + .i_dbg_rreg(i_dbg_rreg), .o_dbg_stall(w_dbg_stall), + .o_dbg_reg(o_dbg_reg), .o_dbg_cc(o_dbg_cc), + .o_break(o_break), + // }}} + // Instruction fetch interface + // {{{ + .o_pf_new_pc(pf_new_pc), .o_clear_icache(clear_icache), + .o_pf_ready(pf_ready), + .o_pf_request_address(pf_request_address), + .i_pf_valid(pf_valid), .i_pf_illegal(pf_illegal), + .i_pf_instruction(pf_instruction), + .i_pf_instruction_pc(pf_instruction_pc), + // }}} + // Memory unit interface + // {{{ + .o_clear_dcache(clear_dcache), .o_mem_ce(mem_ce), + .o_bus_lock(bus_lock), + .o_mem_op(mem_op), .o_mem_addr(mem_cpu_addr), + .o_mem_data(mem_wdata), + .o_mem_lock_pc(mem_lock_pc), + .o_mem_reg(mem_reg), + .i_mem_busy(mem_busy), .i_mem_rdbusy(mem_rdbusy), + .i_mem_pipe_stalled(mem_pipe_stalled), + .i_mem_valid(mem_valid), + .i_bus_err(mem_bus_err), + .i_mem_wreg(mem_wreg), + .i_mem_result(mem_result), + // }}} + // Accounting/CPU usage interface + // {{{ + .o_op_stall(o_op_stall), .o_pf_stall(o_pf_stall), + .o_i_count(o_i_count), + // }}} + .o_debug(cpu_debug), + .o_prof_stb(o_prof_stb), + .o_prof_addr(o_prof_addr), + .o_prof_ticks(o_prof_ticks) + // }}} + ); + + assign o_dbg_stall = w_dbg_stall || !clk_gate; + // }}} + // o_debug -- the debugging bus input + // {{{ + assign o_debug = cpu_debug; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Instruction Fetch + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + generate if (OPT_LGICACHE <= 1) + begin : SINGLE_FETCH + + prefetch #( + // {{{ + .ADDRESS_WIDTH(ADDRESS_WIDTH+WBLSB), + .INSN_WIDTH(INSN_WIDTH), + .DATA_WIDTH(BUS_WIDTH), + // .OPT_LOWPOWER(OPT_LOWPOWER), (Unused) + .OPT_LITTLE_ENDIAN(1'b0) + // }}} + ) pf ( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + // CPU signals + // {{{ + .i_new_pc(pf_new_pc), .i_clear_cache(clear_icache), + .i_ready(pf_ready && clk_gate), + .i_pc(pf_request_address), + .o_valid(pf_valid), .o_illegal(pf_illegal), + .o_insn(pf_instruction), + .o_pc(pf_instruction_pc), + // }}} + // Wishbone signals + // {{{ + .o_wb_cyc(pf_cyc), .o_wb_stb(pf_stb), + .o_wb_we(pf_we), .o_wb_addr(pf_addr), + .o_wb_data(pf_data), + .i_wb_stall(pf_stall), .i_wb_ack(pf_ack), + .i_wb_err(pf_err), .i_wb_data(i_wb_data) + // }}} + // }}} + ); + + end else if (OPT_LGICACHE <= 2) + begin : DBLFETCH + + dblfetch #( + // {{{ + .ADDRESS_WIDTH(ADDRESS_WIDTH+WBLSB), + .DATA_WIDTH(BUS_WIDTH), + .INSN_WIDTH(INSN_WIDTH), + // .OPT_LOWPOWER(OPT_LOWPOWER), (Unused) + .OPT_LITTLE_ENDIAN(1'b0) + // }}} + ) pf ( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + // CPU signals + // {{{ + .i_new_pc(pf_new_pc), + .i_clear_cache(clear_icache), + .i_ready(pf_ready && clk_gate), + .i_pc(pf_request_address), + .o_valid(pf_valid), .o_illegal(pf_illegal), + .o_insn(pf_instruction), + .o_pc(pf_instruction_pc), + // }}} + // Wishbone signals + // {{{ + .o_wb_cyc(pf_cyc), .o_wb_stb(pf_stb), .o_wb_we(pf_we), + .o_wb_addr(pf_addr), .o_wb_data(pf_data), + .i_wb_stall(pf_stall), .i_wb_ack(pf_ack), + .i_wb_err(pf_err), .i_wb_data(i_wb_data) + // }}} + // }}} + ); + + end else begin : PFCACHE + + pfcache #( + // {{{ + .BUS_WIDTH(BUS_WIDTH), + // .INSN_WIDTH(INSN_WIDTH), + .LGCACHELEN(OPT_LGICACHE-WBLSB), + // .OPT_LOWPOWER(OPT_LOWPOWER), (Unused) + .ADDRESS_WIDTH(ADDRESS_WIDTH) + // }}} + ) pf( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + // CPU signals + .i_new_pc(pf_new_pc), .i_clear_cache(clear_icache), + .i_ready(pf_ready && clk_gate), .i_pc(pf_request_address), + .o_valid(pf_valid), .o_illegal(pf_illegal), + .o_insn(pf_instruction), + .o_pc(pf_instruction_pc), + // Wishbone signals + .o_wb_cyc(pf_cyc), .o_wb_stb(pf_stb), + .o_wb_we(pf_we), .o_wb_addr(pf_addr), + .o_wb_data(pf_data), + .i_wb_stall(pf_stall), .i_wb_ack(pf_ack), + .i_wb_err(pf_err), .i_wb_data(i_wb_data) + // }}} + ); + + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Memory Unit + // {{{ + //////////////////////////////////////////////////////////////////////// + // + generate if (OPT_DCACHE) + begin : DATA_CACHE + + dcache #( + // {{{ + .LGCACHELEN(OPT_LGDCACHE-WBLSB), + .ADDRESS_WIDTH(AW), + .BUS_WIDTH(BUS_WIDTH), + .LGNLINES(OPT_LGDCACHE-WBLSB-3), + .OPT_LOCAL_BUS(WITH_LOCAL_BUS), + .OPT_LOWPOWER(OPT_LOWPOWER), + .OPT_PIPE(OPT_MEMPIPE), + .OPT_LOCK(OPT_LOCK) +`ifdef FORMAL + , .OPT_FIFO_DEPTH(2) + , .F_LGDEPTH(F_LGDEPTH) +`endif + // }}} + ) mem( + // {{{ + .i_clk(cpu_clock), .i_reset(i_reset),.i_clear(clear_dcache), + // CPU interface + .i_pipe_stb(mem_ce), .i_lock(bus_lock && OPT_PIPELINED), + .i_op(mem_op), .i_addr(mem_cpu_addr),.i_data(mem_wdata), + .i_oreg(mem_reg), + .o_busy(mem_busy), .o_rdbusy(mem_rdbusy), + .o_pipe_stalled(mem_pipe_stalled), + .o_valid(mem_valid), .o_err(mem_bus_err), + .o_wreg(mem_wreg), .o_data(mem_result), + // Wishbone interface + .o_wb_cyc_gbl(mem_cyc_gbl), .o_wb_cyc_lcl(mem_cyc_lcl), + .o_wb_stb_gbl(mem_stb_gbl), .o_wb_stb_lcl(mem_stb_lcl), + .o_wb_we(mem_we), .o_wb_addr(mem_bus_addr), + .o_wb_data(mem_data),.o_wb_sel(mem_sel), + .i_wb_stall(mem_stall), .i_wb_ack(mem_ack), + .i_wb_err(mem_err),.i_wb_data(i_wb_data) + // }}} + ); + + end else if (OPT_MEMPIPE) + begin : PIPELINED_MEM + + pipemem #( + // {{{ + .ADDRESS_WIDTH(AW), + .BUS_WIDTH(BUS_WIDTH), + .OPT_LOCK(OPT_LOCK), + // .OPT_LOWPOWER(OPT_LOWPOWER), (Unused) + .WITH_LOCAL_BUS(WITH_LOCAL_BUS) +`ifdef FORMAL + , .OPT_MAXDEPTH(4'h3), + .F_LGDEPTH(F_LGDEPTH) +`endif + // }}} + ) domem( + // {{{ + .i_clk(cpu_clock), .i_reset(i_reset), + // CPU interface + .i_pipe_stb(mem_ce), .i_lock(bus_lock && OPT_PIPELINED), + .i_op(mem_op), .i_addr(mem_cpu_addr), + .i_data(mem_wdata), .i_oreg(mem_reg), + .o_busy(mem_busy), .o_rdbusy(mem_rdbusy), + .o_pipe_stalled(mem_pipe_stalled), + .o_valid(mem_valid), .o_err(mem_bus_err), + .o_wreg(mem_wreg), .o_result(mem_result), + // Wishbone interface + .o_wb_cyc_gbl(mem_cyc_gbl), .o_wb_cyc_lcl(mem_cyc_lcl), + .o_wb_stb_gbl(mem_stb_gbl), + .o_wb_stb_lcl(mem_stb_lcl), + .o_wb_we(mem_we), .o_wb_addr(mem_bus_addr), + .o_wb_data(mem_data),.o_wb_sel(mem_sel), + .i_wb_stall(mem_stall), .i_wb_ack(mem_ack), + .i_wb_err(mem_err),.i_wb_data(i_wb_data) + // }}} + ); + + end else begin : BARE_MEM + + memops #( + // {{{ + .ADDRESS_WIDTH(AW), + .BUS_WIDTH(BUS_WIDTH), + .OPT_LOWPOWER(OPT_LOWPOWER), + .OPT_LOCK(OPT_LOCK), + .WITH_LOCAL_BUS(WITH_LOCAL_BUS) +`ifdef FORMAL + , .F_LGDEPTH(F_LGDEPTH) +`endif // F_LGDEPTH + // }}} + ) domem( + // {{{ + .i_clk(cpu_clock), .i_reset(i_reset), + // CPU interface + .i_stb(mem_ce), .i_lock(bus_lock && OPT_PIPELINED), + .i_op(mem_op), .i_addr(mem_cpu_addr), + .i_data(mem_wdata), .i_oreg(mem_reg), + .o_busy(mem_busy), .o_rdbusy(mem_rdbusy), + .o_valid(mem_valid), .o_err(mem_bus_err), + .o_wreg(mem_wreg), .o_result(mem_result), + // Wishbone interface + .o_wb_cyc_gbl(mem_cyc_gbl), .o_wb_cyc_lcl(mem_cyc_lcl), + .o_wb_stb_gbl(mem_stb_gbl), .o_wb_stb_lcl(mem_stb_lcl), + .o_wb_we(mem_we), .o_wb_addr(mem_bus_addr), + .o_wb_data(mem_data), .o_wb_sel(mem_sel), + .i_wb_stall(mem_stall), .i_wb_ack(mem_ack), + .i_wb_err(mem_err), .i_wb_data(i_wb_data) + // }}} + ); + + assign mem_pipe_stalled = mem_busy; + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Bus arbiter + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // Either the prefetch or the instruction gets the memory bus, but + // never both under this arbitration scheme. + generate if (OPT_PIPELINED) + begin : PRIORITY_DATA + + wbdblpriarb #( + // {{{ + .AW(AW), + .DW(BUS_WIDTH), + .OPT_ZERO_ON_IDLE(OPT_LOWPOWER) + // }}} + ) pformem( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + // Memory access to the arbiter, priority position + .i_a_cyc_a(mem_cyc_gbl), .i_a_cyc_b(mem_cyc_lcl), + .i_a_stb_a(mem_stb_gbl), .i_a_stb_b(mem_stb_lcl), + .i_a_we(mem_we), .i_a_adr(mem_bus_addr), + .i_a_dat(mem_data), .i_a_sel(mem_sel), + .o_a_stall(mem_stall), .o_a_ack(mem_ack), + .o_a_err(mem_err), + // Prefetch access to the arbiter + // + // At a first glance, we might want something like: + // + // pf_cyc, 1'b0, pf_stb, 1'b0, pf_we, pf_addr, pf_data, 4'hf, + // + // However, we know that the prefetch will not generate + // any writes. Therefore, the write specific lines + // (mem_data) can be shared with the memory in order to + // ease timing and LUT usage. This is not true of + // mem_sel, which may be used to know which bytes we are + // reading from. + .i_b_cyc_a(pf_cyc), .i_b_cyc_b(1'b0), + .i_b_stb_a(pf_stb), .i_b_stb_b(1'b0), + .i_b_we(pf_we), .i_b_adr(pf_addr), + .i_b_dat(mem_data), + .i_b_sel({(BUS_WIDTH/8){1'b1}}), + .o_b_stall(pf_stall), .o_b_ack(pf_ack), + .o_b_err(pf_err), + // Common wires, in and out, of the arbiter + .o_cyc_a(o_wb_gbl_cyc), .o_cyc_b(o_wb_lcl_cyc), + .o_stb_a(o_wb_gbl_stb), .o_stb_b(o_wb_lcl_stb), + .o_we(o_wb_we), .o_adr(o_wb_addr), + .o_dat(o_wb_data), .o_sel(o_wb_sel), + .i_stall(i_wb_stall), .i_ack(i_wb_ack), + .i_err(i_wb_err) + // }}} + ); + + end else begin : PRIORITY_PREFETCH + + wbdblpriarb #( + // {{{ + .DW(BUS_WIDTH), .AW(AW), + .OPT_ZERO_ON_IDLE(OPT_LOWPOWER) + // }}} + ) pformem( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + // Prefetch access to the arbiter, priority position + // + .i_a_cyc_a(pf_cyc), .i_a_cyc_b(1'b0), + .i_a_stb_a(pf_stb), .i_a_stb_b(1'b0), + .i_a_we(pf_we), .i_a_adr(pf_addr), + .i_a_dat(mem_data), + .i_a_sel({(BUS_WIDTH/8){1'b1}}), + .o_a_stall(pf_stall), .o_a_ack(pf_ack), + .o_a_err(pf_err), + // Memory access to the arbiter + .i_b_cyc_a(mem_cyc_gbl), .i_b_cyc_b(mem_cyc_lcl), + .i_b_stb_a(mem_stb_gbl),.i_b_stb_b(mem_stb_lcl), + .i_b_we(mem_we), .i_b_adr(mem_bus_addr), + .i_b_dat(mem_data), .i_b_sel(mem_sel), + .o_b_stall(mem_stall), .o_b_ack(mem_ack), + .o_b_err(mem_err), + // Common wires, in and out, of the arbiter + .o_cyc_a(o_wb_gbl_cyc), .o_cyc_b(o_wb_lcl_cyc), + .o_stb_a(o_wb_gbl_stb), .o_stb_b(o_wb_lcl_stb), + .o_we(o_wb_we), .o_adr(o_wb_addr), + .o_dat(o_wb_data), .o_sel(o_wb_sel), + .i_stall(i_wb_stall), .i_ack(i_wb_ack), + .i_err(i_wb_err) + // }}} + ); + + end endgenerate + //}}} + //////////////////////////////////////////////////////////////////////// + // + // (Optional) Clock Gate + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + generate if (OPT_CLKGATE) + begin : GATE_CPU_CLOCK + // {{{ + reg gatep; + reg gaten /* verilator clock_enable */; + + initial gatep = 1'b1; + always @(posedge i_clk) + if (i_reset) + gatep <= 1'b1; + else + gatep <= cpu_clken || i_dbg_we || i_cpu_clken; + + initial gaten = 1'b1; + always @(negedge i_clk) + if (i_reset) + gaten <= 1'b1; + else + gaten <= gatep; + + assign cpu_clock = i_clk && gaten; + assign clk_gate = gatep; + // }}} + end else begin : NO_CLOCK_GATE + + assign cpu_clock = i_clk; + assign clk_gate = 1'b1; + + // Verilattor lint_off UNUSED + wire unused_clk; + assign unused_clk = &{ 1'b0, i_cpu_clken, cpu_clken }; + // Verilator lint_on UNUSED + end endgenerate + // }}} + + // Make Verilator happy + // {{{ + // verilator coverage_off + // Verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, pf_data, mem_lock_pc, clear_dcache }; + // Verilator lint_on UNUSED + // verilator coverage_on + // }}} +endmodule diff --git a/rtl/main.v b/rtl/main.v index c391956..bd964ca 100644 --- a/rtl/main.v +++ b/rtl/main.v @@ -2,7 +2,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: ./main.v -// +// {{{ // Project: ZBasic, a generic toplevel implementation using the full ZipCPU // // DO NOT EDIT THIS FILE! @@ -15,11 +15,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -32,15 +32,19 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} `default_nettype none +//////////////////////////////////////////////////////////////////////////////// +// +// Macro defines +// {{{ // // // Here is a list of defines which may be used, post auto-design @@ -55,16 +59,16 @@ // First, the independent access fields for any bus masters `define WBUBUS_MASTER // And then for the independent peripherals -`define BUSCONSOLE_ACCESS -`define GPIO_ACCESS +`define INCLUDE_ZIPCPU +`define BKRAM_ACCESS `define VERSION_ACCESS -`define BUSPIC_ACCESS +`define SDSPI_ACCESS `define RTC_ACCESS -`define BKRAM_ACCESS -`define FLASH_ACCESS -`define INCLUDE_ZIPCPU +`define BUSCONSOLE_ACCESS +`define BUSPIC_ACCESS `define PWRCOUNT_ACCESS -`define SDSPI_ACCESS +`define GPIO_ACCESS +`define FLASH_ACCESS // // // The list of those things that have @DEPENDS tags @@ -75,22 +79,26 @@ // Any core with both an @ACCESS and a @DEPENDS tag will show up here. // The @DEPENDS tag will turn into a series of ifdef's, with the @ACCESS // being defined only if all of the ifdef's are true// -`ifdef RTC_ACCESS -`define RTCDATE_ACCESS -`endif // RTC_ACCESS +// Deplist for @$(PREFIX)=flashcfg `ifdef FLASH_ACCESS `define FLASHCFG_ACCESS `endif // FLASH_ACCESS +// Deplist for @$(PREFIX)=rtcdate +`ifdef RTC_ACCESS +`define RTCDATE_ACCESS +`endif // RTC_ACCESS // // End of dependency list // // +// }}} +//////////////////////////////////////////////////////////////////////////////// // // Any include files -// +// {{{ // These are drawn from anything with a MAIN.INCLUDE definition. `include "builddate.v" -// +// }}} // // Finally, we define our main module itself. We start with the list of // I/O ports, or wires, passed into (or out of) the main function. @@ -99,40 +107,32 @@ // from the fields given by @MAIN.PORTLIST // module main(i_clk, i_reset, - // GPIO ports - i_gpio, o_gpio, + // {{{ + i_cpu_reset, + i_sim_write, i_sim_addr, i_sim_data, + o_prof_stb, o_prof_addr, o_prof_ticks, // UART/host to wishbone interface i_wbu_uart_rx, o_wbu_uart_tx, - // The Universal QSPI Flash - o_qspi_cs_n, o_qspi_sck, o_qspi_dat, i_qspi_dat, o_qspi_mod, - i_cpu_reset, // The SD-Card wires - o_sd_sck, o_sd_cmd, o_sd_data, i_sd_cmd, i_sd_data, i_sd_detect); + o_sd_sck, o_sd_cmd, o_sd_data, i_sd_cmd, i_sd_data, i_sd_detect, + // GPIO ports + i_gpio, o_gpio, + // The Universal QSPI Flash + o_qspi_cs_n, o_qspi_sck, o_qspi_dat, i_qspi_dat, o_qspi_mod + // }}} + ); +//////////////////////////////////////////////////////////////////////////////// // // Any parameter definitions -// +// {{{ // These are drawn from anything with a MAIN.PARAM definition. // As they aren't connected to the toplevel at all, it would // be best to use localparam over parameter, but here we don't // check + //////////////////////////////////////////////////////////////////////// // - // WBUBUS parameters - // - // Baudrate : 1000000 - // Clock : 100000000 - localparam [23:0] BUSUART = 24'h64; // 1000000 baud - localparam DBGBUSBITS = $clog2(BUSUART); - // - // Maximum command is 6 bytes, where each byte takes 10 baud clocks - // and each baud clock requires DBGBUSBITS to represent. Here, - // we'll add one more for good measure. - localparam DBGBUSWATCHDOG_RAW = DBGBUSBITS + 9; - localparam DBGBUSWATCHDOG = (DBGBUSWATCHDOG_RAW > 19) - ? DBGBUSWATCHDOG_RAW : 19; - // - // - // Variables/definitions needed by the ZipCPU BUS master - // + // Variables/definitions/parameters used by the ZipCPU bus master + // {{{ // // A 32-bit address indicating where the ZipCPU should start running // from @@ -149,6 +149,20 @@ module main(i_clk, i_reset, // A boolean, indicating whether or not the ZipCPU be halted on startup? localparam ZIP_START_HALTED=1'b1; // + // WBUBUS parameters + // + // Baudrate : 1000000 + // Clock : 100000000 + localparam [23:0] BUSUART = 24'h64; // 1000000 baud + localparam DBGBUSBITS = $clog2(BUSUART); + // + // Maximum command is 6 bytes, where each byte takes 10 baud clocks + // and each baud clock requires DBGBUSBITS to represent. Here, + // we'll add one more for good measure. + localparam DBGBUSWATCHDOG_RAW = DBGBUSBITS + 9; + localparam DBGBUSWATCHDOG = (DBGBUSWATCHDOG_RAW > 19) + ? DBGBUSWATCHDOG_RAW : 19; + // // Initial calendar DATE // `ifdef VERSION_ACCESS @@ -156,7 +170,11 @@ module main(i_clk, i_reset, `else parameter INITIAL_DATE = 30'h20000101; `endif +// }}} +//////////////////////////////////////////////////////////////////////////////// // +// Port declarations +// {{{ // The next step is to declare all of the various ports that were just // listed above. // @@ -167,19 +185,17 @@ module main(i_clk, i_reset, // verilator lint_off UNUSED input wire i_reset; // verilator lint_on UNUSED - localparam NGPI = 11, NGPO=11; - // GPIO ports - input [(NGPI-1):0] i_gpio; - output wire [(NGPO-1):0] o_gpio; + input wire i_cpu_reset; + input wire i_sim_write; + input wire [7-1:0] i_sim_addr; + input wire [31:0] i_sim_data; + output wire o_prof_stb; + output wire [23+1:0] o_prof_addr; + output wire [31:0] o_prof_ticks; input wire i_wbu_uart_rx; output wire o_wbu_uart_tx; - // The Universal QSPI flash - output wire o_qspi_cs_n, o_qspi_sck; - output wire [3:0] o_qspi_dat; - input wire [3:0] i_qspi_dat; - output wire [1:0] o_qspi_mod; - input wire i_cpu_reset; // SD-Card declarations + // {{{ output wire o_sd_sck, o_sd_cmd; output wire [3:0] o_sd_data; // verilator lint_off UNUSED @@ -187,41 +203,56 @@ module main(i_clk, i_reset, input wire [3:0] i_sd_data; // verilator lint_on UNUSED input wire i_sd_detect; - // Make Verilator happy ... defining bus wires for lots of components - // often ends up with unused wires lying around. We'll turn off - // Ver1lator's lint warning here that checks for unused wires. + // }}} + localparam NGPI = 11, NGPO=11; + // GPIO ports + input [(NGPI-1):0] i_gpio; + output wire [(NGPO-1):0] o_gpio; + // The Universal QSPI flash + output wire o_qspi_cs_n, o_qspi_sck; + output wire [3:0] o_qspi_dat; + input wire [3:0] i_qspi_dat; + output wire [1:0] o_qspi_mod; +// }}} + // Make Verilator happy + // {{{ + // Defining bus wires for lots of components often ends up with unused + // wires lying around. We'll turn off Ver1lator's lint warning + // here that checks for unused wires. + // }}} // verilator lint_off UNUSED - - - + //////////////////////////////////////////////////////////////////////// // // Declaring interrupt lines - // + // {{{ // These declarations come from the various components values // given under the @INT..WIRE key. // - wire uarttxf_int; // uart.INT.UARTTXF.WIRE + wire zip_cpu_int; // zip.INT.ZIP.WIRE + wire sdcard_int; // sdcard.INT.SDCARD.WIRE + wire rtc_int; // rtc.INT.RTC.WIRE wire uartrxf_int; // uart.INT.UARTRXF.WIRE wire uarttx_int; // uart.INT.UARTTX.WIRE + wire uarttxf_int; // uart.INT.UARTTXF.WIRE wire uartrx_int; // uart.INT.UARTRX.WIRE - wire gpio_int; // gpio.INT.GPIO.WIRE wire w_bus_int; // buspic.INT.BUS.WIRE - wire rtc_int; // rtc.INT.RTC.WIRE - wire zip_cpu_int; // zip.INT.ZIP.WIRE - wire sdcard_int; // sdcard.INT.SDCARD.WIRE - - + wire gpio_int; // gpio.INT.GPIO.WIRE + // }}} + //////////////////////////////////////////////////////////////////////// // // Component declarations - // + // {{{ // These declarations come from the @MAIN.DEFNS keys found in the // various components comprising the design. // - // Console definitions - wire w_console_rx_stb, w_console_tx_stb, w_console_busy; - wire [6:0] w_console_rx_data, w_console_tx_data; - wire [31:0] uart_debug; - wire sd_reset; + // ZipSystem/ZipCPU connection definitions + // All we define here is a set of scope wires + wire [31:0] zip_debug; + wire zip_trigger; + wire [ZIP_INTS-1:0] zip_int_vector; + // }}} +// BUILDTIME doesnt need to include builddate.v a second time +// `include "builddate.v" // // // UART interface @@ -244,50 +275,52 @@ module main(i_clk, i_reset, wire zip_dbg_stall, zip_dbg_ack; wire [31:0] zip_dbg_data; `endif + // SD SPI definitions + // Verilator lint_off UNUSED + wire [31:0] sdspi_debug; + // Verilator lint_on UNUSED + wire w_sd_cs_n, w_sd_mosi, w_sd_miso; // This clock step is designed to match 100000000 Hz localparam [31:0] RTC_CLKSTEP = 32'h002af31d; wire rtc_ppd; + // Verilator lint_off UNUSED wire ck_pps; + // Verilator lint_on UNUSED + // Console definitions + wire w_console_rx_stb, w_console_tx_stb, w_console_busy; + wire [6:0] w_console_rx_data, w_console_tx_data; + // Verilator lint_off UNUSED + wire [31:0] uart_debug; + // Verilator lint_on UNUSED + reg [24-1:0] r_buserr_addr; + reg [31:0] r_pwrcount_data; + wire sd_reset; // Definitions for the flash debug port + // Verilator lint_off UNUSED wire flash_dbg_trigger; wire [31:0] flash_debug; -// BUILDTIME doesnt need to include builddate.v a second time -// `include "builddate.v" - reg [24-1:0] r_buserr_addr; - // ZipSystem/ZipCPU connection definitions - // All we define here is a set of scope wires - wire [31:0] zip_debug; - wire zip_trigger; - wire [ZIP_INTS-1:0] zip_int_vector; - reg [31:0] r_pwrcount_data; - wire[31:0] sdspi_debug; - + // Verilator lint_on UNUSED +// }}} + //////////////////////////////////////////////////////////////////////// // // Declaring interrupt vector wires - // + // {{{ // These declarations come from the various components having // PIC and PIC.MAX keys. // - wire [14:0] bus_int_vector; wire [14:0] sys_int_vector; wire [14:0] alt_int_vector; + wire [14:0] bus_int_vector; + // }}} + //////////////////////////////////////////////////////////////////////// // - // - // Define bus wires - // - // + // Declare bus signals + // {{{ + //////////////////////////////////////////////////////////////////////// // Bus wb - // Wishbone definitions for bus wb, component wbu_arbiter - // Verilator lint_off UNUSED - wire wb_wbu_arbiter_cyc, wb_wbu_arbiter_stb, wb_wbu_arbiter_we; - wire [22:0] wb_wbu_arbiter_addr; - wire [31:0] wb_wbu_arbiter_data; - wire [3:0] wb_wbu_arbiter_sel; - wire wb_wbu_arbiter_stall, wb_wbu_arbiter_ack, wb_wbu_arbiter_err; - wire [31:0] wb_wbu_arbiter_idata; - // Verilator lint_on UNUSED + // {{{ // Wishbone definitions for bus wb, component zip // Verilator lint_off UNUSED wire wb_zip_cyc, wb_zip_stb, wb_zip_we; @@ -297,6 +330,15 @@ module main(i_clk, i_reset, wire wb_zip_stall, wb_zip_ack, wb_zip_err; wire [31:0] wb_zip_idata; // Verilator lint_on UNUSED + // Wishbone definitions for bus wb, component wbu_arbiter + // Verilator lint_off UNUSED + wire wb_wbu_arbiter_cyc, wb_wbu_arbiter_stb, wb_wbu_arbiter_we; + wire [22:0] wb_wbu_arbiter_addr; + wire [31:0] wb_wbu_arbiter_data; + wire [3:0] wb_wbu_arbiter_sel; + wire wb_wbu_arbiter_stall, wb_wbu_arbiter_ack, wb_wbu_arbiter_err; + wire [31:0] wb_wbu_arbiter_idata; + // Verilator lint_on UNUSED // Wishbone definitions for bus wb(SIO), component buildtime // Verilator lint_off UNUSED wire wb_buildtime_cyc, wb_buildtime_stb, wb_buildtime_we; @@ -423,7 +465,9 @@ module main(i_clk, i_reset, wire wb_flash_stall, wb_flash_ack, wb_flash_err; wire [31:0] wb_flash_idata; // Verilator lint_on UNUSED + // }}} // Bus wbu + // {{{ // Wishbone definitions for bus wbu, component wbu // Verilator lint_off UNUSED wire wbu_cyc, wbu_stb, wbu_we; @@ -444,20 +488,22 @@ module main(i_clk, i_reset, // Verilator lint_on UNUSED // Wishbone definitions for bus wbu, component zip // Verilator lint_off UNUSED - wire wbu_zip_cyc, wbu_zip_stb, wbu_zip_we; - wire [23:0] wbu_zip_addr; - wire [31:0] wbu_zip_data; - wire [3:0] wbu_zip_sel; - wire wbu_zip_stall, wbu_zip_ack, wbu_zip_err; - wire [31:0] wbu_zip_idata; + wire zip_dbg_cyc, zip_dbg_stb, zip_dbg_we; + wire [23:0] zip_dbg_addr; + wire [31:0] zip_dbg_data; + wire [3:0] zip_dbg_sel; + wire zip_dbg_stall, zip_dbg_ack, zip_dbg_err; + wire [31:0] zip_dbg_idata; // Verilator lint_on UNUSED - - // - // Peripheral address decoding + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// // + // Peripheral address decoding, bus handling + // {{{ // // BUS-LOGIC for wb - // + // {{{ // // wb Bus logic to handle SINGLE slaves // @@ -573,44 +619,44 @@ module main(i_clk, i_reset, wb_xbar( .i_clk(i_clk), .i_reset(i_reset), .i_mcyc({ - wb_zip_cyc, - wb_wbu_arbiter_cyc + wb_wbu_arbiter_cyc, + wb_zip_cyc }), .i_mstb({ - wb_zip_stb, - wb_wbu_arbiter_stb + wb_wbu_arbiter_stb, + wb_zip_stb }), .i_mwe({ - wb_zip_we, - wb_wbu_arbiter_we + wb_wbu_arbiter_we, + wb_zip_we }), .i_maddr({ - wb_zip_addr, - wb_wbu_arbiter_addr + wb_wbu_arbiter_addr, + wb_zip_addr }), .i_mdata({ - wb_zip_data, - wb_wbu_arbiter_data + wb_wbu_arbiter_data, + wb_zip_data }), .i_msel({ - wb_zip_sel, - wb_wbu_arbiter_sel + wb_wbu_arbiter_sel, + wb_zip_sel }), .o_mstall({ - wb_zip_stall, - wb_wbu_arbiter_stall + wb_wbu_arbiter_stall, + wb_zip_stall }), .o_mack({ - wb_zip_ack, - wb_wbu_arbiter_ack + wb_wbu_arbiter_ack, + wb_zip_ack }), .o_mdata({ - wb_zip_idata, - wb_wbu_arbiter_idata + wb_wbu_arbiter_idata, + wb_zip_idata }), .o_merr({ - wb_zip_err, - wb_wbu_arbiter_err + wb_wbu_arbiter_err, + wb_zip_err }), // Slave connections .o_scyc({ @@ -705,9 +751,11 @@ module main(i_clk, i_reset, }) ); + // End of bus logic for wb + // }}} // // BUS-LOGIC for wbu - // + // {{{ // // No class SINGLE peripherals on the "wbu" bus // @@ -717,7 +765,7 @@ module main(i_clk, i_reset, // // info: @ERROR.WIRE for wbu_arbiter matches the buses error name, wbu_wbu_arbiter_err - assign wbu_zip_err= 1'b0; + assign zip_dbg_err= 1'b0; // // Connect the wbu bus components together using the wbxbar() // @@ -773,50 +821,54 @@ module main(i_clk, i_reset, }), // Slave connections .o_scyc({ - wbu_zip_cyc, + zip_dbg_cyc, wbu_wbu_arbiter_cyc }), .o_sstb({ - wbu_zip_stb, + zip_dbg_stb, wbu_wbu_arbiter_stb }), .o_swe({ - wbu_zip_we, + zip_dbg_we, wbu_wbu_arbiter_we }), .o_saddr({ - wbu_zip_addr, + zip_dbg_addr, wbu_wbu_arbiter_addr }), .o_sdata({ - wbu_zip_data, + zip_dbg_data, wbu_wbu_arbiter_data }), .o_ssel({ - wbu_zip_sel, + zip_dbg_sel, wbu_wbu_arbiter_sel }), .i_sstall({ - wbu_zip_stall, + zip_dbg_stall, wbu_wbu_arbiter_stall }), .i_sack({ - wbu_zip_ack, + zip_dbg_ack, wbu_wbu_arbiter_ack }), .i_sdata({ - wbu_zip_idata, + zip_dbg_idata, wbu_wbu_arbiter_idata }), .i_serr({ - wbu_zip_err, + zip_dbg_err, wbu_wbu_arbiter_err }) ); + // End of bus logic for wbu + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// // // Declare the interrupt busses - // + // {{{ // Interrupt busses are defined by anything with a @PIC tag. // The @PIC.BUS tag defines the name of the wire bus below, // while the @PIC.MAX tag determines the size of the bus width. @@ -828,33 +880,33 @@ module main(i_clk, i_reset, // exists, then your interrupt will be assigned to the position given // by the ID# in that tag. // - assign bus_int_vector = { - 1'b0, - 1'b0, - 1'b0, + assign sys_int_vector = { 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, + uarttxf_int, + uartrxf_int, + sdcard_int, + w_bus_int, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, - sdcard_int, - gpio_int + 1'b0 }; - assign sys_int_vector = { + assign alt_int_vector = { 1'b0, 1'b0, 1'b0, 1'b0, + uartrx_int, + uarttx_int, + rtc_int, + 1'b0, 1'b0, - sdcard_int, - uartrxf_int, - uarttxf_int, - w_bus_int, 1'b0, 1'b0, 1'b0, @@ -862,14 +914,11 @@ module main(i_clk, i_reset, 1'b0, 1'b0 }; - assign alt_int_vector = { + assign bus_int_vector = { 1'b0, 1'b0, 1'b0, 1'b0, - rtc_int, - uartrx_int, - uarttx_int, 1'b0, 1'b0, 1'b0, @@ -877,10 +926,19 @@ module main(i_clk, i_reset, 1'b0, 1'b0, 1'b0, - 1'b0 + 1'b0, + 1'b0, + gpio_int, + sdcard_int }; - - + // }}} + //////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////// + // + // @MAIN.INSERT and @MAIN.ALT + // {{{ + //////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////// // // // Now we turn to defining all of the parts and pieces of what @@ -895,101 +953,161 @@ module main(i_clk, i_reset, // or making sure all of the various interrupt wires are set to // zero if the component is not included. // - assign wb_wbu_arbiter_cyc = wbu_wbu_arbiter_cyc; - assign wb_wbu_arbiter_stb = wbu_wbu_arbiter_stb; - assign wb_wbu_arbiter_we = wbu_wbu_arbiter_we; - assign wb_wbu_arbiter_addr = wbu_wbu_arbiter_addr[23-1:0]; - assign wb_wbu_arbiter_data = wbu_wbu_arbiter_data; - assign wb_wbu_arbiter_sel = wbu_wbu_arbiter_sel; +`ifdef INCLUDE_ZIPCPU + // {{{ + //////////////////////////////////////////////////////////////////////// // - assign wbu_wbu_arbiter_stall = wb_wbu_arbiter_stall; - assign wbu_wbu_arbiter_ack = wb_wbu_arbiter_ack; - assign wbu_wbu_arbiter_idata = wb_wbu_arbiter_idata; - assign wbu_wbu_arbiter_err = wb_wbu_arbiter_err; + // The ZipCPU/ZipSystem BUS master + // {{{ + // + assign zip_int_vector = { alt_int_vector[14:8], sys_int_vector[14:6] }; + zipsystem #( + // {{{ + .RESET_ADDRESS(RESET_ADDRESS), + .ADDRESS_WIDTH(ZIP_ADDRESS_WIDTH + $clog2(32/8)), + .BUS_WIDTH(32), + .OPT_LGICACHE(12), + .OPT_LGDCACHE(12), + .START_HALTED(ZIP_START_HALTED), + .RESET_DURATION(20), + .OPT_PIPELINED(1), + .OPT_PROFILER(1), + .EXTERNAL_INTERRUPTS(ZIP_INTS) + // }}} + ) swic( + // {{{ + .i_clk(i_clk), .i_reset((i_reset)||(i_cpu_reset)), + .i_sim_write(i_sim_write), + .i_sim_addr(i_sim_addr), + .i_sim_data(i_sim_data), + // Zippys wishbone interface + // [o|i]_wb_* + .o_wb_cyc(wb_zip_cyc), .o_wb_stb(wb_zip_stb), .o_wb_we(wb_zip_we), + .o_wb_addr(wb_zip_addr[23-1:0]), + .o_wb_data(wb_zip_data), // 32 bits wide + .o_wb_sel(wb_zip_sel), // 32/8 bits wide + .i_wb_stall(wb_zip_stall), .i_wb_ack(wb_zip_ack), .i_wb_data(wb_zip_idata), .i_wb_err(wb_zip_err), + .i_ext_int(zip_int_vector), .o_ext_int(zip_cpu_int), + // Debug wishbone interface + // [o|i]_db g_* + .i_dbg_cyc(zip_dbg_cyc), .i_dbg_stb(zip_dbg_stb), .i_dbg_we(zip_dbg_we), + .i_dbg_addr(zip_dbg_addr[7-1:0]), + .i_dbg_data(zip_dbg_data), // 32 bits wide + .i_dbg_sel(zip_dbg_sel), // 32/8 bits wide + .o_dbg_stall(zip_dbg_stall),.o_dbg_ack(zip_dbg_ack), .o_dbg_data(zip_dbg_idata), + .o_cpu_debug(zip_debug), + .o_prof_stb(o_prof_stb), + .o_prof_addr(o_prof_addr), + .o_prof_ticks(o_prof_ticks) + // }}} + ); -`ifdef BUSCONSOLE_ACCESS - wbconsole #(.LGFLEN(6)) console(i_clk, 1'b0, - wb_uart_cyc, wb_uart_stb, wb_uart_we, - wb_uart_addr[2-1:0], - wb_uart_data, // 32 bits wide - wb_uart_sel, // 32/8 bits wide - wb_uart_stall, wb_uart_ack, wb_uart_idata, - w_console_tx_stb, w_console_tx_data, w_console_busy, - w_console_rx_stb, w_console_rx_data, - uartrx_int, uarttx_int, uartrxf_int, uarttxf_int, - uart_debug); -`else // BUSCONSOLE_ACCESS + assign zip_trigger = zip_debug[31]; + // Verilator lint_off UNUSED + wire unused_zip; + assign unused_zip = &{ 1'b0, sys_int_vector[5:0], + alt_int_vector[7:0] }; + // Verilator lint_on UNUSED + // }}} + // }}} +`else // INCLUDE_ZIPCPU + // {{{ + // Null bus master + // {{{ + // }}} + // Null bus slave + // {{{ // - // In the case that there is no wb_uart peripheral - // responding on the wb bus - assign wb_uart_ack = 1'b0; - assign wb_uart_err = (wb_uart_stb); - assign wb_uart_stall = 0; - assign wb_uart_idata = 0; + // In the case that there is no zip_dbg peripheral + // responding on the wbu bus + assign zip_dbg_ack = 1'b0; + assign zip_dbg_err = (zip_dbg_stb); + assign zip_dbg_stall = 0; + assign zip_dbg_idata = 0; - assign uarttxf_int = 1'b0; // uart.INT.UARTTXF.WIRE - assign uartrxf_int = 1'b0; // uart.INT.UARTRXF.WIRE - assign uarttx_int = 1'b0; // uart.INT.UARTTX.WIRE - assign uartrx_int = 1'b0; // uart.INT.UARTRX.WIRE -`endif // BUSCONSOLE_ACCESS + // }}} + // Null interrupt definitions + // {{{ + assign zip_cpu_int = 1'b0; // zip.INT.ZIP.WIRE + // }}} + // }}} +`endif // INCLUDE_ZIPCPU + + assign wb_buildtime_idata = `BUILDTIME; + assign wb_buildtime_ack = wb_buildtime_stb; + assign wb_buildtime_stall = 1'b0; +`ifdef BKRAM_ACCESS + // {{{ + memdev #( + .LGMEMSZ(20), .EXTRACLOCK(1), + .DW(32) + ) bkrami( + i_clk, i_reset, + wb_bkram_cyc, wb_bkram_stb, wb_bkram_we, + wb_bkram_addr[18-1:0], + wb_bkram_data, // 32 bits wide + wb_bkram_sel, // 32/8 bits wide + wb_bkram_stall, wb_bkram_ack, wb_bkram_idata + ); + // }}} +`else // BKRAM_ACCESS + // {{{ + // Null bus slave + // {{{ -`ifdef GPIO_ACCESS - // - // GPIO - // - // This interface should allow us to control up to 16 GPIO inputs, and - // another 16 GPIO outputs. The interrupt trips when any of the inputs - // changes. (Sorry, which input isn't (yet) selectable.) // - localparam INITIAL_GPIO = 11'h0; - wbgpio #(NGPI, NGPO, INITIAL_GPIO) - gpioi(i_clk, wb_gpio_cyc, wb_gpio_stb, wb_gpio_we, - wb_gpio_data, // 32 bits wide - wb_gpio_sel, // 32/8 bits wide - wb_gpio_stall, wb_gpio_ack, wb_gpio_idata, - i_gpio, o_gpio, gpio_int); + // In the case that there is no wb_bkram peripheral + // responding on the wb bus + assign wb_bkram_ack = 1'b0; + assign wb_bkram_err = (wb_bkram_stb); + assign wb_bkram_stall = 0; + assign wb_bkram_idata = 0; - assign sd_reset = o_gpio[0]; -`else // GPIO_ACCESS - assign gpio_int = 1'b0; // gpio.INT.GPIO.WIRE -`endif // GPIO_ACCESS + // }}} + // }}} +`endif // BKRAM_ACCESS `ifdef VERSION_ACCESS + // {{{ assign wb_version_idata = `DATESTAMP; assign wb_version_ack = wb_version_stb; assign wb_version_stall = 1'b0; + // }}} `else // VERSION_ACCESS + // {{{ + // }}} `endif // VERSION_ACCESS -`ifdef FLASHCFG_ACCESS - // The Flash control interface is defined by the flash instantiation - // hence we don't need to do anything to define it here. -`else // FLASHCFG_ACCESS - - // - // In the case that there is no wb_flashcfg peripheral - // responding on the wb bus - assign wb_flashcfg_ack = 1'b0; - assign wb_flashcfg_err = (wb_flashcfg_stb); - assign wb_flashcfg_stall = 0; - assign wb_flashcfg_idata = 0; - -`endif // FLASHCFG_ACCESS - `ifdef WBUBUS_MASTER + // {{{ // The Host USB interface, to be used by the WB-UART bus - rxuartlite #(.TIMER_BITS(DBGBUSBITS), - .CLOCKS_PER_BAUD(BUSUART[DBGBUSBITS-1:0])) - rcv(i_clk, i_wbu_uart_rx, - wbu_rx_stb, wbu_rx_data); - txuartlite #(.TIMING_BITS(DBGBUSBITS[4:0]), - .CLOCKS_PER_BAUD(BUSUART[DBGBUSBITS-1:0])) - txv(i_clk, - wbu_tx_stb, - wbu_tx_data, - o_wbu_uart_tx, - wbu_tx_busy); + rxuartlite #( + // {{{ + .TIMER_BITS(DBGBUSBITS), + .CLOCKS_PER_BAUD(BUSUART[DBGBUSBITS-1:0]) + // }}} + ) rcv( + // {{{ + i_clk, i_wbu_uart_rx, + wbu_rx_stb, wbu_rx_data + // }}} + ); + + txuartlite #( + // {{{ + .TIMING_BITS(DBGBUSBITS[4:0]), + .CLOCKS_PER_BAUD(BUSUART[DBGBUSBITS-1:0]) + // }}} + ) txv( + // {{{ + i_clk, + wbu_tx_stb, + wbu_tx_data, + o_wbu_uart_tx, + wbu_tx_busy + // }}} + ); `ifdef INCLUDE_ZIPCPU `else @@ -1005,10 +1123,11 @@ module main(i_clk, i_reset, wire [31:0] wbu_tmp_addr; // Verilator lint_on UNUSED wbuconsole #(.LGWATCHDOG(DBGBUSWATCHDOG)) - genbus(i_clk, wbu_rx_stb, wbu_rx_data, + genbus(i_clk, 1'b0, wbu_rx_stb, wbu_rx_data, wbu_cyc, wbu_stb, wbu_we, wbu_tmp_addr, wbu_data, wbu_stall, wbu_ack, - wbu_err, wbu_idata, + wbu_idata, + wbu_err, w_bus_int, wbu_tx_stb, wbu_tx_data, wbu_tx_busy, // @@ -1018,37 +1137,97 @@ module main(i_clk, i_reset, wbubus_dbg[0]); assign wbu_sel = 4'hf; assign wbu_addr = wbu_tmp_addr[(24-1):0]; + // }}} `else // WBUBUS_MASTER + // {{{ + // Null bus master + // {{{ + // }}} + // }}} `endif // WBUBUS_MASTER -`ifdef BUSPIC_ACCESS +`ifdef SDSPI_ACCESS + // {{{ + //////////////////////////////////////////////////////////////////////// // - // The BUS Interrupt controller + // SD Card SPI handling + // {{{ + //////////////////////////////////////////////////////////////////////// // - icontrol #(15) buspici(i_clk, 1'b0, - wb_buspic_cyc, wb_buspic_stb, wb_buspic_we, - wb_buspic_data, // 32 bits wide - wb_buspic_sel, // 32/8 bits wide - wb_buspic_stall, wb_buspic_ack, wb_buspic_idata, - bus_int_vector, w_bus_int); -`else // BUSPIC_ACCESS - assign w_bus_int = 1'b0; // buspic.INT.BUS.WIRE -`endif // BUSPIC_ACCESS + // + + sdspi + sdcardi( + // {{{ + i_clk, sd_reset, + wb_sdcard_cyc, wb_sdcard_stb, wb_sdcard_we, + wb_sdcard_addr[2-1:0], + wb_sdcard_data, // 32 bits wide + wb_sdcard_sel, // 32/8 bits wide + wb_sdcard_stall, wb_sdcard_ack, wb_sdcard_idata, + w_sd_cs_n, o_sd_sck, w_sd_mosi, w_sd_miso, i_sd_detect, + sdcard_int, 1'b1, sdspi_debug + // }}} + ); + + assign w_sd_miso = i_sd_data[0]; + assign o_sd_data = { w_sd_cs_n, 3'b111 }; + assign o_sd_cmd = w_sd_mosi; + // }}} + // }}} +`else // SDSPI_ACCESS + // {{{ + assign o_sd_sck = 1'b1; + assign o_sd_cmd = 1'b1; + assign o_sd_data = 4'hf; + // Null bus slave + // {{{ + + // + // In the case that there is no wb_sdcard peripheral + // responding on the wb bus + assign wb_sdcard_ack = 1'b0; + assign wb_sdcard_err = (wb_sdcard_stb); + assign wb_sdcard_stall = 0; + assign wb_sdcard_idata = 0; + + // }}} + // Null interrupt definitions + // {{{ + assign sdcard_int = 1'b0; // sdcard.INT.SDCARD.WIRE + // }}} + // }}} +`endif // SDSPI_ACCESS `ifdef RTC_ACCESS - rtclight #(.DEFAULT_SPEED(32'h2af31d), + // {{{ + rtclight #( + // {{{ + .DEFAULT_SPEED(RTC_CLKSTEP), .OPT_TIMER(1'b1), .OPT_STOPWATCH(1'b1), .OPT_ALARM(1'b0), - .OPT_FIXED_SPEED(1'b1)) - thertc(i_clk, i_reset, wb_rtc_cyc, wb_rtc_stb, wb_rtc_we, - wb_rtc_addr[3-1:0], - wb_rtc_data, // 32 bits wide - wb_rtc_sel, // 32/8 bits wide - wb_rtc_stall, wb_rtc_ack, wb_rtc_idata, - rtc_int, ck_pps, rtc_ppd); + .OPT_FIXED_SPEED(1'b1) + // }}} + ) thertc( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + .i_wb_cyc(wb_rtc_cyc), .i_wb_stb(wb_rtc_stb), .i_wb_we(wb_rtc_we), + .i_wb_addr(wb_rtc_addr[3-1:0]), + .i_wb_data(wb_rtc_data), // 32 bits wide + .i_wb_sel(wb_rtc_sel), // 32/8 bits wide + .o_wb_stall(wb_rtc_stall),.o_wb_ack(wb_rtc_ack), .o_wb_data(wb_rtc_idata), + .o_interrupt(rtc_int), + .o_pps(ck_pps), + .o_ppd(rtc_ppd) + // }}} + ); + // }}} `else // RTC_ACCESS + // {{{ assign ck_pps = 1'b0; + // Null bus slave + // {{{ // // In the case that there is no wb_rtc peripheral @@ -1058,76 +1237,83 @@ module main(i_clk, i_reset, assign wb_rtc_stall = 0; assign wb_rtc_idata = 0; + // }}} + // Null interrupt definitions + // {{{ assign rtc_int = 1'b0; // rtc.INT.RTC.WIRE + // }}} + // }}} `endif // RTC_ACCESS -`ifdef BKRAM_ACCESS - memdev #(.LGMEMSZ(20), .EXTRACLOCK(1)) - bkrami(i_clk, i_reset, - wb_bkram_cyc, wb_bkram_stb, wb_bkram_we, - wb_bkram_addr[18-1:0], - wb_bkram_data, // 32 bits wide - wb_bkram_sel, // 32/8 bits wide - wb_bkram_stall, wb_bkram_ack, wb_bkram_idata); -`else // BKRAM_ACCESS +`ifdef BUSCONSOLE_ACCESS + // {{{ + wbconsole #( + .LGFLEN(6) + ) console( + // {{{ + .i_clk(i_clk), .i_reset(1'b0), + .i_wb_cyc(wb_uart_cyc), .i_wb_stb(wb_uart_stb), .i_wb_we(wb_uart_we), + .i_wb_addr(wb_uart_addr[2-1:0]), + .i_wb_data(wb_uart_data), // 32 bits wide + .i_wb_sel(wb_uart_sel), // 32/8 bits wide + .o_wb_stall(wb_uart_stall),.o_wb_ack(wb_uart_ack), .o_wb_data(wb_uart_idata), + .o_uart_stb(w_console_tx_stb), .o_uart_data(w_console_tx_data), + .i_uart_busy(w_console_busy), + .i_uart_stb(w_console_rx_stb), .i_uart_data(w_console_rx_data), + .o_uart_rx_int(uartrx_int), .o_uart_tx_int(uarttx_int), + .o_uart_rxfifo_int(uartrxf_int), + .o_uart_txfifo_int(uarttxf_int), + .o_debug(uart_debug) + // }}} + ); + // }}} +`else // BUSCONSOLE_ACCESS + // {{{ + // Null bus slave + // {{{ // - // In the case that there is no wb_bkram peripheral + // In the case that there is no wb_uart peripheral // responding on the wb bus - assign wb_bkram_ack = 1'b0; - assign wb_bkram_err = (wb_bkram_stb); - assign wb_bkram_stall = 0; - assign wb_bkram_idata = 0; - -`endif // BKRAM_ACCESS + assign wb_uart_ack = 1'b0; + assign wb_uart_err = (wb_uart_stb); + assign wb_uart_stall = 0; + assign wb_uart_idata = 0; -`ifdef FLASH_ACCESS - qflexpress #(.LGFLASHSZ(24), .OPT_CLKDIV(1), - .OPT_ENDIANSWAP(0), - .NDUMMY(6), .RDDELAY(0), - .OPT_STARTUP_FILE("spansion.hex"), -`ifdef FLASHCFG_ACCESS - .OPT_CFG(1'b1) -`else - .OPT_CFG(1'b0) -`endif - ) - flashi(i_clk, i_reset, - // Primary memory reading inputs - wb_flash_cyc, wb_flash_stb, wb_flash_we, - wb_flash_addr[22-1:0], - wb_flash_data, // 32 bits wide - wb_flash_sel, // 32/8 bits wide - wb_flash_stall, wb_flash_ack, wb_flash_idata, - // Configuration bus ports - wb_flashcfg_cyc, wb_flashcfg_stb, wb_flashcfg_we, - wb_flashcfg_data, // 32 bits wide - wb_flashcfg_sel, // 32/8 bits wide - wb_flashcfg_stall, wb_flashcfg_ack, wb_flashcfg_idata, - o_qspi_sck, o_qspi_cs_n, o_qspi_mod, o_qspi_dat, i_qspi_dat, - flash_dbg_trigger, flash_debug); -`else // FLASH_ACCESS - assign o_qspi_sck = 1'b1; - assign o_qspi_cs_n = 1'b1; - assign o_qspi_mod = 2'b01; - assign o_qspi_dat = 4'b1111; - // Verilator lint_off UNUSED - wire flash_unused = &{ 1'b0, i_qspi_dat }; - // Verilator lint_on UNUSED + // }}} + // Null interrupt definitions + // {{{ + assign uartrxf_int = 1'b0; // uart.INT.UARTRXF.WIRE + assign uarttx_int = 1'b0; // uart.INT.UARTTX.WIRE + assign uarttxf_int = 1'b0; // uart.INT.UARTTXF.WIRE + assign uartrx_int = 1'b0; // uart.INT.UARTRX.WIRE + // }}} + // }}} +`endif // BUSCONSOLE_ACCESS +`ifdef BUSPIC_ACCESS + // {{{ // - // In the case that there is no wb_flash peripheral - // responding on the wb bus - assign wb_flash_ack = 1'b0; - assign wb_flash_err = (wb_flash_stb); - assign wb_flash_stall = 0; - assign wb_flash_idata = 0; - -`endif // FLASH_ACCESS + // The BUS Interrupt controller + // + icontrol #(15) + buspici( + i_clk, 1'b0, + wb_buspic_cyc, wb_buspic_stb, wb_buspic_we, + wb_buspic_data, // 32 bits wide + wb_buspic_sel, // 32/8 bits wide + wb_buspic_stall, wb_buspic_ack, wb_buspic_idata, + bus_int_vector, w_bus_int); + // }}} +`else // BUSPIC_ACCESS + // {{{ + // Null interrupt definitions + // {{{ + assign w_bus_int = 1'b0; // buspic.INT.BUS.WIRE + // }}} + // }}} +`endif // BUSPIC_ACCESS - assign wb_buildtime_idata = `BUILDTIME; - assign wb_buildtime_ack = wb_buildtime_stb; - assign wb_buildtime_stall = 1'b0; always @(posedge i_clk) if (wb_zip_err) begin @@ -1142,49 +1328,8 @@ module main(i_clk, i_reset, assign wb_buserr_ack = wb_buserr_stb; assign wb_buserr_idata = { {(30-24){1'b0}}, r_buserr_addr, 2'b00 }; -`ifdef INCLUDE_ZIPCPU - // - // - // The ZipCPU/ZipSystem BUS master - // - // - assign zip_int_vector = { alt_int_vector[14:8], sys_int_vector[14:6] }; - zipsystem #(.RESET_ADDRESS(RESET_ADDRESS), - .ADDRESS_WIDTH(ZIP_ADDRESS_WIDTH), - .LGICACHE(12),.LGDCACHE(12), - .START_HALTED(ZIP_START_HALTED), - .RESET_DURATION(20), - .EXTERNAL_INTERRUPTS(ZIP_INTS)) - swic(i_clk, (i_reset)||(i_cpu_reset), - // Zippys wishbone interface - wb_zip_cyc, wb_zip_stb, wb_zip_we, - wb_zip_addr[23-1:0], - wb_zip_data, // 32 bits wide - wb_zip_sel, // 32/8 bits wide - wb_zip_stall, wb_zip_ack, wb_zip_idata,wb_zip_err, - zip_int_vector, zip_cpu_int, - // Debug wishbone interface - wbu_zip_cyc, wbu_zip_stb, wbu_zip_we, - wbu_zip_addr[1-1:0], - wbu_zip_data, // 32 bits wide - wbu_zip_sel, // 32/8 bits wide - wbu_zip_stall, wbu_zip_ack, wbu_zip_idata, - zip_debug); - assign zip_trigger = zip_debug[31]; -`else // INCLUDE_ZIPCPU - - // - // In the case that there is no wbu_zip peripheral - // responding on the wbu bus - assign wbu_zip_ack = 1'b0; - assign wbu_zip_err = (wbu_zip_stb); - assign wbu_zip_stall = 0; - assign wbu_zip_idata = 0; - - assign zip_cpu_int = 1'b0; // zip.INT.ZIP.WIRE -`endif // INCLUDE_ZIPCPU - `ifdef PWRCOUNT_ACCESS + // {{{ initial r_pwrcount_data = 32'h0; always @(posedge i_clk) if (r_pwrcount_data[31]) @@ -1195,10 +1340,26 @@ module main(i_clk, i_reset, assign wb_pwrcount_stall = 1'b0; assign wb_pwrcount_ack = wb_pwrcount_stb; assign wb_pwrcount_idata = r_pwrcount_data; + // }}} `else // PWRCOUNT_ACCESS + // {{{ + // }}} `endif // PWRCOUNT_ACCESS + assign wb_wbu_arbiter_cyc = wbu_wbu_arbiter_cyc; + assign wb_wbu_arbiter_stb = wbu_wbu_arbiter_stb; + assign wb_wbu_arbiter_we = wbu_wbu_arbiter_we; + assign wb_wbu_arbiter_addr = wbu_wbu_arbiter_addr[23-1:0]; + assign wb_wbu_arbiter_data = wbu_wbu_arbiter_data; + assign wb_wbu_arbiter_sel = wbu_wbu_arbiter_sel; + // + assign wbu_wbu_arbiter_stall = wb_wbu_arbiter_stall; + assign wbu_wbu_arbiter_ack = wb_wbu_arbiter_ack; + assign wbu_wbu_arbiter_idata = wb_wbu_arbiter_idata; + assign wbu_wbu_arbiter_err = wb_wbu_arbiter_err; + `ifdef RTCDATE_ACCESS + // {{{ // // The Calendar DATE // @@ -1208,41 +1369,131 @@ module main(i_clk, i_reset, wb_rtcdate_data, // 32 bits wide wb_rtcdate_sel, // 32/8 bits wide wb_rtcdate_stall, wb_rtcdate_ack, wb_rtcdate_idata); + // }}} `else // RTCDATE_ACCESS + // {{{ + // }}} `endif // RTCDATE_ACCESS -`ifdef SDSPI_ACCESS - // SPI mapping - wire w_sd_cs_n, w_sd_mosi, w_sd_miso; +`ifdef GPIO_ACCESS + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // GPIO + // {{{ + // This interface should allow us to control up to 16 GPIO inputs, + // and another 16 GPIO outputs. The interrupt trips when any of + // the inputs changes. (Sorry, which input isn't (yet) selectable.) + // + localparam [NGPO-1:0] INITIAL_GPIO = 11'h0; - sdspi sdcardi(i_clk, sd_reset, - wb_sdcard_cyc, wb_sdcard_stb, wb_sdcard_we, - wb_sdcard_addr[2-1:0], - wb_sdcard_data, // 32 bits wide - wb_sdcard_sel, // 32/8 bits wide - wb_sdcard_stall, wb_sdcard_ack, wb_sdcard_idata, - w_sd_cs_n, o_sd_sck, w_sd_mosi, w_sd_miso, i_sd_detect, - sdcard_int, 1'b1, sdspi_debug); + wbgpio #( + .NIN(NGPI), .NOUT(NGPO), .DEFAULT(INITIAL_GPIO) + ) gpioi( + // {{{ + i_clk, wb_gpio_cyc, wb_gpio_stb, wb_gpio_we, + wb_gpio_data, // 32 bits wide + wb_gpio_sel, // 32/8 bits wide + wb_gpio_stall, wb_gpio_ack, wb_gpio_idata, + i_gpio, o_gpio, gpio_int + // }}} + ); - assign w_sd_miso = i_sd_data[0]; - assign o_sd_data = { w_sd_cs_n, 3'b111 }; - assign o_sd_cmd = w_sd_mosi; -`else // SDSPI_ACCESS - assign o_sd_sck = 1'b1; - assign o_sd_cmd = 1'b1; - assign o_sd_data = 4'hf; + assign sd_reset = o_gpio[0]; + // }}} + // }}} +`else // GPIO_ACCESS + // {{{ + // Null interrupt definitions + // {{{ + assign gpio_int = 1'b0; // gpio.INT.GPIO.WIRE + // }}} + // }}} +`endif // GPIO_ACCESS + +`ifdef FLASHCFG_ACCESS + // {{{ + // The Flash control interface is defined by the flash instantiation + // hence we don't need to do anything to define it here. + // }}} +`else // FLASHCFG_ACCESS + // {{{ + // Null bus slave + // {{{ // - // In the case that there is no wb_sdcard peripheral + // In the case that there is no wb_flashcfg peripheral // responding on the wb bus - assign wb_sdcard_ack = 1'b0; - assign wb_sdcard_err = (wb_sdcard_stb); - assign wb_sdcard_stall = 0; - assign wb_sdcard_idata = 0; + assign wb_flashcfg_ack = 1'b0; + assign wb_flashcfg_err = (wb_flashcfg_stb); + assign wb_flashcfg_stall = 0; + assign wb_flashcfg_idata = 0; - assign sdcard_int = 1'b0; // sdcard.INT.SDCARD.WIRE -`endif // SDSPI_ACCESS + // }}} + // }}} +`endif // FLASHCFG_ACCESS + +`ifdef FLASH_ACCESS + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // Flash controller + // {{{ + qflexpress #( + // {{{ + .LGFLASHSZ(24), .OPT_CLKDIV(1), + .OPT_ENDIANSWAP(0), + .NDUMMY(6), .RDDELAY(0), + .OPT_STARTUP_FILE("spansion.hex"), +`ifdef FLASHCFG_ACCESS + .OPT_CFG(1'b1) +`else + .OPT_CFG(1'b0) +`endif + // }}} + ) flashi( + // {{{ + i_clk, i_reset, + // Primary memory reading inputs + wb_flash_cyc, wb_flash_stb, wb_flash_we, + wb_flash_addr[22-1:0], + wb_flash_data, // 32 bits wide + wb_flash_sel, // 32/8 bits wide + wb_flash_stall, wb_flash_ack, wb_flash_idata, + // Configuration bus ports + wb_flashcfg_cyc, wb_flashcfg_stb, wb_flashcfg_we, + wb_flashcfg_data, // 32 bits wide + wb_flashcfg_sel, // 32/8 bits wide + wb_flashcfg_stall, wb_flashcfg_ack, wb_flashcfg_idata, + o_qspi_sck, o_qspi_cs_n, o_qspi_mod, o_qspi_dat, i_qspi_dat, + flash_dbg_trigger, flash_debug + // }}} + ); + // }}} + // }}} +`else // FLASH_ACCESS + // {{{ + assign o_qspi_sck = 1'b1; + assign o_qspi_cs_n = 1'b1; + assign o_qspi_mod = 2'b01; + assign o_qspi_dat = 4'b1111; + // Verilator lint_off UNUSED + wire flash_unused = &{ 1'b0, i_qspi_dat }; + // Verilator lint_on UNUSED + // Null bus slave + // {{{ + // + // In the case that there is no wb_flash peripheral + // responding on the wb bus + assign wb_flash_ack = 1'b0; + assign wb_flash_err = (wb_flash_stb); + assign wb_flash_stall = 0; + assign wb_flash_idata = 0; + // }}} + // }}} +`endif // FLASH_ACCESS + // }}} endmodule // main.v diff --git a/rtl/make.inc b/rtl/make.inc index 635a5f3..9a51647 100644 --- a/rtl/make.inc +++ b/rtl/make.inc @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: ./rtl.make.inc -## +## {{{ ## Project: ZBasic, a generic toplevel implementation using the full ZipCPU ## ## DO NOT EDIT THIS FILE! @@ -14,11 +14,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -31,33 +31,33 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## -CONSOLED := wbuart -CONSOLE := $(addprefix $(CONSOLED)/,txuartlite.v rxuartlite.v ufifo.v) -GPIO := wbgpio.v - -FLASH := qflexpress.v +## }}} +ZIPCPUD := cpu +ZIPCPU := $(addprefix $(ZIPCPUD)/,zipsystem.v zipcore.v zipwb.v cpuops.v pfcache.v pipemem.v pfcache.v idecode.v wbpriarbiter.v zipsystem.v zipcounter.v zipjiffies.v ziptimer.v wbdmac.v icontrol.v wbwatchdog.v busdelay.v) +BKRAM := memdev.v WBUBUSD := wbubus WBUBUS := $(addprefix $(WBUBUSD)/,wbuconsole.v wbufifo.v wbucompactlines.v wbucompress.v wbudecompress.v wbudeword.v wbuexec.v wbuidleint.v wbuinput.v wbuoutput.v wbureadcw.v wbusixchar.v wbutohex.v wbconsole.v) -BUSPICD := cpu -BUSPIC := $(addprefix $(BUSPICD)/,icontrol.v) +SDSPID := sdspi +SDSPI := $(addprefix $(SDSPID)/,sdspi.v llsdspi.v spicmd.v spirxdata.v spitxdata.v) RTCD := rtc RTC := $(addprefix $(RTCD)/,rtclight.v rtcbare.v rtcalarm.v rtcstopwatch.v rtctimer.v) -BKRAM := memdev.v - -ZIPCPUD := cpu -ZIPCPU := $(addprefix $(ZIPCPUD)/,zipcpu.v cpuops.v pfcache.v pipemem.v pfcache.v idecode.v wbpriarbiter.v zipsystem.v zipcounter.v zipjiffies.v ziptimer.v wbdmac.v icontrol.v wbwatchdog.v busdelay.v cpudefs.v) +CONSOLED := wbuart +CONSOLE := $(addprefix $(CONSOLED)/,txuartlite.v rxuartlite.v ufifo.v) +BUSPICD := cpu +BUSPIC := $(addprefix $(BUSPICD)/,icontrol.v) RTCDATED := rtc RTCDATE := $(addprefix $(RTCDATED)/,rtcdate.v) -SDSPID := sdspi -SDSPI := $(addprefix $(SDSPID)/,sdspi.v llsdspi.v spicmd.v spirxdata.v spitxdata.v) -VFLIST := main.v $(CONSOLE) $(GPIO) $(FLASH) $(WBUBUS) $(BUSPIC) $(RTC) $(BKRAM) $(ZIPCPU) $(RTCDATE) $(SDSPI) -AUTOVDIRS := -y wbuart -y wbubus -y cpu -y rtc -y sdspi +GPIO := wbgpio.v + +FLASH := qflexpress.v + +VFLIST := main.v $(ZIPCPU) $(BKRAM) $(WBUBUS) $(SDSPI) $(RTC) $(CONSOLE) $(BUSPIC) $(RTCDATE) $(GPIO) $(FLASH) +AUTOVDIRS := -y cpu -y wbubus -y sdspi -y rtc -y wbuart diff --git a/rtl/memdev.v b/rtl/memdev.v index dc0d62f..1c95053 100644 --- a/rtl/memdev.v +++ b/rtl/memdev.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: memdev.v -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This file is really simple: it creates an on-chip memory, @@ -17,11 +17,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -34,59 +34,74 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module memdev(i_clk, i_reset, - i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, i_wb_sel, - o_wb_stall, o_wb_ack, o_wb_data); - parameter LGMEMSZ=15, DW=32, EXTRACLOCK= 1; - parameter HEXFILE=""; - parameter [0:0] OPT_ROM = 1'b0; - localparam AW = LGMEMSZ - 2; - input wire i_clk, i_reset; - input wire i_wb_cyc, i_wb_stb, i_wb_we; - input wire [(AW-1):0] i_wb_addr; - input wire [(DW-1):0] i_wb_data; - input wire [(DW/8-1):0] i_wb_sel; - output wire o_wb_stall; - output reg o_wb_ack; - output reg [(DW-1):0] o_wb_data; - +// }}} +module memdev #( + // {{{ + parameter LGMEMSZ=15, DW=32, EXTRACLOCK= 1, + parameter HEXFILE="", + parameter [0:0] OPT_ROM = 1'b0, + localparam AW = LGMEMSZ - 2 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + input wire i_wb_cyc, i_wb_stb, i_wb_we, + input wire [(AW-1):0] i_wb_addr, + input wire [(DW-1):0] i_wb_data, + input wire [(DW/8-1):0] i_wb_sel, + output wire o_wb_stall, + output reg o_wb_ack, + output reg [(DW-1):0] o_wb_data + // }}} + ); + + // Local declarations + // {{{ wire w_wstb, w_stb; wire [(DW-1):0] w_data; wire [(AW-1):0] w_addr; wire [(DW/8-1):0] w_sel; reg [(DW-1):0] mem [0:((1< for a copy. -// +// }}} // License: LGPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/lgpl.html // -// //////////////////////////////////////////////////////////////////////////////// // // `default_nettype none -// +// }}} // 290 raw, 372 w/ pipe, 410 cfg, 499 cfg w/pipe -module qflexpress(i_clk, i_reset, - i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, i_wb_sel, - o_wb_stall, o_wb_ack, o_wb_data, - i_cfg_cyc, i_cfg_stb, i_cfg_we, i_cfg_data, i_cfg_sel, - o_cfg_stall, o_cfg_ack, o_cfg_data, - o_qspi_sck, o_qspi_cs_n, o_qspi_mod, o_qspi_dat, i_qspi_dat, - o_dbg_trigger, o_debug); - // - // LGFLASHSZ is the size of the flash memory. It defines the number - // of bits in the address register and more. This controller will support - // flash sizes up to 2^LGFLASHSZ, where LGFLASHSZ goes up to 32. - parameter LGFLASHSZ=24; - // - // OPT_PIPE makes it possible to string multiple requests together, - // with no intervening need to shutdown the QSPI connection and send a - // new address - parameter [0:0] OPT_PIPE = 1'b1; - // - // OPT_CFG enables the configuration logic port, and hence the - // ability to erase and program the flash, as well as the ability - // to perform other commands such as read-manufacturer ID, adjust - // configuration registers, etc. - parameter [0:0] OPT_CFG = 1'b1; - // - // OPT_STARTUP enables the startup logic - parameter [0:0] OPT_STARTUP = 1'b1; - // - // OPT_ADDR32 enables 32 bit addressing, rather than 24bit - // Control this by controlling the LGFLASHSZ parameter above. Anything - // greater than 24 will use 32-bit addressing, otherwise the regular - // 24-bit addressing - localparam [0:0] OPT_ADDR32 = (LGFLASHSZ > 24); - // - parameter OPT_CLKDIV = 0; - // - // Normally, I place the first byte read from the flash, and the lowest - // flash address, into bits [7:0], and then shift it up--to where upon - // return it is found in bits [31:24]. This is ideal for a big endian - // systems, not so much for little endian systems. The endian swap - // allows the bus to swap the return values in order to support little - // endian systems. - parameter [0:0] OPT_ENDIANSWAP = 1'b0; - // - // OPT_ODDR will be true any time the clock has no clock division - localparam [0:0] OPT_ODDR = (OPT_CLKDIV == 0); - // - // CKDV_BITS is the number of bits necessary to represent a counter - // that can do the CLKDIV division - localparam CKDV_BITS = (OPT_CLKDIV == 0) ? 0 - : ((OPT_CLKDIV < 2) ? 1 - : ((OPT_CLKDIV < 4) ? 2 - : ((OPT_CLKDIV < 8) ? 3 - : ((OPT_CLKDIV < 16) ? 4 - : ((OPT_CLKDIV < 32) ? 5 - : ((OPT_CLKDIV < 64) ? 6 - : ((OPT_CLKDIV < 128) ? 7 - : ((OPT_CLKDIV < 256) ? 8 : 9)))))))); - // - // RDDELAY is the number of clock cycles from when o_qspi_dat is valid - // until i_qspi_dat is valid. Read delays from 0-4 have been verified. - // DDR Registered I/O on a Xilinx device can be done with a RDDELAY=3 - // On Intel/Altera devices, RDDELAY=2 works - // I'm using RDDELAY=0 for my iCE40 devices - // - parameter RDDELAY = 0; - // - // NDUMMY is the number of "dummy" clock cycles between the 24-bits of - // the Quad I/O address and the first data bits. This includes the - // two clocks of the Quad output mode byte, 0xa0. The default is 10 - // for a Micron device. Windbond seems to want 2. Note your flash - // device carefully when you choose this value. - // - parameter NDUMMY = 10; - // - // For dealing with multiple flash devices, the OPT_STARTUP_FILE allows - // a hex file to be provided containing the necessary script to place - // the design into the proper initial configuration. - parameter OPT_STARTUP_FILE="spansion.hex"; - // - // - // - // - localparam [4:0] CFG_MODE = 12; - localparam [4:0] QSPEED_BIT = 11; - localparam [4:0] DSPEED_BIT = 10; // Not supported - localparam [4:0] DIR_BIT = 9; - localparam [4:0] USER_CS_n = 8; - // - localparam [1:0] NORMAL_SPI = 2'b00; - localparam [1:0] QUAD_WRITE = 2'b10; - localparam [1:0] QUAD_READ = 2'b11; - // localparam [7:0] DIO_READ_CMD = 8'hbb; - localparam [7:0] QIO_READ_CMD = OPT_ADDR32 ? 8'hec : 8'heb; - // - localparam AW=LGFLASHSZ-2; - localparam DW=32; - // - input wire i_clk, i_reset; - // - // Flash memory port - input wire i_wb_cyc, i_wb_stb, i_wb_we; - input wire [(AW-1):0] i_wb_addr; - input wire [(DW-1):0] i_wb_data; - input wire [DW/8-1:0] i_wb_sel; - // - output wire o_wb_stall, o_wb_ack; - output reg [(DW-1):0] o_wb_data; - // - // Configuration port - input wire i_cfg_cyc, i_cfg_stb, i_cfg_we; - input wire [(DW-1):0] i_cfg_data; - input wire [DW/8-1:0] i_cfg_sel; - // - output wire o_cfg_stall, o_cfg_ack; - output wire [(DW-1):0] o_cfg_data; +module qflexpress #( + // {{{ + // LGFLASHSZ + // {{{ + // LGFLASHSZ is the size of the flash memory. It defines the + // number of bits in the address register and more. This + // controller will support flash sizes up to 2^LGFLASHSZ, + // where LGFLASHSZ goes up to 32. + parameter LGFLASHSZ=24, + // }}} + // OPT_PIPE + // {{{ + // OPT_PIPE makes it possible to string multiple requests + // together, with no intervening need to shutdown the QSPI + // connection and send a new address + parameter [0:0] OPT_PIPE = 1'b1, + // }}} + // OPT_CFG + // {{{ + // OPT_CFG enables the configuration logic port, and hence the + // ability to erase and program the flash, as well as the + // ability to perform other commands such as read-manufacturer + // ID, adjust configuration registers, etc. + parameter [0:0] OPT_CFG = 1'b1, + // }}} + // + // OPT_STARTUP enables the startup logic + parameter [0:0] OPT_STARTUP = 1'b1, + // + // OPT_ADDR32 + // {{{ + // OPT_ADDR32 enables 32 bit addressing, rather than 24bit + // Control this by controlling the LGFLASHSZ parameter above. + // Anything greater than 24 will use 32-bit addressing, + // otherwise the regular 24-bit addressing + localparam [0:0] OPT_ADDR32 = (LGFLASHSZ > 24), + // }}} + // + parameter OPT_CLKDIV = 0, + // + // OPT_ENDIANSWAP + // {{{ + // Normally, I place the first byte read from the flash, and + // the lowest flash address, into bits [7:0], and then shift + // it up--to where upon return it is found in bits [31:24]. + // This is ideal for a big endian systems, not so much for + // little endian systems. The endian swap allows the bus to + // swap the return values in order to support little endian + // systems. + parameter [0:0] OPT_ENDIANSWAP = 1'b1, + // }}} + // OPT_ODDR + // {{{ + // OPT_ODDR will be true any time the clock has no clock + // division + localparam [0:0] OPT_ODDR = (OPT_CLKDIV == 0), + // }}} + // CKDV_BITS + // {{{ + // CKDV_BITS is the number of bits necessary to represent a + // counter that can do the CLKDIV division + localparam CKDV_BITS = (OPT_CLKDIV == 0) ? 0 + : ((OPT_CLKDIV < 2) ? 1 + : ((OPT_CLKDIV < 4) ? 2 + : ((OPT_CLKDIV < 8) ? 3 + : ((OPT_CLKDIV < 16) ? 4 + : ((OPT_CLKDIV < 32) ? 5 + : ((OPT_CLKDIV < 64) ? 6 + : ((OPT_CLKDIV < 128) ? 7 + : ((OPT_CLKDIV < 256) ? 8 : 9)))))))), + // }}} + // RDDELAY + // {{{ + // RDDELAY is the number of clock cycles from when o_qspi_dat + // is valid until i_qspi_dat is valid. Read delays from 0-4 + // have been verified. DDR Registered I/O on a Xilinx device + // can be done with a RDDELAY=3 + // On Intel/Altera devices, RDDELAY=2 works + // I'm using RDDELAY=0 for my iCE40 devices + parameter RDDELAY = 0, + // }}} + // NDUMMY + // {{{ + // NDUMMY is the number of "dummy" clock cycles between the + // 24-bits of the Quad I/O address and the first data bits. + // This includes the two clocks of the Quad output mode byte, + // 0xa0. The default is 10 for a Micron device. Windbond + // seems to want 2. Note your flash device carefully when + // you choose this value. + parameter NDUMMY = 6, + // }}} + // OPT_STARTUP_FILE + // {{{ + // For dealing with multiple flash devices, the + // OPT_STARTUP_FILE allows a hex file to be provided containing + // the necessary script to place the design into the proper + // initial configuration. + parameter OPT_STARTUP_FILE="spansion.hex", + // }}} + // + // + localparam [4:0] CFG_MODE = 12, + localparam [4:0] QSPEED_BIT = 11, + // localparam [4:0] DSPEED_BIT = 10, // Not supported + localparam [4:0] DIR_BIT = 9, + localparam [4:0] USER_CS_n = 8, + // + localparam [1:0] NORMAL_SPI = 2'b00, + localparam [1:0] QUAD_WRITE = 2'b10, + localparam [1:0] QUAD_READ = 2'b11, + // localparam [7:0] DIO_READ_CMD = 8'hbb, + // localparam [7:0] QIO_READ_CMD = OPT_ADDR32 ? 8'hec : 8'heb, + // + localparam AW=LGFLASHSZ-2, + localparam DW=32 +`ifdef FORMAL + , localparam F_LGDEPTH=$clog2(3+RDDELAY+(OPT_ADDR32 ? 2:0)) +`endif + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Flash memory port + input wire i_wb_cyc, i_wb_stb, i_wb_we, + input wire [AW-1:0] i_wb_addr, + input wire [DW-1:0] i_wb_data, + input wire [DW/8-1:0] i_wb_sel, + // + output wire o_wb_stall, o_wb_ack, + output reg [DW-1:0] o_wb_data, + // Configuration port + input wire i_cfg_cyc, i_cfg_stb, i_cfg_we, + input wire [(DW-1):0] i_cfg_data, + input wire [DW/8-1:0] i_cfg_sel, + // + output wire o_cfg_stall, o_cfg_ack, + output wire [DW-1:0] o_cfg_data, + // + output reg o_qspi_sck, + output reg o_qspi_cs_n, + output reg [1:0] o_qspi_mod, + output wire [3:0] o_qspi_dat, + input wire [3:0] i_qspi_dat, + // + // Debugging port (if used) + output wire o_dbg_trigger, + output wire [31:0] o_debug + // }}} + ); + + // Signal declarations + // {{{ + reg dly_ack, read_sck, xtra_stall; + // clk_ctr must have enough bits for ... + // 6 address clocks, 4-bits each + // NDUMMY dummy clocks, including two mode bytes + // 8 data clocks + // (RDDELAY clocks not counted here) + reg [4:0] clk_ctr; + // - output reg o_qspi_sck; - output reg o_qspi_cs_n; - output reg [1:0] o_qspi_mod; - output wire [3:0] o_qspi_dat; - input wire [3:0] i_qspi_dat; + // User override logic // - // Debugging port - output wire o_dbg_trigger; - output wire [31:0] o_debug; - + reg cfg_mode, cfg_speed, cfg_dir, cfg_cs; + wire cfg_write, cfg_hs_write, cfg_ls_write, cfg_hs_read, + user_request, bus_request, pipe_req, cfg_noop, cfg_stb; // // // Arbitrated bus registers and inputs @@ -206,10 +246,11 @@ module qflexpress(i_clk, i_reset, // // Memory vs Configuration bus arbiter // - wbarbiter #(.DW(DW), .AW(AW+1), .SCHEME("PRIORITY"), - .F_MAX_STALL(0), - .F_MAX_ACK_DELAY(0)) - arbiter(i_clk, i_reset, + wbarbiter #( + // {{{ + .DW(DW), .AW(AW+1), .SCHEME("PRIORITY") + // }}} + ) arbiter(i_clk, i_reset, i_wb_cyc, i_wb_stb, i_wb_we, { 1'b0, i_wb_addr }, i_wb_data, i_wb_sel, o_wb_stall, o_wb_ack, ign_wb_err, @@ -246,22 +287,6 @@ module qflexpress(i_clk, i_reset, assign o_cfg_data = 0; end endgenerate - - reg dly_ack, read_sck, xtra_stall; - // clk_ctr must have enough bits for ... - // 6 address clocks, 4-bits each - // NDUMMY dummy clocks, including two mode bytes - // 8 data clocks - // (RDDELAY clocks not counted here) - reg [4:0] clk_ctr; - - // - // User override logic - // - reg cfg_mode, cfg_speed, cfg_dir, cfg_cs; - wire cfg_write, cfg_hs_write, cfg_ls_write, cfg_hs_read, - user_request, bus_request, pipe_req, cfg_noop, cfg_stb; - // assign bus_request = (mem_bus_stb)&&(!bus_stall) &&(!bus_we)&&(!cfg_mode); assign cfg_stb = (OPT_CFG)&&(cfg_bus_stb)&&(!bus_stall); @@ -285,10 +310,23 @@ module qflexpress(i_clk, i_reset, reg m_clk; reg [3:0] m_dat; + reg [32+(OPT_ADDR32 ? 8:0)+4*(OPT_ODDR ? 0:1)-1:0] data_pipe; + reg pre_ack = 1'b0; + reg actual_sck; + + // }}} - generate if (OPT_ODDR) - begin + //////////////////////////////////////////////////////////////////////// + // + // Clock division: ckstb, ckpos, ckneg, ckpre + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + generate if (OPT_ODDR) + begin // Flash clock == system clock speed + // {{{ always @(*) begin ckstb = 1'b1; @@ -296,10 +334,10 @@ module qflexpress(i_clk, i_reset, ckneg = 1'b1; ckpre = 1'b1; end - + // }}} end else if (OPT_CLKDIV == 1) - begin : CKSTB_ONE - + begin : CKSTB_ONE // Flash clock can be generated logically, == sysclk/2 + // {{{ reg clk_counter; initial clk_counter = 1'b1; @@ -320,9 +358,9 @@ module qflexpress(i_clk, i_reset, ckpos = (clk_counter == 1); ckneg = (clk_counter == 0); end - + // }}} end else begin : CKSTB_GEN - + // {{{ reg [CKDV_BITS-1:0] clk_counter; initial clk_counter = OPT_CLKDIV; @@ -354,15 +392,21 @@ module qflexpress(i_clk, i_reset, always @(*) ckneg = ckstb; + // }}} end endgenerate - - // + // }}} + //////////////////////////////////////////////////////////////////////// // // Maintenance / startup portion + // {{{ + //////////////////////////////////////////////////////////////////////// // // generate if (OPT_STARTUP) begin : GEN_STARTUP + // {{{ + // Signal declarations + // {{{ localparam M_WAITBIT=10; localparam M_LGADDR=5; localparam M_FIRSTIDX=0; @@ -375,7 +419,10 @@ module qflexpress(i_clk, i_reset, reg m_midcount; reg [3:0] m_bitcount; reg [7:0] m_byte; + // }}} + // Command ISA description: + // {{{ // Let's script our startup with a series of commands. // These commands are specific to the Micron Serial NOR flash // memory that was on the original Arty A7 board. Switching @@ -402,11 +449,12 @@ module qflexpress(i_clk, i_reset, // 8'bit data To be sent 1-bit at a time in NORMAL_SPI // mode, or 4-bits at a time in QUAD_WRITE // mode. Ignored otherwis - // + // }}} integer k; initial if (OPT_STARTUP_FILE != 0) $readmemh(OPT_STARTUP_FILE, m_cmd_word); else begin + // {{{ for(k=0; k<(1< 1); if (m_counter > 0) m_counter <= m_counter - 1'b1; + // }}} end + // }}} + // m_cs_n, m_mod, m_bitcount + // {{{ initial m_cs_n = 1'b1; initial m_mod = NORMAL_SPI; always @(posedge i_clk) @@ -570,12 +635,16 @@ module qflexpress(i_clk, i_reset, m_bitcount <= (!OPT_ODDR && m_cs_n) ? 4'h8 : 4'h7;//i.e.7 end end + // }}} + // m_dat, m_byte + // {{{ always @(posedge i_clk) if (m_ce) begin if (m_bitcount == 0) begin + // {{{ if (!OPT_ODDR && m_cs_n) begin m_dat <= {(4){m_this_word[7]}}; @@ -590,7 +659,9 @@ module qflexpress(i_clk, i_reset, m_byte <= { m_this_word[6:0], 1'b0 }; end end + // }}} end else begin + // {{{ m_dat <= m_byte[7:4]; m_byte <= { m_byte[3:0], 4'h0 }; if (!m_mod[1]) @@ -601,15 +672,19 @@ module qflexpress(i_clk, i_reset, end else begin m_byte <= { m_byte[3:0], 4'b00 }; end + // }}} end end + // }}} + // m_clk + // {{{ if (OPT_ODDR) begin always @(*) m_clk = !m_cs_n; end else begin - + // {{{ always @(posedge i_clk) if (i_reset) m_clk <= 1'b1; @@ -623,10 +698,13 @@ module qflexpress(i_clk, i_reset, m_clk <= 1'b1; else if (ckneg) m_clk <= 1'b0; + // }}} end + // }}} + // }}} end else begin : NO_STARTUP_OPT - + // {{{ always @(*) begin maintenance = 0; @@ -637,22 +715,23 @@ module qflexpress(i_clk, i_reset, end // verilator lint_off UNUSED - wire [8:0] unused_maintenance; - assign unused_maintenance = { maintenance, + wire unused_maintenance; + assign unused_maintenance = &{ 1'b0, maintenance, m_mod, m_cs_n, m_clk, m_dat }; // verilator lint_on UNUSED + // }}} end endgenerate - - - reg [32+(OPT_ADDR32 ? 8:0)+4*(OPT_ODDR ? 0:1)-1:0] data_pipe; - reg pre_ack = 1'b0; - reg actual_sck; - - // + // }}} + //////////////////////////////////////////////////////////////////////// // // Data / access portion + // {{{ + //////////////////////////////////////////////////////////////////////// // // + + // data_pipe + // {{{ initial data_pipe = 0; always @(posedge i_clk) begin @@ -689,9 +768,12 @@ module qflexpress(i_clk, i_reset, if (maintenance) data_pipe[28+(OPT_ADDR32 ? 8:0)+4*(OPT_ODDR ? 0:1) +: 4] <= m_dat; end + // }}} assign o_qspi_dat = data_pipe[28+(OPT_ADDR32 ? 8:0)+4*(OPT_ODDR ? 0:1) +: 4]; + // pre_ack + // {{{ // Since we can't abort any transaction once started, without // risking losing XIP mode or any other mode we might be in, we'll // keep track of whether this operation should be ack'd upon @@ -701,9 +783,13 @@ module qflexpress(i_clk, i_reset, pre_ack <= 1'b0; else if ((bus_request)||(cfg_write)) pre_ack <= 1'b1; + // }}} + // pipe_req + // {{{ generate if (OPT_PIPE) begin : OPT_PIPE_BLOCK + // {{{ reg r_pipe_req; wire w_pipe_condition; @@ -728,11 +814,14 @@ module qflexpress(i_clk, i_reset, r_pipe_req <= w_pipe_condition; assign pipe_req = r_pipe_req; + // }}} end else begin assign pipe_req = 1'b0; end endgenerate + // }}} - + // clk_ctr + // {{{ initial clk_ctr = 0; always @(posedge i_clk) if ((i_reset)||(maintenance)) @@ -752,7 +841,10 @@ module qflexpress(i_clk, i_reset, clk_ctr <= 5'd2 + ((OPT_ODDR) ? 0:1); else if ((ckstb)&&(|clk_ctr)) clk_ctr <= clk_ctr - 1'b1; + // }}} + // o_qspi_sck + // {{{ initial o_qspi_sck = (!OPT_ODDR); always @(posedge i_clk) if (i_reset) @@ -765,6 +857,7 @@ module qflexpress(i_clk, i_reset, o_qspi_sck <= 1'b1; else if (OPT_ODDR) begin + // {{{ if ((cfg_mode)&&(clk_ctr <= 1)) // Config mode has no pipe instructions o_qspi_sck <= 1'b0; @@ -772,11 +865,13 @@ module qflexpress(i_clk, i_reset, o_qspi_sck <= 1'b1; else o_qspi_sck <= 1'b0; + // }}} end else if (((ckpos)&&(!o_qspi_sck))||(o_qspi_cs_n)) begin o_qspi_sck <= 1'b1; - end else if ((ckneg)&&(o_qspi_sck)) begin - + end else if ((ckneg)&&(o_qspi_sck)) + begin + // {{{ if ((cfg_mode)&&(clk_ctr <= 1)) // Config mode has no pipe instructions o_qspi_sck <= 1'b1; @@ -784,8 +879,12 @@ module qflexpress(i_clk, i_reset, o_qspi_sck <= 1'b0; else o_qspi_sck <= 1'b1; + // }}} end + // }}} + // o_qspi_cs_n + // {{{ initial o_qspi_cs_n = 1'b1; always @(posedge i_clk) if (i_reset) @@ -800,7 +899,10 @@ module qflexpress(i_clk, i_reset, o_qspi_cs_n <= 1'b0; else if (ckstb) o_qspi_cs_n <= (clk_ctr <= 1); + // }}} + // o_qspi_mod + // {{{ // Control the mode of the external pins // NORMAL_SPI: i_miso is an input, o_mosi is an output // QUAD_READ: i_miso is an input, o_mosi is an input @@ -821,7 +923,10 @@ module qflexpress(i_clk, i_reset, o_qspi_mod <= NORMAL_SPI; else if ((ckstb)&&(clk_ctr <= 5'd9)&&((!cfg_mode)||(!cfg_dir))) o_qspi_mod <= QUAD_READ; + // }}} + // bus_stall + // {{{ initial bus_stall = 1'b1; always @(posedge i_clk) if (i_reset) @@ -840,9 +945,13 @@ module qflexpress(i_clk, i_reset, bus_stall <= 1'b1; else bus_stall <= 1'b0; + // }}} end else if (ckpre && (mem_bus_stb)&&(pipe_req)&&(clk_ctr == 5'd1)) bus_stall <= 1'b0; + // }}} + // dly_ack + // {{{ initial dly_ack = 1'b0; always @(posedge i_clk) if (i_reset) @@ -855,42 +964,48 @@ module qflexpress(i_clk, i_reset, dly_ack <= 1'b1; else dly_ack <= 1'b0; + // }}} + // actual_sck + // {{{ generate if (OPT_ODDR) begin : SCK_ACTUAL - + // {{{ always @(*) actual_sck = o_qspi_sck; - + // }}} end else if (OPT_CLKDIV == 1) begin : SCK_ONE - + // {{{ initial actual_sck = 1'b0; always @(posedge i_clk) if (i_reset) actual_sck <= 1'b0; else actual_sck <= (!o_qspi_sck)&&(clk_ctr > 0); - + // }}} end else begin : SCK_ANY - + // {{{ initial actual_sck = 1'b0; always @(posedge i_clk) if (i_reset) actual_sck <= 1'b0; else actual_sck <= (o_qspi_sck)&&(ckpre)&&(clk_ctr > 0); - + // }}} end endgenerate + // }}} + // read_sck, o_wb_ack, and xtra_stall + // {{{ `ifdef FORMAL reg [F_LGDEPTH-1:0] f_extra; `endif generate if (RDDELAY == 0) begin : RDDELAY_NONE - + // {{{ always @(*) begin read_sck = actual_sck; @@ -898,18 +1013,21 @@ module qflexpress(i_clk, i_reset, xtra_stall = 1'b0; end - + // }}} end else begin : RDDELAY_NONZERO - + // {{{ reg [RDDELAY-1:0] sck_pipe, ack_pipe, stall_pipe; reg not_done; + // sck_pipe, ack_pipe, and stall_pipe + // {{{ initial sck_pipe = 0; initial ack_pipe = 0; initial stall_pipe = -1; if (RDDELAY > 1) begin + // {{{ always @(posedge i_clk) if (i_reset) sck_pipe <= 0; @@ -927,9 +1045,10 @@ module qflexpress(i_clk, i_reset, stall_pipe <= -1; else stall_pipe <= { stall_pipe[RDDELAY-2:0], not_done }; - - - end else begin + // }}} + end else // if (RDDELAY > 0) + begin + // {{{ always @(posedge i_clk) if (i_reset) sck_pipe <= 0; @@ -947,8 +1066,12 @@ module qflexpress(i_clk, i_reset, stall_pipe <= -1; else stall_pipe <= not_done; + // }}} end + // }}} + // not_done + // {{{ always @(*) begin not_done = bus_stb && !bus_stall; @@ -957,6 +1080,7 @@ module qflexpress(i_clk, i_reset, if ((clk_ctr == 1)&&(!ckstb)) not_done = 1'b1; end + // }}} always @(*) bus_ack = ack_pipe[RDDELAY-1]; @@ -967,14 +1091,19 @@ module qflexpress(i_clk, i_reset, always @(*) xtra_stall = |stall_pipe; + // }}} end endgenerate + // }}} + // o_wb_data + // {{{ always @(posedge i_clk) begin if (read_sck) begin if (OPT_ENDIANSWAP) begin + // {{{ if (!o_qspi_mod[1]) begin o_wb_data <= { o_wb_data[30:24], i_qspi_dat[1], @@ -996,7 +1125,7 @@ module qflexpress(i_clk, i_reset, else o_wb_data[7:0]<= { o_wb_data[3:0], i_qspi_dat }; end - + // }}} end else if (!o_qspi_mod[1]) // No endian-swapping o_wb_data <= { o_wb_data[30:0], i_qspi_dat[1] }; @@ -1008,27 +1137,38 @@ module qflexpress(i_clk, i_reset, o_wb_data[16:8] <= { 4'b0, cfg_mode, cfg_speed, 1'b0, cfg_dir, cfg_cs }; end - - + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// // - // - // User override access + // Configuration / user overrride access port + // {{{ + //////////////////////////////////////////////////////////////////////// // // + + // cfg_mode + // {{{ initial cfg_mode = 1'b0; always @(posedge i_clk) if ((i_reset)||(!OPT_CFG)) cfg_mode <= 1'b0; else if ((cfg_bus_stb)&&(!bus_stall)&&(i_cfg_we)) cfg_mode <= i_cfg_data[CFG_MODE]; + // }}} + // cfg_cs + // {{{ initial cfg_cs = 1'b0; always @(posedge i_clk) if ((i_reset)||(!OPT_CFG)) cfg_cs <= 1'b0; else if ((cfg_bus_stb)&&(!bus_stall)&&(i_cfg_we)) cfg_cs <= (!i_cfg_data[USER_CS_n])&&(i_cfg_data[CFG_MODE]); + // }}} + // cfg_speed, cfg_dir + // {{{ initial cfg_speed = 1'b0; initial cfg_dir = 1'b0; always @(posedge i_clk) @@ -1041,6 +1181,16 @@ module qflexpress(i_clk, i_reset, cfg_speed <= i_cfg_data[QSPEED_BIT]; cfg_dir <= i_cfg_data[DIR_BIT]; end + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Debugging bus (if used) + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + reg r_last_cfg; @@ -1058,6 +1208,9 @@ module qflexpress(i_clk, i_reset, ? i_cfg_data[7:0] : o_wb_data[7:0] }; + // }}} + // Make Verilator happy + // {{{ // verilator lint_off UNUSED wire unused; assign unused = &{ 1'b0, i_wb_data, i_cfg_data[31:12], bus_sel }; diff --git a/rtl/rtc/rtcalarm.v b/rtl/rtc/rtcalarm.v index c836bbb..b8c4b11 100644 --- a/rtl/rtc/rtcalarm.v +++ b/rtl/rtc/rtcalarm.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: rtcalarm.v -// +// {{{ // Project: A Wishbone Controlled Real--time Clock Core, w/ GPS synch // // Purpose: Implement an alarm for a real time clock. @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,38 +28,48 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // // `default_nettype none -// +// }}} // set, clear, turn on, turn off -module rtcalarm(i_clk, i_reset, i_now, +module rtcalarm #( + // {{{ + parameter [0:0] OPT_PREVALIDATED_INPUT = 1'b0, + parameter [21:0] OPT_INITIAL_ALARM_TIME = 0, + parameter [0:0] OPT_START_ENABLED = 0, + parameter [0:0] OPT_FIXED_ALARM_TIME = 0 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, // - i_wr, i_clear, i_enable, i_alarm_time, i_valid, + input wire [21:0] i_now, // - o_data, o_alarm); - parameter [0:0] OPT_PREVALIDATED_INPUT = 1'b0; - parameter [21:0] OPT_INITIAL_ALARM_TIME = 0; - parameter [0:0] OPT_START_ENABLED = 0; - parameter [0:0] OPT_FIXED_ALARM_TIME = 0; - // - input wire i_clk, i_reset; - // - input wire [21:0] i_now; - // - input wire i_wr; - input wire i_enable, i_clear; - input wire [21:0] i_alarm_time; - input wire [2:0] i_valid; - // - output wire [31:0] o_data; - output wire o_alarm; + input wire i_wr, + input wire i_clear, i_enable, + input wire [21:0] i_alarm_time, + input wire [2:0] i_valid, + // + output wire [31:0] o_data, + output wire o_alarm + // }}} + ); + + // Signal declarations + // {{{ + reg [2:0] pre_valid; + reg [21:0] validated_alarm_time; + reg [21:0] alarm_time, past_time; + reg enabled, // Whether the alarm is enabled + tripped; // Whether the alarm has tripped + // }}} // // The alarm code @@ -70,20 +80,22 @@ module rtcalarm(i_clk, i_reset, i_now, // can come and see that the alarm tripped. // // - reg [21:0] alarm_time, past_time; - reg enabled, // Whether the alarm is enabled - tripped; // Whether the alarm has tripped + // enabled + // {{{ initial enabled = OPT_START_ENABLED; always @(posedge i_clk) if (i_reset) enabled <= OPT_START_ENABLED; else if (i_wr) enabled <= i_enable; + // }}} always @(posedge i_clk) past_time <= i_now; + // tripped + // {{{ initial tripped= 1'b0; always @(posedge i_clk) if (i_reset) @@ -92,21 +104,22 @@ module rtcalarm(i_clk, i_reset, i_now, tripped <= 1'b1; else if ((i_wr)&&(i_clear)) tripped <= 1'b0; + // }}} - reg [2:0] pre_valid; - reg [21:0] validated_alarm_time; + // pre_valid, validated_alarm_time + // {{{ generate if (OPT_PREVALIDATED_INPUT) begin : INPUT_IS_VALID - + // {{{ always @(*) pre_valid = ((!OPT_FIXED_ALARM_TIME)&&(i_wr)) ? i_valid : 0; always @(*) validated_alarm_time = i_valid; - + // }}} end else begin : CHECK_INPUT_VALIDITY - + // {{{ initial pre_valid = 0; always @(posedge i_clk) if ((i_reset)||(!i_wr)||(OPT_FIXED_ALARM_TIME)) @@ -128,9 +141,12 @@ module rtcalarm(i_clk, i_reset, i_now, always @(posedge i_clk) validated_alarm_time <= i_alarm_time; - + // }}} end endgenerate + // }}} + // alarm_time + // {{{ initial alarm_time = OPT_INITIAL_ALARM_TIME; always @(posedge i_clk) if (i_reset) @@ -147,16 +163,27 @@ module rtcalarm(i_clk, i_reset, i_now, if (pre_valid[2]) // Hours alarm_time[21:16] <= validated_alarm_time[21:16]; end + // }}} assign o_data = { 6'h0, tripped, enabled, 2'b00, alarm_time }; assign o_alarm = tripped; // Make verilator happy + // {{{ // verilator lint_off UNUSED - // wire [6:0] unused; - // assign unused = { i_wb_cyc, i_wb_data[31:26] }; + // wire unused; + // assign unused = &{ 1'b0, i_wb_cyc, i_wb_data[31:26] }; // verilator lint_on UNUSED - + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL `ifdef RTCALARM `define ASSUME assume @@ -247,6 +274,12 @@ module rtcalarm(i_clk, i_reset, i_now, if ((f_past_valid)&&($past(i_wr))&&(!$past(i_reset))) `ASSERT(enabled == $past(i_enable)); + //////////////////////////////////////////////////////////////////////// + // + // Cover checks + // {{{ + //////////////////////////////////////////////////////////////////////// + // always @(posedge i_clk) if ((f_past_valid)&&(!$past(tripped))) cover(tripped); @@ -254,6 +287,7 @@ module rtcalarm(i_clk, i_reset, i_now, always @(posedge i_clk) if ((f_past_valid)&&($past(tripped))) cover(!tripped); - + // }}} `endif +// }}} endmodule diff --git a/rtl/rtc/rtcbare.v b/rtl/rtc/rtcbare.v index 2e0ff57..ecec880 100644 --- a/rtl/rtc/rtcbare.v +++ b/rtl/rtc/rtcbare.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: rtcbare.v -// +// {{{ // Project: A Wishbone Controlled Real--time Clock Core, w/ GPS synch // // Purpose: This is the bare RTC clock logic. It accepts an (optional) @@ -24,11 +24,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -41,8 +41,9 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // // @@ -50,41 +51,54 @@ // // `default_nettype none -// -module rtcbare(i_clk, i_reset, +// }}} +module rtcbare #( + // {{{ + // Set OPT_PREVALIDATED_INPUT to 1'b1 if the instantiating + // module will never set i_wr true with invalid BCD data in + // i_data. Otherwise, setting it to zero will cause a + // validation check, and ignore any incoming values that are + // not valid BCD on a bytewise level. + parameter [0:0] OPT_PREVALIDATED_INPUT = 1'b0 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, // Wishbone interface - i_pps, i_wr, i_data, i_valid, + input wire i_pps, i_wr, + input wire [21:0] i_data, + input wire [2:0] i_valid, // Output registers - o_data, // multiplexed based upon i_wb_addr + output wire [21:0] o_data, // multiplexed based on i_wb_adr // A once-per-day strobe on the last clock of the day - o_ppd); - // - // Set OPT_PREVALIDATED_INPUT to 1'b1 if the instantiating module - // will never set i_wr true with invalid BCD data in i_data. - // Otherwise, setting it to zero will cause a validation check, - // and ignore any incoming values that are not valid BCD on a bytewise - // level. - parameter [0:0] OPT_PREVALIDATED_INPUT = 1'b0; - // - input wire i_clk, i_reset; - // - input wire i_pps, i_wr; - input wire [21:0] i_data; - input wire [2:0] i_valid; - output wire [21:0] o_data; - output wire o_ppd; + output wire o_ppd + // }}} + ); + // Signal declarations + // {{{ reg [21:0] bcd_clock, next_clock; reg [5:0] carry; reg pre_ppd; + reg [2:0] pre_valid; + reg [21:0] pre_bcd_clock; + + reg [2:0] suppressed, suppress_count; + // }}} + + // pre_ppd + // {{{ initial pre_ppd = 1'b0; always @(posedge i_clk) if (i_reset) pre_ppd <= 1'b0; else pre_ppd <= (bcd_clock == 22'h23_59_59); + // }}} + // carry, next_clock + // {{{ initial carry = 0; initial next_clock = 22'h00_00_01; always @(posedge i_clk) @@ -148,18 +162,14 @@ module rtcbare(i_clk, i_reset, else next_clock[21:20] <= bcd_clock[21:20]; end + // }}} - // - // + // pre_bcd_clock, pre_valid + // {{{ // Validate the input before setting the clock - // - // - reg [2:0] pre_valid; - reg [21:0] pre_bcd_clock; - generate if (OPT_PREVALIDATED_INPUT) begin : NO_VALIDATION_REQUIRED - + // {{{ // Write data through with no check // This can be combinatorial, with no clock required. // @@ -171,9 +181,9 @@ module rtcbare(i_clk, i_reset, always @(*) pre_bcd_clock = i_data; - + // }}} end else begin : VALIDATE_INPUT_BCD - // + // {{{ // Double check that the input contains valid BCD data // // We'll use this to prevent a write given invalid data @@ -199,12 +209,12 @@ module rtcbare(i_clk, i_reset, always @(posedge i_clk) pre_bcd_clock <= i_data; - + // }}} end endgenerate + // }}} - - reg [2:0] suppressed, suppress_count; - + // suprpressed, suppress_count + // {{{ initial suppressed = 3'h7; initial suppress_count = 3'h5; always @(posedge i_clk) @@ -222,8 +232,10 @@ module rtcbare(i_clk, i_reset, suppress_count <= suppress_count - 1; else suppressed <= 0; + // }}} - + // bcd_clock + // {{{ initial bcd_clock = 0; always @(posedge i_clk) if (i_reset) @@ -245,10 +257,19 @@ module rtcbare(i_clk, i_reset, if (pre_valid[2]) bcd_clock[21:16] <= pre_bcd_clock[21:16]; end + // }}} assign o_data = bcd_clock; assign o_ppd = (pre_ppd)&&(i_pps); - +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL `ifdef RTCBARE `define ASSUME assume @@ -357,7 +378,6 @@ module rtcbare(i_clk, i_reset, always @(*) if (suppressed[1]) `ASSERT(suppressed[2]); - - `endif +// }}}} endmodule diff --git a/rtl/rtc/rtcdate.v b/rtl/rtc/rtcdate.v index b02032b..052a43f 100644 --- a/rtl/rtc/rtcdate.v +++ b/rtl/rtc/rtcdate.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: rtcdate.v -// +// {{{ // Project: A Wishbone Controlled Real--time Clock Core // // Purpose: @@ -27,11 +27,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -44,8 +44,9 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // // @@ -53,42 +54,60 @@ // // `default_nettype none -// -module rtcdate(i_clk, i_ppd, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, i_wb_sel, - o_wb_stall, o_wb_ack, o_wb_data); - parameter [29:0] INITIAL_DATE = 30'h20000101; - input wire i_clk; - // A one part per day signal, i.e. basically a clock enable line that - // controls when the beginning of the day happens. This line should - // be high on the very last second of any day in order for the rtcdate - // module to always have the right date. - input wire i_ppd; - // Wishbone inputs - input wire i_wb_cyc, i_wb_stb, i_wb_we; - input wire [31:0] i_wb_data; - input wire [3:0] i_wb_sel; - // Wishbone outputs - output wire o_wb_stall; - output reg o_wb_ack; - output wire [31:0] o_wb_data; - +// }}} +module rtcdate #( + parameter [29:0] INITIAL_DATE = 30'h20000101 + ) ( + // {{{ + input wire i_clk, + // A one part per day signal, i.e. basically a clock enable + // line that controls when the beginning of the day happens. + // This line should be high on the very last second of any day + // in order for the rtcdate module to always have the right + // date. + input wire i_ppd, + // Wishbone inputs + input wire i_wb_cyc, i_wb_stb, i_wb_we, + input wire [31:0] i_wb_data, + input wire [3:0] i_wb_sel, + // Wishbone outputs + output wire o_wb_stall, + output reg o_wb_ack, + output wire [31:0] o_wb_data + // }}} + ); + + // Signal declarations + // {{{ wire update; reg [9:0] r_block_updates; + reg [5:0] r_day; + reg [4:0] r_mon; + reg [13:0] r_year; + + reg last_day_of_month, last_day_of_year, is_leap_year; + reg [5:0] days_per_month; + reg year_divisible_by_four, century_year, four_century_year; + reg [5:0] next_day, fixd_day; + reg [4:0] next_mon, fixd_mon; + reg [13:0] next_year; + reg [2:0] next_year_c; + // }}} + + // r_block_updates + // {{{ initial r_block_updates = 10'h3ff; always @(posedge i_clk) if ((i_wb_we)&&(i_wb_stb)) r_block_updates <= 10'h3ff; else r_block_updates <= { r_block_updates[8:0], 1'b0 }; + // }}} assign update = (i_ppd)&&(!r_block_updates[9]); - reg [5:0] r_day; - reg [4:0] r_mon; - reg [13:0] r_year; - - reg last_day_of_month, last_day_of_year, is_leap_year; - reg [5:0] days_per_month; + // days_per_month + // {{{ initial days_per_month = 6'h31; // Remember, this is BCD always @(posedge i_clk) begin // Clock 3 @@ -108,6 +127,15 @@ module rtcdate(i_clk, i_ppd, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, i_wb_sel, default: days_per_month <= 6'h31; // Invalid month endcase end + // }}} + + //////////////////////////////////////////////////////////////////////// + // + // Years + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // initial last_day_of_month = 1'b0; always @(posedge i_clk) // Clock 4 @@ -118,23 +146,33 @@ module rtcdate(i_clk, i_ppd, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, i_wb_sel, last_day_of_year <= (last_day_of_month) && (r_mon == 5'h12); - reg year_divisible_by_four, century_year, four_century_year; initial year_divisible_by_four = 1'b0; - initial century_year = 1'b0; - initial four_century_year = 1'b0; - initial is_leap_year = 1'b0; always @(posedge i_clk) // Clock 1 year_divisible_by_four<= ((!r_year[0])&&(r_year[4]==r_year[1])); + + initial century_year = 1'b0; always @(posedge i_clk) // Clock 1 century_year <= (r_year[7:0] == 8'h00); + + initial four_century_year = 1'b0; always @(posedge i_clk) // Clock 1 four_century_year <= ((!r_year[8])&&((r_year[12]==r_year[9]))); + + initial is_leap_year = 1'b0; always @(posedge i_clk) // Clock 2 is_leap_year <= (year_divisible_by_four)&&((!century_year) ||((century_year)&&(four_century_year))); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Days + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + // Adjust the day of month - reg [5:0] next_day, fixd_day; initial next_day = INITIAL_DATE[5:0]; always @(posedge i_clk) if (last_day_of_month) @@ -166,10 +204,17 @@ module rtcdate(i_clk, i_ppd, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, i_wb_sel, if ((i_wb_stb)&&(i_wb_we)&&(!i_wb_data[7])&&(i_wb_sel[0])) r_day <= i_wb_data[5:0]; end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Months + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // // Adjust the month of the year - reg [4:0] next_mon, fixd_mon; - + initial next_mon = 5'h01; always @(posedge i_clk) if (last_day_of_year) next_mon <= 5'h01; @@ -200,10 +245,16 @@ module rtcdate(i_clk, i_ppd, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, i_wb_sel, if ((i_wb_stb)&&(i_wb_we)&&(!i_wb_data[15])&&(i_wb_sel[1])) r_mon <= i_wb_data[12:8]; end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Years (again) + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // // Adjust the year - reg [13:0] next_year; - reg [2:0] next_year_c; initial next_year = INITIAL_DATE[29:16]; initial next_year_c = 0; always @(posedge i_clk) @@ -243,22 +294,40 @@ module rtcdate(i_clk, i_ppd, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, i_wb_sel, &&(i_wb_sel[3:2]==2'b11)) r_year <= i_wb_data[29:16]; end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Bus returns + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // initial o_wb_ack = 1'b0; always @(posedge i_clk) o_wb_ack <= (i_wb_stb); assign o_wb_stall = 1'b0; - assign o_wb_data = { 2'h0, r_year, 3'h0, r_mon, 2'h0, r_day }; + // }}} // Make Verilator happy + // {{{ // verilator lint_off UNUSED wire unused; - assign unused = &{ 1'b0, i_wb_cyc, i_wb_data[30], - i_wb_data[14:13], i_wb_data[6] }; + assign unused = &{ 1'b0, i_wb_cyc, i_wb_data[30], i_wb_data[14:13], + i_wb_data[6] }; // verilator lint_on UNUSED - + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL `ifdef RTCDATE `define ASSUME assume @@ -326,6 +395,6 @@ module rtcdate(i_clk, i_ppd, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, i_wb_sel, assert(r_year[ 7: 4] <= 4'h9); assert(r_year[11: 8] <= 4'h9); end - `endif +// }}} endmodule diff --git a/rtl/rtc/rtclight.v b/rtl/rtc/rtclight.v index b0558c9..bd502ba 100644 --- a/rtl/rtc/rtclight.v +++ b/rtl/rtc/rtclight.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: rtclight.v -// +// {{{ // Project: A Wishbone Controlled Real--time Clock Core // // Purpose: Implement a real time clock, including alarm, count--down @@ -17,11 +17,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -34,8 +34,9 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // // @@ -43,35 +44,38 @@ // // `default_nettype none -// -module rtclight(i_clk, i_reset, +// }}} +module rtclight #( + // {{{ + parameter DEFAULT_SPEED = 32'd2814750, // 100 Mhz + parameter [0:0] OPT_TIMER = 1'b1, + parameter [0:0] OPT_STOPWATCH = 1'b1, + parameter [0:0] OPT_ALARM = 1'b1, + parameter [0:0] OPT_FIXED_SPEED = 1'b1 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, // Wishbone interface - i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, i_wb_sel, - o_wb_stall, o_wb_ack, o_wb_data, + input wire i_wb_cyc, i_wb_stb, i_wb_we, + input wire [2:0] i_wb_addr, + input wire [31:0] i_wb_data, + input wire [3:0] i_wb_sel, + // + output wire o_wb_stall, + output reg o_wb_ack, + output reg [31:0] o_wb_data, // Output controls - o_interrupt, + output wire o_interrupt, // A once-per-second strobe - o_pps, + o_pps, // A once-per-day strobe on the last clock of the day - o_ppd); - parameter DEFAULT_SPEED = 32'd2814750; // 100 Mhz - parameter [0:0] OPT_TIMER = 1'b1; - parameter [0:0] OPT_STOPWATCH = 1'b1; - parameter [0:0] OPT_ALARM = 1'b1; - parameter [0:0] OPT_FIXED_SPEED = 1'b1; - - input wire i_clk, i_reset; - // - input wire i_wb_cyc, i_wb_stb, i_wb_we; - input wire [2:0] i_wb_addr; - input wire [31:0] i_wb_data; - input wire [3:0] i_wb_sel; - // - output reg o_wb_ack; - output wire o_wb_stall; - output reg [31:0] o_wb_data; - output wire o_interrupt, o_pps, o_ppd; + o_ppd + // }}} + ); + // Signal declarations + // {{{ reg [31:0] ckspeed; wire [21:0] clock_data; @@ -82,7 +86,18 @@ module rtclight(i_clk, i_reset, reg ck_wr, tm_wr, al_wr, wr_zero; reg [31:0] wr_data; reg [2:0] wr_valid; + wire tm_int, al_int; + reg ck_carry; + reg [39:0] ck_counter; + wire ck_pps, ck_ppd; + reg ck_prepps; + reg [7:0] ck_sub; + wire sp_sel; + // }}} + + // ck_wr, tm_wr, al_wr + // {{{ initial ck_wr = 1'b0; initial tm_wr = 1'b0; initial al_wr = 1'b0; @@ -98,7 +113,10 @@ module rtclight(i_clk, i_reset, //sw_wr<=((i_wb_stb)&&(i_wb_addr==3'b010)&&(i_wb_we)); al_wr <= ((i_wb_stb)&&(i_wb_addr==3'b011)&&(i_wb_we)); end + // }}} + // wr_data, wr_valid, wr_zero + // {{{ always @(posedge i_clk) begin wr_data <= i_wb_data; @@ -110,31 +128,46 @@ module rtclight(i_clk, i_reset, &&(i_wb_data[21:16] <= 6'h23); wr_zero <= (i_wb_data[23:0]==0); end + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Sub-clock handling, PPS generation + // {{{ + //////////////////////////////////////////////////////////////////////// + // - wire tm_int, al_int; - - reg ck_carry; - reg [39:0] ck_counter; initial ck_carry = 1'b0; initial ck_counter = 40'h00; always @(posedge i_clk) { ck_carry, ck_counter } <= ck_counter + { 8'h00, ckspeed }; - wire ck_pps, ck_ppd; - reg ck_prepps; - reg [7:0] ck_sub; assign ck_pps = (ck_carry)&&(ck_prepps); assign o_pps = ck_pps; + always @(posedge i_clk) begin if (ck_carry) ck_sub <= ck_sub + 8'h1; ck_prepps <= (ck_sub == 8'hff); end - + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Bare clock + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // rtcbare clock(i_clk, i_reset, ck_pps, ck_wr, wr_data[21:0], wr_valid, clock_data, ck_ppd); - + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Timer + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // generate if (OPT_TIMER) begin rtctimer #(.LGSUBCK(8)) @@ -150,6 +183,14 @@ module rtclight(i_clk, i_reset, assign unused_timer = tm_wr; // Verilator lint_on UNUSED end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Stopwatch + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // generate if (OPT_STOPWATCH) begin @@ -174,6 +215,14 @@ module rtclight(i_clk, i_reset, assign sw_running = 0; end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Alarm + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // generate if (OPT_ALARM) begin @@ -191,6 +240,14 @@ module rtclight(i_clk, i_reset, assign unused_alarm = al_wr; // Verilator lint_on UNUSED end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Clock speedx control + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // // // The ckspeed register is equal to 2^48 divded by the number of @@ -200,7 +257,6 @@ module rtclight(i_clk, i_reset, // device. Further, because this is only the lower 32 bits of a // 48 bit counter per seconds, the clock jitter is kept below // 1 part in 65 thousand. - wire sp_sel; generate if (!OPT_FIXED_SPEED) begin : ADJUSTABLE_CLOCK_RATE @@ -218,13 +274,14 @@ module rtclight(i_clk, i_reset, ckspeed = DEFAULT_SPEED; end endgenerate - - assign o_interrupt = tm_int || al_int; - - // A once-per day strobe, on the last second of the day so that the - // the next clock is the first clock of the day. This is useful for - // connecting this module to a year/month/date date/calendar module. - assign o_ppd = (ck_ppd); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Bus returns + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // assign o_wb_stall = 1'b0; @@ -236,21 +293,34 @@ module rtclight(i_clk, i_reset, o_wb_ack <= i_wb_stb; always @(posedge i_clk) - case(i_wb_addr[2:0]) - 3'b000: o_wb_data <= { 10'h0, clock_data }; - 3'b001: o_wb_data <= timer_data; - 3'b010: o_wb_data <= { sw_running, stopwatch_data }; - 3'b011: o_wb_data <= alarm_data; - 3'b100: o_wb_data <= ckspeed; - default: o_wb_data <= 32'h000; - endcase + case(i_wb_addr[2:0]) + 3'b000: o_wb_data <= { 10'h0, clock_data }; + 3'b001: o_wb_data <= timer_data; + 3'b010: o_wb_data <= { sw_running, stopwatch_data }; + 3'b011: o_wb_data <= alarm_data; + 3'b100: o_wb_data <= ckspeed; + default: o_wb_data <= 32'h000; + endcase + // }}} + + // o_ppd + // {{{ + // A once-per day strobe, on the last second of the day so that the + // the next clock is the first clock of the day. This is useful for + // connecting this module to a year/month/date date/calendar module. + assign o_ppd = (ck_ppd); + // }}} + + assign o_interrupt = tm_int || al_int; + // Make verilator hapy + // {{{ // verilator lint_off UNUSED wire unused; assign unused = &{ 1'b0, sp_sel, i_wb_cyc, wr_data[31:25], i_wb_sel[3] }; // verilator lint_on UNUSED - + // }}} `ifdef FORMAL // `ifdef RTCLIGHT diff --git a/rtl/rtc/rtcstopwatch.v b/rtl/rtc/rtcstopwatch.v index a770b09..370c0ec 100644 --- a/rtl/rtc/rtcstopwatch.v +++ b/rtl/rtc/rtcstopwatch.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: rtcstopwatch.v -// +// {{{ // Project: A Wishbone Controlled Real--time Clock Core, w/ GPS synch // // Purpose: Implement a stop watch in BCD. @@ -10,11 +10,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -27,8 +27,9 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // // @@ -36,24 +37,31 @@ // // `default_nettype none -// -module rtcstopwatch(i_clk, i_reset, i_ckstep, - i_clear, i_start, i_stop, o_value, o_running); - // - input wire i_clk, i_reset; - // - input wire [31:0] i_ckstep; - input wire i_clear, i_start, i_stop; - output wire [30:0] o_value; - output wire o_running; +// }}} +module rtcstopwatch( + // {{{ + input wire i_clk, i_reset, + // + input wire [31:0] i_ckstep, + input wire i_clear, i_start, i_stop, + output wire [30:0] o_value, + output wire o_running + // }}} + ); + // Signal declarations + // {{{ reg [30:0] counter; reg [45:0] sw_subticks; reg [36:0] sw_step; reg [13:0] last_step; reg [6:0] sw_carry; reg sw_ppms, carry, sw_running; + reg [30:0] next_sw; + // }}} + // i_ckstep + // {{{ // i_ckstep is the bottom 32 bits of a 48 counter step that rolls over // once per second. If we multiply by 100, we'll then have a 48 bit // counter step that will roll over once every 10 milliseconds. @@ -63,10 +71,13 @@ module rtcstopwatch(i_clk, i_reset, i_ckstep, sw_step <= { 1'b0, i_ckstep, 4'h0 } + { 2'b0, i_ckstep, 3'h0 } + { 5'h0, i_ckstep }; + // }}} always @(posedge i_clk) last_step <= sw_step[36:23]; + // sw_ppms, sw_subticks, carry + // {{{ initial sw_ppms = 0; initial sw_subticks = 0; initial carry = 0; @@ -83,6 +94,7 @@ module rtcstopwatch(i_clk, i_reset, i_ckstep, + {{(23){1'b0}}, carry }; end else sw_ppms <= 1'b0; + // }}} // // Stopwatch functionality @@ -95,7 +107,8 @@ module rtcstopwatch(i_clk, i_reset, i_ckstep, // will only clear it if it was already stopped. // - reg [30:0] next_sw; + // next_sw + // {{{ initial next_sw = 0; always @(posedge i_clk) if (i_reset || i_clear) @@ -167,30 +180,47 @@ module rtcstopwatch(i_clk, i_reset, i_ckstep, if (sw_carry[6]) next_sw[30:28] <= counter[30:28] + 1'b1; end + // }}} - + // counter + // {{{ initial counter = 31'h00000; always @(posedge i_clk) if (i_reset || i_clear) counter <= 0; else if ((sw_ppms)&&(sw_running)) counter <= next_sw; + // }}} + // sw_running + // {{{ initial sw_running = 0; always @(posedge i_clk) if (i_reset || i_stop) sw_running <= 1'b0; else if (i_start) sw_running <= 1'b1; + // }}} assign o_value = counter; assign o_running = sw_running; // Make verilator happy + // {{{ // verilator lint_off UNUSED - wire [2:0] unused; - assign unused = sw_step[2:0]; + wire unused; + assign unused = &{ 1'b0, sw_step[2:0] }; // verilator lint_on UNUSED + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL `ifdef STOPWATCH `define ASSUME assume @@ -246,4 +276,5 @@ module rtcstopwatch(i_clk, i_reset, i_ckstep, `ASSERT(!sw_ppms); `endif +// }}} endmodule diff --git a/rtl/rtc/rtctimer.v b/rtl/rtc/rtctimer.v index 232612d..1ab3692 100644 --- a/rtl/rtc/rtctimer.v +++ b/rtl/rtc/rtctimer.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: rtctimer.v -// +// {{{ // Project: A Wishbone Controlled Real--time Clock Core, w/ GPS synch // // Purpose: Implements a count down timer @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,34 +28,37 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // // `default_nettype none -// -module rtctimer(i_clk, i_reset, - i_sub_ck, i_wr, i_data, i_valid, i_zero, - o_data, o_interrupt); - parameter LGSUBCK = 2; - parameter [0:0] OPT_PREVALIDATED_INPUT = 1'b0; - parameter [21:0] OPT_FIXED_INTERVAL = 0; - // - input wire i_clk, i_reset; - // - input wire i_sub_ck; - // - input wire i_wr; - input wire [24:0] i_data; - input wire [2:0] i_valid; - input wire i_zero; - output wire [31:0] o_data; - output wire o_interrupt; - +// }}} +module rtctimer #( + // {{{ + parameter LGSUBCK = 2 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // + input wire i_sub_ck, + // + input wire i_wr, + input wire [24:0] i_data, + input wire [2:0] i_valid, + input wire i_zero, + output wire [31:0] o_data, + output wire o_interrupt + // }}} + ); + + // Signal declarations + // {{{ reg [23:0] bcd_timer; // wire [6:0] bcd_hours, bcd_minutes, bcd_seconds; // assign bcd_seconds = bcd_timer[ 6:0]; @@ -67,6 +70,13 @@ module rtctimer(i_clk, i_reset, reg [4:0] tmr_carry, pre_tmr_carry; reg last_tick; + reg tm_pre_pps, tm_int, tm_running, tm_alarm; + wire tm_stopped, tm_pps; + reg [(LGSUBCK-1):0] tm_sub; + // }}} + + // pre_tmr_carry, tmr_carry, next_timer + // {{{ initial tmr_carry = 0; initial next_timer = 0; always @(posedge i_clk) @@ -86,6 +96,7 @@ module rtctimer(i_clk, i_reset, // Keep unused bits at zero next_timer <= 24'h00; // Seconds + // {{{ if (tmr_carry[0]) next_timer[3:0] <= 4'h9; else @@ -97,8 +108,10 @@ module rtctimer(i_clk, i_reset, next_timer[ 6: 4] <= (bcd_timer[ 6: 4]-3'h1); else next_timer[6:4] <= bcd_timer[6:4]; + // }}} // Minutes + // {{{ if (tmr_carry[2]) next_timer[11:8] <= 4'h9; else if (tmr_carry[1]) @@ -111,8 +124,10 @@ module rtctimer(i_clk, i_reset, next_timer[14:12] <= (bcd_timer[14:12]-3'h1); else next_timer[14:12] <= bcd_timer[14:12]; + // }}} // Hours + // {{{ if (tmr_carry[4]) next_timer[19:16] <= 4'h9; else if (tmr_carry[3]) @@ -124,21 +139,24 @@ module rtctimer(i_clk, i_reset, next_timer[23:20] <= (bcd_timer[23:20]-4'h1); else next_timer[23:20] <= bcd_timer[23:20]; + // }}} end + // }}} - reg tm_pre_pps, tm_int, tm_running, tm_alarm; - wire tm_stopped, tm_pps; assign tm_stopped = !tm_running; - reg [(LGSUBCK-1):0] tm_sub; - + // tm_sub + // {{{ initial tm_sub = 0; always @(posedge i_clk) if ((i_reset)||((i_wr)&&(!tm_running)&&(&i_valid)&&(!i_zero))) tm_sub <= 0; else if ((i_sub_ck)&&(tm_running)) tm_sub <= tm_sub + 1; + // }}} + // tm_pre_pps, tm_pps + // {{{ initial tm_pre_pps = 1'b0; always @(posedge i_clk) if ((i_reset)||((i_wr)&&(!tm_running))) @@ -149,12 +167,14 @@ module rtctimer(i_clk, i_reset, tm_pre_pps <= (&tm_sub); assign tm_pps = (tm_pre_pps)&&(i_sub_ck); + // }}} + // bcd_timer, tm_int, tm_running, tm_alarm + // {{{ initial bcd_timer = 24'h00; initial tm_int = 1'b0; initial tm_running = 1'b0; initial tm_alarm = 1'b0; - always @(posedge i_clk) if (i_reset) begin @@ -199,16 +219,27 @@ module rtctimer(i_clk, i_reset, tm_alarm <= 1'b0; end end + // }}} assign o_interrupt = tm_int; assign o_data = { 6'h00, tm_alarm, tm_running, bcd_timer }; // Make Verilator happy + // {{{ // verilator lint_off UNUSED - // wire [6:0] unused; - // assign unused = i_data[31:25]; + // wire unused; + // assign unused = &{ 1'b0, i_data[31:25] }; // verilator lint_on UNUSED - + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL `ifdef RTCTIMER `define ASSUME assume @@ -329,4 +360,5 @@ module rtctimer(i_clk, i_reset, if ((f_past_valid)&&(!$past(i_reset))&&($past(tm_alarm))) cover(!tm_alarm); `endif +// }}} endmodule diff --git a/rtl/sdspi/llsdspi.v b/rtl/sdspi/llsdspi.v index 50b6c40..fc8ebab 100644 --- a/rtl/sdspi/llsdspi.v +++ b/rtl/sdspi/llsdspi.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: llsdspi.v -// +// {{{ // Project: SD-Card controller, using a shared SPI interface // // Purpose: This file implements the "lower-level" interface to the @@ -62,11 +62,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2016-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2016-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -79,70 +79,74 @@ // with this program. (It's in the $(ROOT)/doc directory, run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} `default_nettype none // -module llsdspi(i_clk, i_reset, i_speed, i_cs, i_stb, i_byte, - o_cs_n, o_sclk, o_mosi, i_miso, - o_stb, o_byte, o_idle, i_bus_grant); - parameter SPDBITS = 7, - // Minimum startup clocks - STARTUP_CLOCKS = 150, - // System clocks to wait before the startup clock - // sequence - POWERUP_IDLE = 1000; - // - // This core was originally developed for a shared SPI bus - // implementation on a XuLA2-LX25 board--one that shared flash with - // the SD-card. Few cards support such an implementation any more, - // since per protocol CS high (inactive) and clock and data pins - // toggling could well put the SD card into it's SD mode instead of - // SPI mode. - parameter [0:0] OPT_SPI_ARBITRATION = 1'b0; - // - // - localparam [0:0] CSN_ON_STARTUP = 1'b1; - // - // The MOSI INACTIVE VALUE *MUST* be 1'b1 to be compliant - localparam [0:0] MOSI_INACTIVE_VALUE = 1'b1; - // - // Normally, an SPI transaction shuts the clock down when finished. - // If OPT_CONTINUOUS_CLOCK is set, the clock will be continuous. - // This also means that any driving program must be ready when the - // SDSPI is idle. - parameter [0:0] OPT_CONTINUOUS_CLOCK = 1'b0; - - // - input wire i_clk, i_reset; - // Parameters/setup - input wire [(SPDBITS-1):0] i_speed; - // The incoming interface - input wire i_cs; - input wire i_stb; - input wire [7:0] i_byte; - // The actual SPI interface - output reg o_cs_n, o_sclk, o_mosi; - input wire i_miso; - // The outgoing interface - output reg o_stb; - output reg [7:0] o_byte; - output reg o_idle; - // And whether or not we actually own the interface (yet) - input wire i_bus_grant; - +module llsdspi #( + // {{{ + parameter SPDBITS = 7, + // Minimum startup clocks + STARTUP_CLOCKS = 150, + // System clocks to wait before the startup + // clock sequence + POWERUP_IDLE = 1000, + // + // This core was originally developed for a shared SPI bus + // implementation on a XuLA2-LX25 board--one that shared flash + // with the SD-card. Few cards support such an implementation + // any more, since per protocol CS high (inactive) and clock + // and data pins toggling could well put the SD card into it's + // SD mode instead of SPI mode. + parameter [0:0] OPT_SPI_ARBITRATION = 1'b0, + // + // + localparam [0:0] CSN_ON_STARTUP = 1'b1, + // + // The MOSI INACTIVE VALUE *MUST* be 1'b1 to be compliant + localparam [0:0] MOSI_INACTIVE_VALUE = 1'b1, + // + // Normally, an SPI transaction shuts the clock down when + // finished. If OPT_CONTINUOUS_CLOCK is set, the clock will be + // continuous. This also means that any driving program must + // be ready when the SDSPI is idle. + parameter [0:0] OPT_CONTINUOUS_CLOCK = 1'b0 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Parameters/setup + input wire [(SPDBITS-1):0] i_speed, + // The incoming interface + input wire i_cs, + input wire i_stb, + input wire [7:0] i_byte, + // The actual SPI interface + output reg o_cs_n, o_sclk, o_mosi, + input wire i_miso, + // The outgoing interface + output reg o_stb, + output reg [7:0] o_byte, + output reg o_idle, + // And whether or not we actually own the interface (yet) + input wire i_bus_grant + // }}} + ); + + // Signal / localparam declarations + // {{{ localparam [3:0] LLSDSPI_IDLE = 4'h0, LLSDSPI_HOTIDLE = 4'h1, LLSDSPI_WAIT = 4'h2, LLSDSPI_START = 4'h3, LLSDSPI_END = 4'hb; -// + // reg r_z_counter; reg [(SPDBITS-1):0] r_clk_counter; reg r_idle; @@ -152,14 +156,16 @@ module llsdspi(i_clk, i_reset, i_speed, i_cs, i_stb, i_byte, reg restart_counter; wire bus_grant; + reg startup_hold, powerup_hold; + // }}} assign bus_grant = (OPT_SPI_ARBITRATION ? i_bus_grant : 1'b1); - reg startup_hold, powerup_hold; - + // Wait for power up + // {{{ generate if (POWERUP_IDLE > 0) begin : WAIT_FOR_POWERUP - + // {{{ localparam POWERUP_BITS = $clog2(POWERUP_IDLE); reg [POWERUP_BITS-1:0] powerup_counter; @@ -182,9 +188,13 @@ module llsdspi(i_clk, i_reset, i_speed, i_cs, i_stb, i_byte, always @(*) powerup_hold = 0; end endgenerate + // }}} + // Send a minimum number of start up clocks after power up + // {{{ generate if (STARTUP_CLOCKS > 0) begin : WAIT_FOR_STARTUP + // {{{ localparam STARTUP_BITS = $clog2(STARTUP_CLOCKS); reg [STARTUP_BITS-1:0] startup_counter; @@ -210,22 +220,29 @@ module llsdspi(i_clk, i_reset, i_speed, i_cs, i_stb, i_byte, if (startup_counter > 0) assert(startup_hold); `endif + // }}} end else begin always @(*) startup_hold = 0; end endgenerate + // }}} assign byte_accepted = (i_stb)&&(o_idle); //////////////////////////////////////////////////////////////////////// // // Clock divider and speed control + // {{{ + //////////////////////////////////////////////////////////////////////// + // // initial r_clk_counter = 0; initial r_z_counter = 1'b1; + // restart_counter + // {{{ always @(*) if (OPT_CONTINUOUS_CLOCK || powerup_hold) restart_counter = !powerup_hold; @@ -246,7 +263,10 @@ module llsdspi(i_clk, i_reset, i_speed, i_cs, i_stb, i_byte, else restart_counter = !r_idle; end + // }}} + // r_clk_counter, r_z_counter + // {{{ always @(posedge i_clk) begin if (!r_z_counter) @@ -259,11 +279,19 @@ module llsdspi(i_clk, i_reset, i_speed, i_cs, i_stb, i_byte, r_z_counter <= (i_speed == 0); end end + // }}} + // }}} //////////////////////////////////////////////////////////////////////// // // Control o_stb, o_cs_n, and o_mosi + // {{{ + //////////////////////////////////////////////////////////////////////// // + // + + // o_cs_n, r_state + // {{{ initial o_cs_n = CSN_ON_STARTUP; initial r_state = LLSDSPI_IDLE; always @(posedge i_clk) @@ -307,15 +335,24 @@ module llsdspi(i_clk, i_reset, i_speed, i_cs, i_stb, i_byte, if (startup_hold) o_cs_n <= 1; end + // }}} + // r_ireg + // {{{ always @(posedge i_clk) if (r_z_counter && !o_sclk) r_ireg <= { r_ireg[6:0], i_miso }; + // }}} + // o_byte + // {{{ always @(posedge i_clk) if (r_z_counter && o_sclk && r_state == LLSDSPI_END) o_byte <= r_ireg; + // }}} + // r_idle + // {{{ initial r_idle = 0; always @(posedge i_clk) if (startup_hold || i_reset) @@ -332,7 +369,10 @@ module llsdspi(i_clk, i_reset, i_speed, i_cs, i_stb, i_byte, else r_idle <= 1'b0; end + // }}} + // o_sclk + // {{{ initial o_sclk = 1; always @(posedge i_clk) if (i_reset) @@ -345,7 +385,10 @@ module llsdspi(i_clk, i_reset, i_speed, i_cs, i_stb, i_byte, && (startup_hold || (i_cs && !o_cs_n) || !o_sclk)) o_sclk <= (r_state == LLSDSPI_WAIT) || !o_sclk; end + // }}} + // r_byte, o_mosi + // {{{ initial r_byte = -1; initial o_mosi = MOSI_INACTIVE_VALUE; always @(posedge i_clk) @@ -374,14 +417,20 @@ module llsdspi(i_clk, i_reset, i_speed, i_cs, i_stb, i_byte, o_mosi <= MOSI_INACTIVE_VALUE; end end + // }}} + // o_stb + // {{{ initial o_stb = 1'b0; always @(posedge i_clk) if (i_reset || startup_hold || !i_cs || !r_z_counter || !o_sclk) o_stb <= 1'b0; else o_stb <= (r_state >= LLSDSPI_END); + // }}} + // o_idle + // {{{ always @(*) begin if (OPT_CONTINUOUS_CLOCK) @@ -389,9 +438,18 @@ module llsdspi(i_clk, i_reset, i_speed, i_cs, i_stb, i_byte, else o_idle = (r_idle)&&(r_z_counter); end - + // }}} + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL -`endif +`endif // FORMAL +// }}} endmodule - - diff --git a/rtl/sdspi/sdspi.v b/rtl/sdspi/sdspi.v index d053553..0922f27 100644 --- a/rtl/sdspi/sdspi.v +++ b/rtl/sdspi/sdspi.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: sdspi.v -// +// {{{ // Project: SD-Card controller, using a shared SPI interface // // Purpose: SD Card controller, using SPI interface with the card and @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2016-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2016-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,110 +30,111 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module sdspi(i_clk, i_sd_reset, +// }}} +module sdspi #( + // {{{ + parameter [0:0] OPT_CARD_DETECT = 1'b1, + // + // LGFIFOLN + // {{{ + // LGFIFOLN defines the size of the internal memory in words. + // An LGFIFOLN of 7 is appropriate for a 2^(7+2)=512 byte FIFO + parameter LGFIFOLN = 7, + // }}} + parameter POWERUP_IDLE = 1000, + // STARTUP_CLOCKS + // {{{ + // Many SD-Cards require a minimum number of SPI clocks to get + // them started. STARTUP_CLOCKS defines this number. Set this + // to zero if you don't want to use this initialization + // sequence. + parameter STARTUP_CLOCKS = 75, + // }}} + // CKDIV_BITS + // {{{ + // For my first design, using an 80MHz clock, 7 bits to the + // clock divider was plenty. Now that I'm starting to use + // faster and faster designs, it becomes important to + // parameterize the number of bits in the clock divider. More + // than 8, however, and the interface will need to change. + parameter CKDIV_BITS = 8, + // }}} + // INITIAL_CLKDIV + // {{{ + // The SPI frequency is given by the system clock frequency + // divided by a (clock_divider + 1). INITIAL_CLKDIV provides + // an initial value for this clock divider. + parameter [CKDIV_BITS-1:0] INITIAL_CLKDIV = 8'h7c, + // }}} + // OPT_SPI_ARBITRATION + // {{{ + // When I originally built this SDSPI controller, it was for an + // environment where the SPI was shared. Doing this requires + // feedback from an arbiter, to know when one SPI device has + // the bus or not. This feedback is provided in i_bus_grant. + // If you don't have an arbiter, just set i_bus_grant to the + // constant 1'b1 and set OPT_SPI_ARBITRATION to 1'b0 to remove + // this extra logic. + parameter [0:0] OPT_SPI_ARBITRATION = 1'b0, + // }}} + // + // + parameter [0:0] OPT_EXTRA_WB_CLOCK = 1'b0, + // + // + // + localparam AW = 2, DW = 32 + // }}} + ) ( + // {{{ + input wire i_clk, i_sd_reset, // Wishbone interface - i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, i_wb_sel, - o_wb_stall, o_wb_ack, o_wb_data, + // {{{ + input wire i_wb_cyc, i_wb_stb, i_wb_we, + input wire [AW-1:0] i_wb_addr, + input wire [DW-1:0] i_wb_data, + input wire [DW/8-1:0] i_wb_sel, + output wire o_wb_stall, + output reg o_wb_ack, + output reg [DW-1:0] o_wb_data, + // }}} // SDCard interface - o_cs_n, o_sck, o_mosi, i_miso, i_card_detect, + // {{{ + output wire o_cs_n, o_sck, o_mosi, + input wire i_miso, i_card_detect, + // }}} // Our interrupt - o_int, - // And whether or not we own the bus - i_bus_grant, + output reg o_int, + // .. and whether or not we can use the SPI port + input wire i_bus_grant, // And some wires for debugging it all - o_debug); - parameter [0:0] OPT_CARD_DETECT = 1'b1; - // - // LGFIFOLN defines the size of the internal memory in words. An - // LGFIFOLN of 7 is appropriate for a 2^(7+2)=512 byte FIFO - parameter LGFIFOLN = 7; - parameter POWERUP_IDLE = 1000; - // - // Many SD-Cards require a minimum number of SPI clocks to get them - // started. STARTUP_CLOCKS defines this number. Set this to zero - // if you don't want to use this initialization sequence. - parameter STARTUP_CLOCKS = 75; - // - // For my first design, using an 80MHz clock, 7 bits to the clock - // divider was plenty. Now that I'm starting to use faster and faster - // designs, it becomes important to parameterize the number of bits - // in the clock divider. More than 8, however, and the interface - // will need to change. - parameter CKDIV_BITS = 8; - // - // The SPI frequency is given by the system clock frequency divided - // by a (clock_divider + 1). INITIAL_CLKDIV provides an initial value - // for this clock divider. - parameter [CKDIV_BITS-1:0] INITIAL_CLKDIV = 8'h7c; - // - // When I originally built this SDSPI controller, it was for an - // environment where the SPI was shared. Doing this requires feedback - // from an arbiter, to know when one SPI device has the bus or not. - // This feedback is provided in i_bus_grant. If you don't have an - // arbiter, just set i_bus_grant to the constant 1'b1 and set - // OPT_SPI_ARBITRATION to 1'b0 to remove this extra logic. - parameter [0:0] OPT_SPI_ARBITRATION = 1'b0; - // - // - parameter [0:0] OPT_EXTRA_WB_CLOCK = 1'b0; - // - // - // - localparam AW = 2, DW = 32; + // + output reg [DW-1:0] o_debug + // }}} + ); + + // Signal / parameter declarations + // {{{ localparam [1:0] SDSPI_CMD_ADDRESS = 2'b00, SDSPI_DAT_ADDRESS = 2'b01, SDSPI_FIFO_A_ADDR = 2'b10, SDSPI_FIFO_B_ADDR = 2'b11; - localparam [1:0] SDSPI_EXPECT_R1 = 2'b00, - SDSPI_EXPECT_R1B = 2'b01, - SDSPI_EXPECT_R3 = 2'b10; - - localparam [2:0] SDSPI_RSP_NONE = 3'h0, // No response yet from device - SDSPI_RSP_BSYWAIT = 3'h1, // R1b, wait for device to send nonzero - SDSPI_RSP_GETWORD = 3'h2, // Get 32-bit data word from device - SDSPI_RSP_GETTOKEN = 3'h4, // Write to device, read from FIFO, wait for completion token - SDSPI_RSP_WAIT_WHILE_BUSY =3'h5,// Read from device - SDSPI_RSP_RDCOMPLETE = 3'h6, - SDSPI_RSP_WRITING = 3'h7; // Read from device, write into FIFO localparam BLKBASE = 16; - // - // - input wire i_clk, i_sd_reset; - // - input wire i_wb_cyc, i_wb_stb, i_wb_we; - input wire [AW-1:0] i_wb_addr; - input wire [DW-1:0] i_wb_data; - input wire [DW/8-1:0] i_wb_sel; - output wire o_wb_stall; - output reg o_wb_ack; - output reg [DW-1:0] o_wb_data; - // - output wire o_cs_n, o_sck, o_mosi; - input wire i_miso, i_card_detect; - // The interrupt - output reg o_int; - // .. and whether or not we can use the SPI port - input wire i_bus_grant; - // - output reg [DW-1:0] o_debug; - reg dbg_trigger; // // Command register bit definitions // localparam CARD_REMOVED_BIT= 18, - CRCERR_BIT = 16, + // CRCERR_BIT = 16, ERR_BIT = 15, FIFO_ID_BIT = 12, USE_FIFO_BIT = 11, @@ -143,15 +144,78 @@ module sdspi(i_clk, i_sd_reset, // reg r_cmd_busy; + reg dbg_trigger; + wire wb_stb, write_stb, wb_cmd_stb, new_data; wire [AW-1:0] wb_addr; wire [DW-1:0] wb_data; reg [1:0] pipe_addr; reg dly_stb; + reg [31:0] fifo_a [0:((1<> 1; + // }}} + // crc_busy, crc_bit_counter + // {{{ initial crc_busy = 1'b0; initial crc_bit_counter = 20; always @(posedge i_clk) @@ -117,15 +138,19 @@ module spicmd(i_clk, i_reset, i_cmd_stb, i_cmd_type, i_cmd, i_cmd_data, o_busy, crc_bit_counter <= crc_bit_counter - 1; crc_busy <= (crc_bit_counter > 1); end + // }}} + // crc_shift_reg + // {{{ always @(posedge i_clk) if (!o_busy) crc_shift_reg <= { 2'b01, i_cmd, i_cmd_data }; else if (crc_busy) crc_shift_reg <= crc_shift_reg << 2; + // }}} - localparam CRC_POLYNOMIAL = 7'h09; // Was 8'h12 - reg [6:0] next_crc_byte; + // next_crc_byte + // {{{ always @(*) begin next_crc_byte = { crc_byte[6:1], 1'b0 }; @@ -136,14 +161,20 @@ module spicmd(i_clk, i_reset, i_cmd_stb, i_cmd_type, i_cmd, i_cmd_data, o_busy, else next_crc_byte = (next_crc_byte<<1); end + // }}} + // crc_byte + // {{{ initial crc_byte = 0; always @(posedge i_clk) if (!o_busy) crc_byte <= 1; else if (crc_busy) crc_byte <= { next_crc_byte, 1'b1 }; + // }}} + // rx_r1_byte, rx_counter, rxvalid, rx_check_busy + // {{{ initial rxvalid = 1'b0; initial rx_counter = 1; always @(posedge i_clk) @@ -167,14 +198,20 @@ module spicmd(i_clk, i_reset, i_cmd_stb, i_cmd_type, i_cmd, i_cmd_data, o_busy, if (rx_r1_byte && i_ll_byte != 0) rx_check_busy <= 1'b0; end + // }}} + // o_rxvalid + // {{{ initial o_rxvalid = 0; always @(posedge i_clk) if (i_reset || !o_busy) o_rxvalid <= 0; else if (rxvalid && !rx_check_busy) o_rxvalid <= 1; + // }}} + // o_response + // {{{ initial o_response = -1; always @(posedge i_clk) if (!o_busy) @@ -186,7 +223,16 @@ module spicmd(i_clk, i_reset, i_cmd_stb, i_cmd_type, i_cmd, i_cmd_data, o_busy, else o_response[31:0] <= { o_response[23:0], i_ll_byte }; end - + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL `endif endmodule diff --git a/rtl/sdspi/spirxdata.v b/rtl/sdspi/spirxdata.v index 0b7a815..d0760bc 100644 --- a/rtl/sdspi/spirxdata.v +++ b/rtl/sdspi/spirxdata.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: spirxdata.v -// +// {{{ // Project: SD-Card controller, using a shared SPI interface // // Purpose: To handle all of the processing associated with receiving data @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2019-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2019-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,41 +30,43 @@ // with this program. (It's in the $(ROOT)/doc directory, run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module spirxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, - i_ll_stb, i_ll_byte, - o_write, o_addr, o_data, - o_rxvalid, o_response); - parameter DW = 32, AW = 8; - localparam CRC_POLYNOMIAL = 16'h1021; - // - input wire i_clk, i_reset; - // - input wire i_start; - input wire [3:0] i_lgblksz; - input wire i_fifo; - output reg o_busy; - // - input wire i_ll_stb; - input wire [7:0] i_ll_byte; - // - output reg o_write; - output reg [AW-1:0] o_addr; - output reg [DW-1:0] o_data; - // - output reg o_rxvalid; - output reg [7:0] o_response; - - +// }}} +module spirxdata #( + // {{{ + parameter DW = 32, AW = 8, + localparam CRC_POLYNOMIAL = 16'h1021 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // + input wire i_start, + input wire [3:0] i_lgblksz, + input wire i_fifo, + output reg o_busy, + // + input wire i_ll_stb, + input wire [7:0] i_ll_byte, + // + output reg o_write, + output reg [AW-1:0] o_addr, + output reg [DW-1:0] o_data, + // + output reg o_rxvalid, + output reg [7:0] o_response + // }}} + ); + + // Signal declarations + // {{{ reg error_token, start_token, token, received_token, done, lastaddr; reg all_mem_written, lastdata; @@ -79,8 +81,11 @@ module spirxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, reg crc_err, crc_active; reg [2:0] fill; reg [23:0] gearbox; + reg [15:0] first_crc_data; + // }}} - + // error_token + // {{{ always @(*) begin error_token = 0; @@ -90,7 +95,10 @@ module spirxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, if (!i_ll_stb || received_token) error_token = 0; end + // }}} + // start_token + // {{{ always @(*) begin start_token = 0; @@ -100,21 +108,32 @@ module spirxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, if (!i_ll_stb || received_token) start_token = 0; end + // }}} + // token + // {{{ always @(*) token = (start_token || error_token); + // }}} + // done + // {{{ always @(*) done = (i_ll_stb && (crc_byte>1)); + // }}} - + // received_token + // {{{ initial received_token = 0; always @(posedge i_clk) if (i_reset || !o_busy) received_token <= 0; else if (token) received_token <= 1; + // }}} + // o_busy + // {{{ initial o_busy = 0; always @(posedge i_clk) if (i_reset) @@ -123,14 +142,20 @@ module spirxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, o_busy <= i_start; else if (error_token || done) o_busy <= 0; + // }}} + // o_rxvalid + // {{{ initial o_rxvalid = 0; always @(posedge i_clk) if (i_reset || !o_busy) o_rxvalid <= 0; else if (error_token || done) o_rxvalid <= 1; + // }}} + // o_response + // {{{ initial o_response = 0; always @(posedge i_clk) if (i_reset || !o_busy) @@ -139,7 +164,10 @@ module spirxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, o_response <= i_ll_byte; else if (done) o_response <= (crc_err || (crc_data[7:0] != i_ll_byte)) ? 8'h10 : 0; + // }}} + // o_write + // {{{ initial o_write = 0; always @(posedge i_clk) if (i_reset || !o_busy) @@ -148,18 +176,27 @@ module spirxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, o_write <= (&fill) && i_ll_stb; else o_write <= 0; + // }}} - initial o_write = 0; + // o_data + // {{{ + initial o_data = 0; always @(posedge i_clk) if (received_token && !all_mem_written) o_data <= { gearbox, i_ll_byte }; + // }}} + // o_addr + // {{{ always @(posedge i_clk) if (!o_busy) o_addr <= { i_fifo, {(AW-1){1'b0}} }; else if (o_write && !lastaddr) o_addr <= o_addr + 1; + // }}} + // fill + // {{{ initial fill = 0; always @(posedge i_clk) begin @@ -173,27 +210,39 @@ module spirxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, else if (i_ll_stb) fill <= { fill[1:0], 1'b1 }; end + // }}} + // lastdata + // {{{ always @(posedge i_clk) if (!o_busy) lastdata <= 0; else if (!lastdata) lastdata <= (lastaddr && (&fill)); + // }}} + // all_mem_written + // {{{ initial all_mem_written = 0; always @(posedge i_clk) if (i_reset || !o_busy) all_mem_written <= 0; else if (o_write && lastaddr) all_mem_written <= 1; + // }}} + // crc_byte + // {{{ initial crc_byte = 0; always @(posedge i_clk) if (i_reset || !o_busy) crc_byte <= 0; else if (i_ll_stb && lastaddr && lastdata) crc_byte <= crc_byte + 1; + // }}} + // lastaddr, r_lgblksz_m3 + // {{{ initial r_lgblksz_m3 = 0; initial lastaddr = 0; always @(posedge i_clk) @@ -215,16 +264,24 @@ module spirxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, default: lastaddr <= (&o_addr[6:1]); // 512 bytes endcase end + // }}} //////////////////////////////////////////////////////////////////////// // // CRC calculation + // {{{ + //////////////////////////////////////////////////////////////////////// + // // + // new_data_byte + // {{{ always @(*) new_data_byte = (i_ll_stb && !all_mem_written); + // }}} - + // crc_fill, crc_active + // {{{ initial crc_fill = 0; initial crc_active = 0; always @(posedge i_clk) @@ -243,16 +300,20 @@ module spirxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, else crc_active <= (crc_fill > 1); end + // }}} + // crc_gearbox + // {{{ always @(posedge i_clk) if (!crc_active) crc_gearbox <= i_ll_byte; else crc_gearbox <= { crc_gearbox[8-3:0], 2'b00 }; + // }}} - reg [15:0] first_crc_data; - + // first_crc_data, next_crc_data + // {{{ always @(*) begin first_crc_data = crc_data << 1;; @@ -265,14 +326,20 @@ module spirxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, else next_crc_data = (first_crc_data << 1); end + // }}} + // crc_data + // {{{ initial crc_data = 0; always @(posedge i_clk) if (!o_busy) crc_data <= 0; else if (crc_active) crc_data <= next_crc_data; + // }}} + // crc_err + // {{{ initial crc_err = 0; always @(posedge i_clk) if (i_reset || !o_busy) @@ -281,7 +348,18 @@ module spirxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, crc_err <= (crc_data[15:8] != i_ll_byte); // else if (i_ll_stb && (crc_byte == 2) // crc_err <= (crc_data[7:0] != i_ll_byte); - + // }}} + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL `endif // FORMAL +// }}} endmodule diff --git a/rtl/sdspi/spitxdata.v b/rtl/sdspi/spitxdata.v index e4da675..6d50036 100644 --- a/rtl/sdspi/spitxdata.v +++ b/rtl/sdspi/spitxdata.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: spitxdata.v -// +// {{{ // Project: SD-Card controller, using a shared SPI interface // // Purpose: To handle all of the processing associated with sending data @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2016-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2016-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,49 +28,53 @@ // with this program. (It's in the $(ROOT)/doc directory, run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // // `default_nettype none -// -module spitxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, - o_read, o_addr, i_data, - i_ll_busy, o_ll_stb, o_ll_byte, - i_ll_stb, i_ll_byte, o_rxvalid, o_response); - parameter DW = 32, AW = 8, RDDELAY = 2; - localparam CRC_POLYNOMIAL = 16'h1021; - // - input wire i_clk, i_reset; - // - input wire i_start; - input wire [3:0] i_lgblksz; - input wire i_fifo; - output reg o_busy; - // - output reg o_read; - output reg [AW-1:0] o_addr; - input wire [DW-1:0] i_data; - // - input wire i_ll_busy; - output reg o_ll_stb; - output wire [7:0] o_ll_byte; - // - input wire i_ll_stb; - input wire [7:0] i_ll_byte; - // - output reg o_rxvalid; - output reg [7:0] o_response; - +// }}} +module spitxdata #( + // {{{ + parameter DW = 32, AW = 8, RDDELAY = 2, + localparam CRC_POLYNOMIAL = 16'h1021 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // + input wire i_start, + input wire [3:0] i_lgblksz, + input wire i_fifo, + output reg o_busy, + // + output reg o_read, + output reg [AW-1:0] o_addr, + input wire [DW-1:0] i_data, + // + input wire i_ll_busy, + output reg o_ll_stb, + output wire [7:0] o_ll_byte, + // + input wire i_ll_stb, + input wire [7:0] i_ll_byte, + // + output reg o_rxvalid, + output reg [7:0] o_response + // }}} + ); - reg [RDDELAY-1:0] rdvalid; - reg [8+DW-1:0] gearbox; - reg [1+(DW/8)-1:0] fill; - reg crc_flag, crc_stb, data_read, all_mem_read; + // Signal declarations + // {{{ + reg [RDDELAY-1:0] rdvalid; + reg [8+DW-1:0] gearbox; + reg [1+(DW/8)-1:0] fill; + reg crc_flag, crc_stb, data_read, + all_mem_read; reg lastaddr, data_sent, received_token, all_idle; @@ -80,10 +84,18 @@ module spitxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, reg [15:0] crc_data; reg token; + reg [2:0] r_lgblksz_m3; + reg [15:0] next_crc_data; + // }}} + // token + // {{{ always @(*) token = (data_sent && i_ll_stb && i_ll_byte[0] &&!i_ll_byte[4]); + // }}} + // o_busy + // {{{ initial o_busy = 0; always @(posedge i_clk) if (i_reset) @@ -92,7 +104,10 @@ module spitxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, o_busy <= i_start; else if (all_idle && i_ll_stb && (&i_ll_byte)) o_busy <= 0; + // }}} + // o_rxvalid, received_token + // {{{ initial o_rxvalid = 0; initial received_token = 0; always @(posedge i_clk) @@ -102,29 +117,42 @@ module spitxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, { received_token, o_rxvalid } <= 2'b11; else o_rxvalid <= 0; + // }}} + // all_idle + // {{{ initial all_idle = 0; always @(posedge i_clk) if (i_reset || !o_busy) all_idle <= 0; else if (received_token && i_ll_stb && (&i_ll_byte)) all_idle <= 1'b1; + // }}} + // o_response + // {{{ always @(posedge i_clk) if (token) o_response <= i_ll_byte; + // }}} // o_read & o_addr // 0: arbited read request // 1: read data // 2: muxed read data + + // rdvalid + // {{{ initial rdvalid = 0; always @(posedge i_clk) if (i_reset || !o_busy) rdvalid <= 0; else rdvalid <= { rdvalid[RDDELAY-2:0], o_read }; + // }}} + // o_ll_stb + // {{{ initial o_ll_stb = 0; always @(posedge i_clk) if (i_reset || !o_busy) @@ -133,7 +161,10 @@ module spitxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, o_ll_stb <= 1; else if (data_read && !fill[4]) o_ll_stb <= 0; + // }}} + // fill + // {{{ initial fill = 0; always @(posedge i_clk) begin @@ -163,9 +194,12 @@ module spitxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, else if (!o_busy) fill <= (i_start) ? 5'h10 : 0; end + // }}} assign o_ll_byte = gearbox[39:32]; + // crc_stb, o_read + // {{{ initial { crc_stb, o_read } = 0; always @(posedge i_clk) if (i_reset || !o_busy) @@ -182,39 +216,55 @@ module spitxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, end end else { crc_stb, o_read } <= 0; + // }}} + // o_addr + // {{{ always @(posedge i_clk) if (!o_busy) o_addr <= { i_fifo, {(AW-1){1'b0}} }; else if (o_read && !lastaddr) o_addr[AW-2:0] <= o_addr[AW-2:0] + 1; + // }}} + // all_mem_read + // {{{ initial all_mem_read = 0; always @(posedge i_clk) if (i_reset || !o_busy) all_mem_read <= 0; else if (o_read && lastaddr) all_mem_read <= 1; + // }}} + // crc_flag + // {{{ initial crc_flag = 0; always @(posedge i_clk) if (i_reset || !o_busy) crc_flag <= 0; else if (crc_stb) crc_flag <= 1; + // }}} + // data_read + // {{{ always @(*) data_read = crc_flag; + // }}} + // data_sent + // {{{ initial data_sent = 0; always @(posedge i_clk) if (i_reset || !o_busy) data_sent <= 1'b0; else if (data_read && !fill[3] && o_ll_stb && !i_ll_busy) data_sent <= 1'b1; + // }}} - reg [2:0] r_lgblksz_m3; - + // r_lgblksz_m3, lastaddr + // {{{ initial r_lgblksz_m3 = 0; initial lastaddr = 0; always @(posedge i_clk) @@ -236,11 +286,18 @@ module spitxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, default: lastaddr <= (&o_addr[6:1]); // 512 bytes endcase end + // }}} //////////////////////////////////////////////////////////////////////// // // CRC calculation + // {{{ + //////////////////////////////////////////////////////////////////////// // + // + + // crc_fill + // {{{ initial crc_fill = 0; always @(posedge i_clk) if (i_reset || !o_busy) @@ -258,15 +315,19 @@ module spitxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, else crc_active <= (crc_fill > 1); end + // }}} + // crc_gearbox + // {{{ always @(posedge i_clk) if (!crc_active) crc_gearbox <= i_data; else crc_gearbox <= { crc_gearbox[DW-3:0], 2'b00 }; + // }}} - reg [15:0] next_crc_data; - + // next_crc_data + // {{{ always @(*) begin next_crc_data = crc_data << 1;; @@ -279,13 +340,27 @@ module spitxdata(i_clk, i_reset, i_start, i_lgblksz, i_fifo, o_busy, else next_crc_data = (next_crc_data << 1); end + // }}} + // crc_data + // {{{ always @(posedge i_clk) if (!o_busy) crc_data <= 0; else if (crc_active) crc_data <= next_crc_data; - + // }}} + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL `endif +// }}} endmodule diff --git a/rtl/skidbuffer.v b/rtl/skidbuffer.v index 3fdaf0d..0fc4d0e 100644 --- a/rtl/skidbuffer.v +++ b/rtl/skidbuffer.v @@ -1,8 +1,8 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: skidbuffer.v -// -// Project: WB2AXIPSP: bus bridges and other odds and ends +// {{{ +// Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: A basic SKID buffer. // @@ -55,69 +55,79 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2019-2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. // -// Copyright (C) 2019-2020, Gisselquist Technology, LLC -// -// This file is part of the WB2AXIP project. +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. // -// The WB2AXIP project contains free software and gateware, licensed under the -// Apache License, Version 2.0 (the "License"). You may not use this project, -// or this file, except in compliance with the License. You may obtain a copy -// of the License at +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html // -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -// License for the specific language governing permissions and limitations -// under the License. // //////////////////////////////////////////////////////////////////////////////// // // `default_nettype none -// -module skidbuffer(i_clk, i_reset, - i_valid, o_ready, i_data, - o_valid, i_ready, o_data); - parameter [0:0] OPT_LOWPOWER = 0; - parameter [0:0] OPT_OUTREG = 1; - // - parameter [0:0] OPT_PASSTHROUGH = 0; - parameter DW = 8; - input wire i_clk, i_reset; - input wire i_valid; - output reg o_ready; - input wire [DW-1:0] i_data; - output reg o_valid; - input wire i_ready; - output reg [DW-1:0] o_data; - - reg [DW-1:0] r_data; +// }}} +module skidbuffer #( + // {{{ + parameter [0:0] OPT_LOWPOWER = 0, + parameter [0:0] OPT_OUTREG = 1, + // + parameter [0:0] OPT_PASSTHROUGH = 0, + parameter DW = 8, + parameter [0:0] OPT_INITIAL = 1'b1 + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + input wire i_valid, + output wire o_ready, + input wire [DW-1:0] i_data, + output wire o_valid, + input wire i_ready, + output reg [DW-1:0] o_data + // }}} + ); + + wire [DW-1:0] w_data; generate if (OPT_PASSTHROUGH) - begin + begin : PASSTHROUGH + // {{{ + assign { o_valid, o_ready } = { i_valid, i_ready }; - always @(*) - o_ready = i_ready; - always @(*) - o_valid = i_valid; always @(*) if (!i_valid && OPT_LOWPOWER) o_data = 0; else o_data = i_data; - always @(*) - r_data = 0; - end else begin - // + assign w_data = 0; + // }}} + end else begin : LOGIC // We'll start with skid buffer itself - // + // {{{ reg r_valid; + reg [DW-1:0] r_data; - initial r_valid = 0; + // r_valid + // {{{ + initial if (OPT_INITIAL) r_valid = 0; always @(posedge i_clk) if (i_reset) r_valid <= 0; @@ -126,8 +136,11 @@ module skidbuffer(i_clk, i_reset, r_valid <= 1; else if (i_ready) r_valid <= 0; + // }}} - initial r_data = 0; + // r_data + // {{{ + initial if (OPT_INITIAL) r_data = 0; always @(posedge i_clk) if (OPT_LOWPOWER && i_reset) r_data <= 0; @@ -136,18 +149,28 @@ module skidbuffer(i_clk, i_reset, else if ((!OPT_LOWPOWER || !OPT_OUTREG || i_valid) && o_ready) r_data <= i_data; - always @(*) - o_ready = !r_valid; + assign w_data = r_data; + // }}} + + // o_ready + // {{{ + assign o_ready = !r_valid; + // }}} // // And then move on to the output port // if (!OPT_OUTREG) - begin - - always @(*) - o_valid = !i_reset && (i_valid || r_valid); - + begin : NET_OUTPUT + // Outputs are combinatorially determined from inputs + // {{{ + // o_valid + // {{{ + assign o_valid = !i_reset && (i_valid || r_valid); + // }}} + + // o_data + // {{{ always @(*) if (r_valid) o_data = r_data; @@ -155,16 +178,28 @@ module skidbuffer(i_clk, i_reset, o_data = i_data; else o_data = 0; - end else begin - - initial o_valid = 0; + // }}} + // }}} + end else begin : REG_OUTPUT + // Register our outputs + // {{{ + // o_valid + // {{{ + reg ro_valid; + + initial if (OPT_INITIAL) ro_valid = 0; always @(posedge i_clk) if (i_reset) - o_valid <= 0; + ro_valid <= 0; else if (!o_valid || i_ready) - o_valid <= (i_valid || r_valid); + ro_valid <= (i_valid || r_valid); - initial o_data = 0; + assign o_valid = ro_valid; + // }}} + + // o_data + // {{{ + initial if (OPT_INITIAL) o_data = 0; always @(posedge i_clk) if (OPT_LOWPOWER && i_reset) o_data <= 0; @@ -178,17 +213,61 @@ module skidbuffer(i_clk, i_reset, else o_data <= 0; end - end + // }}} + // }}} + end + // }}} end endgenerate + // Keep Verilator happy + // {{{ + // Verilator lint_off UNUSED + wire unused; + assign unused = &{ 1'b0, w_data }; + // Verilator lint_on UNUSED + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL +`ifdef SKIDBUFFER +`define ASSUME assume +`else +`define ASSUME assert +`endif + + reg f_past_valid; + + initial f_past_valid = 0; + always @(posedge i_clk) + f_past_valid <= 1; + + always @(*) + if (!f_past_valid) + assume(i_reset); + + //////////////////////////////////////////////////////////////////////// + // + // Incoming stream properties / assumptions + // {{{ + //////////////////////////////////////////////////////////////////////// + // + always @(posedge i_clk) + if (!f_past_valid) + begin + `ASSUME(!i_valid || !OPT_INITIAL); + end else if ($past(i_valid && !o_ready && !i_reset) && !i_reset) + `ASSUME(i_valid && $stable(i_data)); + `ifdef VERIFIC `define FORMAL_VERIFIC -`endif -`endif -// -`ifdef FORMAL_VERIFIC // Reset properties property RESET_CLEARS_IVALID; @(posedge i_clk) i_reset |=> !i_valid; @@ -204,79 +283,193 @@ module skidbuffer(i_clk, i_reset, `else assert property (IDATA_HELD_WHEN_NOT_READY); `endif +`endif + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Outgoing stream properties / assumptions + // {{{ + //////////////////////////////////////////////////////////////////////// + // generate if (!OPT_PASSTHROUGH) begin - assert property (@(posedge i_clk) - OPT_OUTREG && i_reset |=> o_ready && !o_valid); - - assert property (@(posedge i_clk) - !OPT_OUTREG && i_reset |-> !o_valid); + always @(posedge i_clk) + if (!f_past_valid) // || $past(i_reset)) + begin + // Following any reset, valid must be deasserted + assert(!o_valid || !OPT_INITIAL); + end else if ($past(o_valid && !i_ready && !i_reset) && !i_reset) + // Following any stall, valid must remain high and + // data must be preserved + assert(o_valid && $stable(o_data)); + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Other properties + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + generate if (!OPT_PASSTHROUGH) + begin // Rule #1: - // Once o_valid goes high, the data cannot change until the - // clock after i_ready - assert property (@(posedge i_clk) - disable iff (i_reset) - o_valid && !i_ready - |=> (o_valid && $stable(o_data))); + // If registered, then following any reset we should be + // ready for a new request + // {{{ + always @(posedge i_clk) + if (f_past_valid && $past(OPT_OUTREG && i_reset)) + assert(o_ready); + // }}} // Rule #2: // All incoming data must either go directly to the // output port, or into the skid buffer + // {{{ +`ifndef VERIFIC + always @(posedge i_clk) + if (f_past_valid && !$past(i_reset) && $past(i_valid && o_ready + && (!OPT_OUTREG || o_valid) && !i_ready)) + assert(!o_ready && r_data == $past(i_data)); +`else assert property (@(posedge i_clk) disable iff (i_reset) (i_valid && o_ready && (!OPT_OUTREG || o_valid) && !i_ready) |=> (!o_ready && r_data == $past(i_data))); +`endif + // }}} // Rule #3: // After the last transaction, o_valid should become idle + // {{{ if (!OPT_OUTREG) begin - - assert property (@(posedge i_clk) - disable iff (i_reset) - i_ready |=> (o_valid == i_valid)); - + // {{{ + always @(posedge i_clk) + if (f_past_valid && !$past(i_reset) && !i_reset + && $past(i_ready)) + begin + assert(o_valid == i_valid); + assert(!i_valid || (o_data == i_data)); + end + // }}} end else begin + // {{{ + always @(posedge i_clk) + if (f_past_valid && !$past(i_reset)) + begin + if ($past(i_valid && o_ready)) + assert(o_valid); - assert property (@(posedge i_clk) - disable iff (i_reset) - i_valid && o_ready |=> o_valid); - - assert property (@(posedge i_clk) - disable iff (i_reset) - !i_valid && o_ready && i_ready |=> !o_valid); - + if ($past(!i_valid && o_ready && i_ready)) + assert(!o_valid); + end + // }}} end + // }}} // Rule #4 - // Same thing, but this time for r_valid - assert property (@(posedge i_clk) - !o_ready && i_ready |=> o_ready); - + // Same thing, but this time for o_ready + // {{{ + always @(posedge i_clk) + if (f_past_valid && $past(!o_ready && i_ready)) + assert(o_ready); + // }}} + // If OPT_LOWPOWER is set, o_data and r_data both need to be + // zero any time !o_valid or !r_valid respectively + // {{{ if (OPT_LOWPOWER) begin - // - // If OPT_LOWPOWER is set, o_data and r_data both need - // to be zero any time !o_valid or !r_valid respectively - assert property (@(posedge i_clk) - (OPT_OUTREG || !i_reset) && !o_valid |-> o_data == 0); - - assert property (@(posedge i_clk) - o_ready |-> r_data == 0); - - // else - // if OPT_LOWPOWER isn't set, we can lower our - // logic count by not forcing these values to zero. - end + always @(*) + if ((OPT_OUTREG || !i_reset) && !o_valid) + assert(o_data == 0); + + always @(*) + if (o_ready) + assert(r_data == 0); + end + // }}} + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Cover checks + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // `ifdef SKIDBUFFER + generate if (!OPT_PASSTHROUGH) + begin reg f_changed_data; + initial f_changed_data = 0; + always @(posedge i_clk) + if (i_reset) + f_changed_data <= 1; + else if (i_valid && $past(!i_valid || o_ready)) + begin + if (i_data != $past(i_data + 1)) + f_changed_data <= 0; + end else if (!i_valid && i_data != 0) + f_changed_data <= 0; + + +`ifndef VERIFIC + reg [3:0] cvr_steps, cvr_hold; + + always @(posedge i_clk) + if (i_reset) + begin + cvr_steps <= 0; + cvr_hold <= 0; + end else begin + cvr_steps <= cvr_steps + 1; + cvr_hold <= cvr_hold + 1; + case(cvr_steps) + 0: if (o_valid || i_valid) + cvr_steps <= 0; + 1: if (!i_valid || !i_ready) + cvr_steps <= 0; + 2: if (!i_valid || !i_ready) + cvr_steps <= 0; + 3: if (!i_valid || !i_ready) + cvr_steps <= 0; + 4: if (!i_valid || i_ready) + cvr_steps <= 0; + 5: if (!i_valid || !i_ready) + cvr_steps <= 0; + 6: if (!i_valid || !i_ready) + cvr_steps <= 0; + 7: if (!i_valid || i_ready) + cvr_steps <= 0; + 8: if (!i_valid || i_ready) + cvr_steps <= 0; + 9: if (!i_valid || !i_ready) + cvr_steps <= 0; + 10: if (!i_valid || !i_ready) + cvr_steps <= 0; + 11: if (!i_valid || !i_ready) + cvr_steps <= 0; + 12: begin + cvr_steps <= cvr_steps; + cover(!o_valid && !i_valid && f_changed_data); + if (!o_valid || !i_ready) + cvr_steps <= 0; + else + cvr_hold <= cvr_hold + 1; + end + default: assert(0); + endcase + end + +`else // Cover test cover property (@(posedge i_clk) disable iff (i_reset) @@ -289,22 +482,14 @@ module skidbuffer(i_clk, i_reset, // Wait for the design to clear ##1 o_valid && i_ready [*0:5] ##1 (!o_valid && !i_valid && f_changed_data)); - - initial f_changed_data = 0; - always @(posedge i_clk) - if (i_reset) - f_changed_data <= 1; - else if (i_valid && $past(!i_valid || o_ready)) - begin - if (i_data != $past(i_data + 1)) - f_changed_data <= 0; - end else if (!i_valid && i_data != 0) - f_changed_data <= 0; - -`endif // SKIDCOVER +`endif end endgenerate +`endif // SKIDBUFFER + -`endif // FORMAL_VERIFIC + // }}} +`endif +// }}} endmodule `ifndef YOSYS `default_nettype wire diff --git a/rtl/toplevel.v b/rtl/toplevel.v index 0dbd7fd..a774779 100644 --- a/rtl/toplevel.v +++ b/rtl/toplevel.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: ./toplevel.v -// +// {{{ // Project: ZBasic, a generic toplevel implementation using the full ZipCPU // // DO NOT EDIT THIS FILE! @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} `default_nettype none @@ -56,16 +56,66 @@ // also appear in this list // module toplevel(i_clk, - // GPIO ports - i_gpio, o_gpio, - // UART/host to wishbone interface - i_wbu_uart_rx, o_wbu_uart_tx, - // Top level Quad-SPI I/O ports - o_qspi_cs_n, io_qspi_dat, // A reset wire for the ZipCPU i_cpu_resetn, + // UART/host to wishbone interface + i_wbu_uart_rx, o_wbu_uart_tx, // SD Card - o_sd_sck, io_sd_cmd, io_sd, i_sd_cd); + o_sd_sck, io_sd_cmd, io_sd, i_sd_cd, + // GPIO ports + i_gpio, o_gpio, + // Top level Quad-SPI I/O ports + o_qspi_cs_n, io_qspi_dat); + // + // Declaring any top level parameters. + // + // These declarations just copy data from the @TOP.PARAM key, + // or from the @MAIN.PARAM key if @TOP.PARAM is absent. For + // those peripherals that don't do anything at the top level, + // the @MAIN.PARAM key should be sufficient, so the @TOP.PARAM + // key may be left undefined. + // + //////////////////////////////////////////////////////////////////////// + // + // Variables/definitions/parameters used by the ZipCPU bus master + // {{{ + // + // A 32-bit address indicating where the ZipCPU should start running + // from + localparam RESET_ADDRESS = 20971520; + // + // The number of valid bits on the bus + localparam ZIP_ADDRESS_WIDTH = 23; // Zip-CPU address width + // + // Number of ZipCPU interrupts + localparam ZIP_INTS = 16; + // + // ZIP_START_HALTED + // + // A boolean, indicating whether or not the ZipCPU be halted on startup? + localparam ZIP_START_HALTED=1'b1; + // + // WBUBUS parameters + // + // Baudrate : 1000000 + // Clock : 100000000 + localparam [23:0] BUSUART = 24'h64; // 1000000 baud + localparam DBGBUSBITS = $clog2(BUSUART); + // + // Maximum command is 6 bytes, where each byte takes 10 baud clocks + // and each baud clock requires DBGBUSBITS to represent. Here, + // we'll add one more for good measure. + localparam DBGBUSWATCHDOG_RAW = DBGBUSBITS + 9; + localparam DBGBUSWATCHDOG = (DBGBUSWATCHDOG_RAW > 19) + ? DBGBUSWATCHDOG_RAW : 19; + // + // Initial calendar DATE + // +`ifdef VERSION_ACCESS + parameter INITIAL_DATE = `DATESTAMP; +`else + parameter INITIAL_DATE = 30'h20000101; +`endif // // Declaring our input and output ports. We listed these above, // now we are declaring them here. @@ -79,23 +129,23 @@ module toplevel(i_clk, // We start with any @CLOCK.TOP keys // input wire i_clk; - // GPIO wires - localparam NGPI = 11, NGPO=11; - // GPIO ports - input wire [(11-1):0] i_gpio; - output wire [(11-1):0] o_gpio; - input wire i_wbu_uart_rx; - output wire o_wbu_uart_tx; - // Quad SPI flash - output wire o_qspi_cs_n; - inout wire [3:0] io_qspi_dat; // A reset wire for the ZipCPU input wire i_cpu_resetn; + input wire i_wbu_uart_rx; + output wire o_wbu_uart_tx; // SD Card + // {{{ output wire o_sd_sck; inout wire io_sd_cmd; inout wire [3:0] io_sd; input wire i_sd_cd; + // }}} + // GPIO ports + input wire [(11-1):0] i_gpio; + output wire [(11-1):0] o_gpio; + // Quad SPI flash + output wire o_qspi_cs_n; + inout wire [3:0] io_qspi_dat; // @@ -104,15 +154,18 @@ module toplevel(i_clk, // These declarations just copy data from the @TOP.DEFNS key // within the component data files. // - wire w_qspi_sck, w_qspi_cs_n; - wire [1:0] qspi_bmod; - wire [3:0] qspi_dat; - wire s_clk, s_reset; + // SD Card definitions + // {{{ wire w_sd_cmd; wire [3:0] w_sd_data; wire i_sd_cmd; wire [3:0] i_sd; + // }}} + wire s_clk, s_reset; + wire w_qspi_sck, w_qspi_cs_n; + wire [1:0] qspi_bmod; + wire [3:0] qspi_dat; // @@ -131,16 +184,16 @@ module toplevel(i_clk, // main thedesign(s_clk, s_reset, - // GPIO wires - i_gpio, o_gpio, - // UART/host to wishbone interface - i_wbu_uart_rx, o_wbu_uart_tx, - // Quad SPI flash - w_qspi_cs_n, w_qspi_sck, qspi_dat, io_qspi_dat, qspi_bmod, // Reset wire for the ZipCPU (!i_cpu_resetn), + // UART/host to wishbone interface + i_wbu_uart_rx, o_wbu_uart_tx, // SD Card - o_sd_sck, w_sd_cmd, w_sd_data, io_sd_cmd, io_sd, i_sd_cd); + o_sd_sck, w_sd_cmd, w_sd_data, io_sd_cmd, io_sd, i_sd_cd, + // GPIO wires + i_gpio, o_gpio, + // Quad SPI flash + w_qspi_cs_n, w_qspi_sck, qspi_dat, io_qspi_dat, qspi_bmod); // @@ -150,6 +203,35 @@ module toplevel(i_clk, // + ////////////////////////////////////////////////////////////////////// + // + // SD Card SPI Controller + // {{{ + ////////////////////////////////////////////////////////////////////// + // + // + + // Wires for setting up the SD Card Controller + // {{{ + // This is how we'd set up for SDIO + // assign io_sd_cmd = w_sd_cmd ? 1'bz:1'b0; // MOSI + // assign io_sd[0] = w_sd_data[0]? 1'bz:1'b0; // MISO + // assign io_sd[1] = w_sd_data[1]? 1'bz:1'b0; + // assign io_sd[2] = w_sd_data[2]? 1'bz:1'b0; + // assign io_sd[3] = w_sd_data[3]? 1'bz:1'b0; // CS_n + // }}} + IOBUF sd_cmd_bf(.T(1'b0),.O(i_sd_cmd),.I(w_sd_cmd),.IO(io_sd_cmd));// MISO + IOBUF sd_dat0_bf(.T(1'b1),.O(i_sd[0]),.I(1'b1),.IO(io_sd[0]));// MISO + IOBUF sd_dat1_bf(.T(1'b1),.O(i_sd[1]),.I(1'b1),.IO(io_sd[1])); + IOBUF sd_dat2_bf(.T(1'b1),.O(i_sd[2]),.I(1'b1),.IO(io_sd[2])); + + // Implement an open-drain output + IOBUF sd_dat3_bf(.T(w_sd_data[3]),.O(i_sd[3]),.I(1'b0),.IO(io_sd[3])); + // }}} + + assign s_clk = i_clk; + assign s_reset = 1'b0; // This design requires local, not global resets + // // // Wires for setting up the QSPI flash wishbone peripheral @@ -221,32 +303,6 @@ module toplevel(i_clk, ); - assign s_clk = i_clk; - assign s_reset = 1'b0; // This design requires local, not global resets - - // - // - // Wires for setting up the SD Card Controller - // - // - // assign io_sd_cmd = w_sd_cmd ? 1'bz:1'b0; - // assign io_sd[0] = w_sd_data[0]? 1'bz:1'b0; - // assign io_sd[1] = w_sd_data[1]? 1'bz:1'b0; - // assign io_sd[2] = w_sd_data[2]? 1'bz:1'b0; - // assign io_sd[3] = w_sd_data[3]? 1'bz:1'b0; - - - // IOBUF sd_cmd_buf(.T(w_sd_cmd),.O(i_sd_cmd), .I(1'b0), .IO(io_sd_cmd)); - IOBUF sd_dat0_bf(.T(1'b1),.O(i_sd[0]),.I(1'b1),.IO(io_sd[0])); - IOBUF sd_dat1_bf(.T(1'b1),.O(i_sd[1]),.I(1'b1),.IO(io_sd[1])); - IOBUF sd_dat2_bf(.T(1'b1),.O(i_sd[2]),.I(1'b1),.IO(io_sd[2])); - // IOBUF sd_dat3_bf(.T(w_sd_data[3]),.O(i_sd[3]),.I(1'b0),.IO(io_sd[3])); - - IOBUF sd_cmd_buf(.T(1'b0),.O(i_sd_cmd), .I(w_sd_cmd), .IO(io_sd_cmd)); - IOBUF sd_dat3_bf(.T(1'b0),.O(i_sd[3]),.I(w_sd_data[3]),.IO(io_sd[3])); - - - endmodule // end of toplevel.v module definition diff --git a/rtl/wbgpio.v b/rtl/wbgpio.v index bc01628..fb969db 100644 --- a/rtl/wbgpio.v +++ b/rtl/wbgpio.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbgpio.v -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This extremely simple GPIO controller, although minimally @@ -33,11 +33,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -50,57 +50,72 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbgpio(i_clk, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, i_wb_sel, - o_wb_stall, o_wb_ack, o_wb_data, - i_gpio, o_gpio, o_int); - parameter NIN=16, NOUT=16; - parameter [(NOUT-1):0] DEFAULT=0; - input wire i_clk; - // - input wire i_wb_cyc, i_wb_stb, i_wb_we; - input wire [31:0] i_wb_data; - input wire [32/8-1:0] i_wb_sel; - // - output wire o_wb_stall; - output wire o_wb_ack; - output wire [31:0] o_wb_data; - // - input wire [(NIN-1):0] i_gpio; - output reg [(NOUT-1):0] o_gpio; - // - output reg o_int; +// }}} +module wbgpio #( + // {{{ + parameter NIN=16, NOUT=16, + parameter [(NOUT-1):0] DEFAULT=0 + // }}} + ) ( + // {{{ + input wire i_clk, + // + input wire i_wb_cyc, i_wb_stb, i_wb_we, + input wire [31:0] i_wb_data, + input wire [32/8-1:0] i_wb_sel, + // + output wire o_wb_stall, + output wire o_wb_ack, + output wire [31:0] o_wb_data, + // + input wire [(NIN-1):0] i_gpio, + output reg [(NOUT-1):0] o_gpio, + // + output reg o_int + // }}} + ); + + // Local declarations + // {{{ + reg [(NIN-1):0] x_gpio, q_gpio, r_gpio; + wire [15:0] hi_bits, low_bits; + // }}} assign o_wb_ack = i_wb_stb; assign o_wb_stall = 1'b0; + // o_gpio + // {{{ // 9LUT's, 16 FF's initial o_gpio = DEFAULT; always @(posedge i_clk) if ((i_wb_stb)&&(i_wb_we)) o_gpio <= ((o_gpio)&(~i_wb_data[(NOUT+16-1):16])) |((i_wb_data[(NOUT-1):0])&(i_wb_data[(NOUT+16-1):16])); + // }}} - reg [(NIN-1):0] x_gpio, q_gpio, r_gpio; // 3 LUTs, 33 FF's + // {{{ always @(posedge i_clk) begin { r_gpio, q_gpio, x_gpio } <= { q_gpio, x_gpio, i_gpio }; o_int <= (x_gpio != q_gpio); end + // }}} - wire [15:0] hi_bits, low_bits; + // o_wb_data + // {{{ assign hi_bits[ (NIN -1):0] = r_gpio; assign low_bits[(NOUT-1):0] = o_gpio; + generate if (NIN < 16) assign hi_bits[ 15: NIN] = 0; @@ -109,10 +124,13 @@ module wbgpio(i_clk, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data, i_wb_sel, endgenerate assign o_wb_data = { hi_bits, low_bits }; + // }}} // Make Verilator happy + // {{{ // verilator lint_off UNUSED wire unused; assign unused = &{ 1'b0, i_wb_cyc, i_wb_data[31:0], i_wb_sel }; // verilator lint_on UNUSED + // }}} endmodule diff --git a/rtl/wbscope/wbscopc.v b/rtl/wbscope/wbscopc.v index b92be23..0a3698c 100644 --- a/rtl/wbscope/wbscopc.v +++ b/rtl/wbscope/wbscopc.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbscopc.v -// +// {{{ // Project: WBScope, a wishbone hosted scope // // Purpose: This scope is identical in function to the wishbone scope @@ -42,11 +42,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -59,45 +59,61 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // // `default_nettype none -// -module wbscopc(i_data_clk, i_ce, i_trigger, i_data, - i_wb_clk, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, i_wb_sel, - o_wb_stall, o_wb_ack, o_wb_data, - o_interrupt); - parameter [4:0] LGMEM = 5'd10; - parameter BUSW = 32, NELM=(BUSW-1); - parameter [0:0] SYNCHRONOUS=1; - parameter HOLDOFFBITS=20; - parameter [(HOLDOFFBITS-1):0] DEFAULT_HOLDOFF = ((1<<(LGMEM-1))-4); - parameter STEP_BITS=BUSW-1; - parameter [(STEP_BITS-1):0] MAX_STEP= {(STEP_BITS){1'b1}}; - // The input signals that we wish to record - input wire i_data_clk, i_ce, i_trigger; - input wire [(NELM-1):0] i_data; - // The WISHBONE bus for reading and configuring this scope - input wire i_wb_clk, i_wb_cyc, i_wb_stb, i_wb_we; - input wire i_wb_addr; // One address line only - input wire [(BUSW-1):0] i_wb_data; - input wire [(BUSW/8-1):0] i_wb_sel; - output wire o_wb_stall, o_wb_ack; - output reg [(BUSW-1):0] o_wb_data; - // And, finally, for a final flair --- offer to interrupt the CPU after - // our trigger has gone off. This line is equivalent to the scope - // being stopped. It is not maskable here. - output wire o_interrupt; - - - - reg [(LGMEM-1):0] raddr; +// }}} +module wbscopc #( + // {{{ + parameter [4:0] LGMEM = 5'd10, + parameter BUSW = 32, NELM=(BUSW-1), + parameter [0:0] SYNCHRONOUS=1, + parameter HOLDOFFBITS=20, + parameter [(HOLDOFFBITS-1):0] DEFAULT_HOLDOFF + = ((1<<(LGMEM-1))-4), + parameter STEP_BITS=BUSW-1, + parameter [(STEP_BITS-1):0] MAX_STEP= {(STEP_BITS){1'b1}} + // }}} + ) ( + // {{{ + // The input signals that we wish to record + input wire i_data_clk, i_ce, i_trigger, + input wire [(NELM-1):0] i_data, + // The WISHBONE bus for reading and configuring this scope + // {{{ + input wire i_wb_clk, i_wb_cyc, + i_wb_stb, i_wb_we, + input wire i_wb_addr, // One address line only + input wire [(BUSW-1):0] i_wb_data, + input wire [(BUSW/8-1):0] i_wb_sel, + output wire o_wb_stall, o_wb_ack, + output reg [(BUSW-1):0] o_wb_data, + // }}} + // And, finally, for a final flair --- offer to interrupt the + // CPU after our trigger has gone off. This line is equivalent + // to the scope being stopped. It is not maskable here. + output wire o_interrupt + // }}} + ); + + // Signal declarations + // {{{ + // Pseudo bus signals + // {{{ + wire write_stb, read_from_data, write_to_control; + reg [31:0] o_bus_data; + wire bus_clock; + reg read_address; + wire [BUSW-1:0] i_bus_data; + // }}} + + reg [(LGMEM-1):0] raddr, waddr; reg [(BUSW-1):0] mem[0:((1< 0) begin @@ -139,7 +239,7 @@ module wbscopc(i_data_clk, i_ce, i_trigger, i_data, // Resets are synchronous to the bus clock, not the data clock // so do a clock transfer here - initial q_iflags = 3'b000; + initial { q_iflags, r_iflags } = 6'h0; initial r_reset_complete = 1'b0; always @(posedge i_data_clk) begin @@ -152,13 +252,13 @@ module wbscopc(i_data_clk, i_ce, i_trigger, i_data, assign dw_manual_trigger = r_iflags[1]; assign dw_disable_trigger = r_iflags[0]; - (* ASYNC_REG = "TRUE" *) reg q_reset_complete; - reg qq_reset_complete; + (* ASYNC_REG = "TRUE" *) reg q_reset_complete, + qq_reset_complete; // Pass an acknowledgement back from the data clock to the bus // clock that the reset has been accomplished initial q_reset_complete = 1'b0; initial qq_reset_complete = 1'b0; - always @(posedge i_wb_clk) + always @(posedge bus_clock) begin q_reset_complete <= r_reset_complete; qq_reset_complete <= q_reset_complete; @@ -166,64 +266,80 @@ module wbscopc(i_data_clk, i_ce, i_trigger, i_data, assign bw_reset_complete = qq_reset_complete; end endgenerate - + // }}} + //////////////////////////////////////////////////////////////////////// // // Set up the trigger + // {{{ + //////////////////////////////////////////////////////////////////////// // // - // Write with the i-clk, or input clock. All outputs read with the - // WISHBONE-clk, or i_wb_clk clock. - reg dr_triggered, dr_primed; - wire dw_trigger; + + // dw_trigger -- trigger wire, defined on the data clock + // {{{ + // Write with the i_clk, or input clock. All outputs read with the + // bus clock, or i_wb_clk as we've called it here. assign dw_trigger = (dr_primed)&&( ((i_trigger)&&(!dw_disable_trigger)) ||(dw_manual_trigger)); + // }}} + + // dr_triggered + // {{{ initial dr_triggered = 1'b0; always @(posedge i_data_clk) - if (dw_reset) - dr_triggered <= 1'b0; - else if ((i_ce)&&(dw_trigger)) - dr_triggered <= 1'b1; + if (dw_reset) + dr_triggered <= 1'b0; + else if ((i_ce)&&(dw_trigger)) + dr_triggered <= 1'b1; + // }}} // // Determine when memory is full and capture is complete // // Writes take place on the data clock - // The counter is unsigned - (* ASYNC_REG="TRUE" *) reg [(HOLDOFFBITS-1):0] holdoff_counter; - reg dr_stopped; - initial dr_stopped = 1'b0; + // holdoff_counter + // {{{ + // The counter is unsigned initial holdoff_counter = 0; always @(posedge i_data_clk) - if (dw_reset) - holdoff_counter <= 0; - else if ((i_ce)&&(dr_triggered)&&(!dr_stopped)) - begin - holdoff_counter <= holdoff_counter + 1'b1; - end - + if (dw_reset) + holdoff_counter <= 0; + else if ((i_ce)&&(dr_triggered)&&(!dr_stopped)) + holdoff_counter <= holdoff_counter + 1'b1; + // }}} + + // dr_stopped + // {{{ + initial dr_stopped = 1'b0; always @(posedge i_data_clk) - if ((!dr_triggered)||(dw_reset)) - dr_stopped <= 1'b0; - else if ((i_ce)&&(!dr_stopped)) - begin - if (HOLDOFFBITS > 1) // if (i_ce) - dr_stopped <= (holdoff_counter >= br_holdoff); - else if (HOLDOFFBITS <= 1) - dr_stopped <= ((i_ce)&&(dw_trigger)); - end + if ((!dr_triggered)||(dw_reset)) + dr_stopped <= 1'b0; + else if ((i_ce)&&(!dr_stopped)) + begin + if (HOLDOFFBITS > 1) // if (i_ce) + dr_stopped <= (holdoff_counter >= br_holdoff); + else if (HOLDOFFBITS <= 1) + dr_stopped <= ((i_ce)&&(dw_trigger)); + end + // }}} - localparam DLYSTOP=5; - reg [(DLYSTOP-1):0] dr_stop_pipe; always @(posedge i_data_clk) - if (dw_reset) - dr_stop_pipe <= 0; - else if (i_ce) - dr_stop_pipe <= { dr_stop_pipe[(DLYSTOP-2):0], dr_stopped }; + if (dw_reset) + dr_stop_pipe <= 0; + else if (i_ce) + dr_stop_pipe <= { dr_stop_pipe[(DLYSTOP-2):0], dr_stopped }; - wire dw_final_stop; assign dw_final_stop = dr_stop_pipe[(DLYSTOP-1)]; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Write to memory + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // // A big part of this scope is the run length of any particular // data value. Hence, when the address line (i.e. data[31]) @@ -234,11 +350,9 @@ module wbscopc(i_data_clk, i_ce, i_trigger, i_data, // data changes, but increment it on all other clocks. Should the // address difference get to our maximum value, we let it saturate // rather than overflow. - reg [(STEP_BITS-1):0] ck_addr; - reg [(NELM-1):0] qd_data; - reg dr_force_write, dr_run_timeout, - new_data; + // dr_force_write, dr_force_inhibit + // {{{ // // The "dr_force_write" logic here is designed to make sure we write // at least every MAX_STEP samples, and that we stop as soon as @@ -247,109 +361,123 @@ module wbscopc(i_data_clk, i_ce, i_trigger, i_data, // doesn't have enough transitions to fill our buffer, we'll at least // fill the buffer with repeats. // - reg dr_force_inhibit; initial ck_addr = 0; initial dr_force_write = 1'b0; always @(posedge i_data_clk) - if (dw_reset) - begin - dr_force_write <= 1'b1; - dr_force_inhibit <= 1'b0; - end else if (i_ce) - begin - dr_force_inhibit <= (dr_force_write); - if ((dr_run_timeout)&&(!dr_force_write)&&(!dr_force_inhibit)) - dr_force_write <= 1'b1; - else if (((dw_trigger)&&(!dr_triggered))||(!dr_primed)) - dr_force_write <= 1'b1; - else - dr_force_write <= 1'b0; - end + if (dw_reset) + begin + dr_force_write <= 1'b1; + dr_force_inhibit <= 1'b0; + end else if (i_ce) + begin + dr_force_inhibit <= (dr_force_write); + if ((dr_run_timeout)&&(!dr_force_write)&&(!dr_force_inhibit)) + dr_force_write <= 1'b1; + else if (((dw_trigger)&&(!dr_triggered))||(!dr_primed)) + dr_force_write <= 1'b1; + else + dr_force_write <= 1'b0; + end + // }}} - // - // Keep track of how long it has been since the last write - // + // ck_addr: Keep track of how long it has been since the last write + // {{{ always @(posedge i_data_clk) - if (dw_reset) + if (dw_reset) + ck_addr <= 0; + else if (i_ce) + begin + if ((dr_force_write)||(new_data)||(dr_stopped)) ck_addr <= 0; - else if (i_ce) - begin - if ((dr_force_write)||(new_data)||(dr_stopped)) - ck_addr <= 0; - else - ck_addr <= ck_addr + 1'b1; - end + else + ck_addr <= ck_addr + 1'b1; + end + // }}} + // dr_run_timeout + // {{{ always @(posedge i_data_clk) - if (dw_reset) - dr_run_timeout <= 1'b1; - else if (i_ce) - dr_run_timeout <= (ck_addr >= MAX_STEP-1'b1); - + if (dw_reset) + dr_run_timeout <= 1'b1; + else if (i_ce) + dr_run_timeout <= (ck_addr >= MAX_STEP-1'b1); + // }}} + + // new_data + // {{{ always @(posedge i_data_clk) - if (dw_reset) - new_data <= 1'b1; - else if (i_ce) - new_data <= (i_data != qd_data); - + if (dw_reset) + new_data <= 1'b1; + else if (i_ce) + new_data <= (i_data != qd_data); + // }}} + + // qd_data + // {{{ always @(posedge i_data_clk) - if (i_ce) - qd_data <= i_data; + if (i_ce) + qd_data <= i_data; + // }}} - wire [(BUSW-2):0] w_data; + // w_data + // {{{ generate if (NELM == BUSW-1) assign w_data = qd_data; else assign w_data = { {(BUSW-NELM-1){1'b0}}, qd_data }; endgenerate + // }}} - // + // imm_val, imm_adr, lst_val, lst_adr + // {{{ // To do our RLE compression, we keep track of two registers: the most // recent data to the device (imm_ prefix) and the data from one // clock ago. This allows us to suppress writes to the scope which // would otherwise be two address writes in a row. - reg imm_adr, lst_adr; // Is this an address (1'b1) or data value? - reg [(BUSW-2):0] lst_val, // Data for the scope, delayed by one - imm_val; // Data to write to the scope initial lst_adr = 1'b1; initial imm_adr = 1'b1; always @(posedge i_data_clk) - if (dw_reset) - begin - imm_val <= 31'h0; - imm_adr <= 1'b1; - lst_val <= 31'h0; - lst_adr <= 1'b1; - end else if (i_ce) + if (dw_reset) + begin + imm_val <= 31'h0; + imm_adr <= 1'b1; + lst_val <= 31'h0; + lst_adr <= 1'b1; + end else if (i_ce) + begin + if ((new_data)||(dr_force_write)||(dr_stopped)) begin - if ((new_data)||(dr_force_write)||(dr_stopped)) - begin - imm_val <= w_data; - imm_adr <= 1'b0; // Last thing we wrote was data - lst_val <= imm_val; - lst_adr <= imm_adr; - end else begin - imm_val <= ck_addr; // Minimum value here is '1' - imm_adr <= 1'b1; // This (imm) is an address - lst_val <= imm_val; - lst_adr <= imm_adr; - end + imm_val <= w_data; + imm_adr <= 1'b0; // Last thing we wrote was data + lst_val <= imm_val; + lst_adr <= imm_adr; + end else begin + imm_val <= ck_addr; // Minimum value here is '1' + imm_adr <= 1'b1; // This (imm) is an address + lst_val <= imm_val; + lst_adr <= imm_adr; end + end + // }}} - // + // record_ce + // {{{ // Here's where we suppress writing pairs of address words to the // scope at once. // - reg record_ce; - reg [(BUSW-1):0] r_data; initial record_ce = 1'b0; always @(posedge i_data_clk) record_ce <= (i_ce)&&((!lst_adr)||(!imm_adr))&&(!dr_stop_pipe[2]); + // }}} + + // r_data + // {{{ always @(posedge i_data_clk) r_data <= ((!lst_adr)||(!imm_adr)) ? { lst_adr, lst_val } : { {(32 - NELM){1'b0}}, qd_data }; + // }}} // // Actually do our writes to memory. Record, via 'primed' when @@ -361,23 +489,30 @@ module wbscopc(i_data_clk, i_ce, i_trigger, i_data, // The clock transfer on the stopped line handles the clock // transfer for these signals. // - reg [(LGMEM-1):0] waddr; + + // waddr, dr_primed + // {{{ initial waddr = {(LGMEM){1'b0}}; initial dr_primed = 1'b0; always @(posedge i_data_clk) - if (dw_reset) // For simulation purposes, supply a valid value - begin - waddr <= 0; // upon reset. - dr_primed <= 1'b0; - end else if (record_ce) - begin - // mem[waddr] <= i_data; - waddr <= waddr + {{(LGMEM-1){1'b0}},1'b1}; - dr_primed <= (dr_primed)||(&waddr); - end + if (dw_reset) // For simulation purposes, supply a valid value + begin + waddr <= 0; // upon reset. + dr_primed <= 1'b0; + end else if (record_ce) + begin + // mem[waddr] <= i_data; + waddr <= waddr + {{(LGMEM-1){1'b0}},1'b1}; + dr_primed <= (dr_primed)||(&waddr); + end + // }}} + + // mem[] <= r_data + // {{{ always @(posedge i_data_clk) - if (record_ce) - mem[waddr] <= r_data; + if (record_ce) + mem[waddr] <= r_data; + // }}} // @@ -386,18 +521,21 @@ module wbscopc(i_data_clk, i_ce, i_trigger, i_data, // Bus response // // - + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// // - // Clock transfer of the status signals + // Move the status signals back to the bus clock + // {{{ + //////////////////////////////////////////////////////////////////////// // - wire bw_stopped, bw_triggered, bw_primed; - generate - if (SYNCHRONOUS > 0) - begin + generate if (SYNCHRONOUS > 0) + begin : SYNCHRONOUS_RETURN assign bw_stopped = dw_final_stop; assign bw_triggered = dr_triggered; assign bw_primed = dr_primed; - end else begin + end else begin : ASYNC_STATUS + // {{{ // These aren't a problem, since none of these are strobe // signals. They goes from low to high, and then stays high // for many clocks. Swapping is thus easy--two flip flops to @@ -407,67 +545,73 @@ module wbscopc(i_data_clk, i_ce, i_trigger, i_data, reg [2:0] r_oflags; initial q_oflags = 3'h0; initial r_oflags = 3'h0; - always @(posedge i_wb_clk) - if (bw_reset_request) - begin - q_oflags <= 3'h0; - r_oflags <= 3'h0; - end else begin - q_oflags <= { dw_final_stop, dr_triggered, dr_primed }; - r_oflags <= q_oflags; - end + always @(posedge bus_clock) + if (bw_reset_request) + begin + q_oflags <= 3'h0; + r_oflags <= 3'h0; + end else begin + q_oflags <= { dw_final_stop, dr_triggered, dr_primed }; + r_oflags <= q_oflags; + end assign bw_stopped = r_oflags[2]; assign bw_triggered = r_oflags[1]; assign bw_primed = r_oflags[0]; + // }}} end endgenerate - - + // }}} + //////////////////////////////////////////////////////////////////////// // - // Reads use the bus clock + // Read from the memory, using the bus clock. Otherwise respond to bus + // {{{ + //////////////////////////////////////////////////////////////////////// // - reg br_wb_ack, br_pre_wb_ack; - initial br_wb_ack = 1'b0; - wire bw_cyc_stb; + // + + // Reads use the bus clock assign bw_cyc_stb = (i_wb_stb); + initial br_pre_wb_ack = 1'b0; initial br_wb_ack = 1'b0; - always @(posedge i_wb_clk) + always @(posedge bus_clock) begin - if ((bw_reset_request) - ||((bw_cyc_stb)&&(i_wb_addr)&&(i_wb_we))) + if ((bw_reset_request)||(write_to_control)) raddr <= 0; - else if ((bw_cyc_stb)&&(i_wb_addr)&&(!i_wb_we)&&(bw_stopped)) + else if ((read_from_data)&&(bw_stopped)) raddr <= raddr + 1'b1; // Data read, when stopped br_pre_wb_ack <= bw_cyc_stb; br_wb_ack <= (br_pre_wb_ack)&&(i_wb_cyc); end + assign o_wb_ack = br_wb_ack; + always @(posedge bus_clock) + if (read_from_data) + this_addr <= raddr + waddr + 1'b1; + else + this_addr <= raddr + waddr; - reg [(LGMEM-1):0] this_addr; - always @(posedge i_wb_clk) - if ((bw_cyc_stb)&&(i_wb_addr)&&(!i_wb_we)) - this_addr <= raddr + waddr + 1'b1; - else - this_addr <= raddr + waddr; - - reg [31:0] nxt_mem; - always @(posedge i_wb_clk) + always @(posedge bus_clock) nxt_mem <= mem[this_addr]; - wire [19:0] full_holdoff; + // holdoff sub-register + // {{{ assign full_holdoff[(HOLDOFFBITS-1):0] = br_holdoff; generate if (HOLDOFFBITS < 20) assign full_holdoff[19:(HOLDOFFBITS)] = 0; endgenerate + // }}} - wire [4:0] bw_lgmem; assign bw_lgmem = LGMEM; - always @(posedge i_wb_clk) - if (!i_wb_addr) // Control register read - o_wb_data <= { bw_reset_request, + + // Bus read + // {{{ + always @(posedge bus_clock) + begin + if (!read_address) // Control register read + o_bus_data <= { bw_reset_request, bw_stopped, bw_triggered, bw_primed, @@ -477,28 +621,42 @@ module wbscopc(i_data_clk, i_ce, i_trigger, i_data, bw_lgmem, full_holdoff }; else if (!bw_stopped) // read, prior to stopping - o_wb_data <= {1'b0, w_data };// Violates clk tfr rules + // + // *WARNING*: THIS READ IS NOT PROTECTED FROM + // ASYNCHRONOUS COHERENCE ISSUES! + // + o_bus_data <= {1'b0, w_data };// Violates clk tfr rules else // if (i_wb_addr) // Read from FIFO memory - o_wb_data <= nxt_mem; // mem[raddr+waddr]; - - assign o_wb_stall = 1'b0; - assign o_wb_ack = (i_wb_cyc)&&(br_wb_ack); + o_bus_data <= nxt_mem; // mem[raddr+waddr]; + end + // }}} - reg br_level_interrupt; + assign o_wb_data = o_bus_data; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Interrupt generation + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // initial br_level_interrupt = 1'b0; - assign o_interrupt = (bw_stopped)&&(!bw_disable_trigger) - &&(!br_level_interrupt); - always @(posedge i_wb_clk) + always @(posedge bus_clock) if ((bw_reset_complete)||(bw_reset_request)) br_level_interrupt<= 1'b0; else br_level_interrupt<= (bw_stopped)&&(!bw_disable_trigger); - // Make Verilator happy + assign o_interrupt = (bw_stopped)&&(!bw_disable_trigger) + &&(!br_level_interrupt); + // }}} + + // Make verilator happy + // {{{ // verilator lint_off UNUSED wire unused; - assign unused = &{ 1'b0, i_wb_data[30:28], i_wb_data[25:HOLDOFFBITS], + assign unused = &{ 1'b0, i_bus_data[30:28], i_bus_data[25:HOLDOFFBITS], i_wb_sel }; // verilator lint_on UNUSED - + // }}} endmodule diff --git a/rtl/wbscope/wbscope.v b/rtl/wbscope/wbscope.v index a2e1c96..8a6cddf 100644 --- a/rtl/wbscope/wbscope.v +++ b/rtl/wbscope/wbscope.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbscope.v -// +// {{{ // Project: WBScope, a wishbone hosted scope // // Purpose: This is a generic/library routine for providing a bus accessed @@ -58,9 +58,9 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or // modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at @@ -75,77 +75,106 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // // `default_nettype none -// -module wbscope(i_data_clk, i_ce, i_trigger, i_data, - i_wb_clk, i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, i_wb_sel, - o_wb_stall, o_wb_ack, o_wb_data, - o_interrupt); - parameter [4:0] LGMEM = 5'd10; - parameter BUSW = 32; - parameter [0:0] SYNCHRONOUS=1; - parameter HOLDOFFBITS = 20; - parameter [(HOLDOFFBITS-1):0] DEFAULT_HOLDOFF = ((1<<(LGMEM-1))-4); - // The input signals that we wish to record - input wire i_data_clk, i_ce, i_trigger; - input wire [(BUSW-1):0] i_data; - // The WISHBONE bus for reading and configuring this scope - input wire i_wb_clk, i_wb_cyc, i_wb_stb, i_wb_we; - input wire i_wb_addr; // One address line only - input wire [(BUSW-1):0] i_wb_data; - input wire [(BUSW/8-1):0] i_wb_sel; - output wire o_wb_stall, o_wb_ack; - output wire [(BUSW-1):0] o_wb_data; - // And, finally, for a final flair --- offer to interrupt the CPU after - // our trigger has gone off. This line is equivalent to the scope - // being stopped. It is not maskable here. - output wire o_interrupt; - - wire bus_clock; +// }}} +module wbscope #( + // {{{ + parameter [4:0] LGMEM = 5'd10, + parameter BUSW = 32, + parameter [0:0] SYNCHRONOUS=1, + parameter HOLDOFFBITS = 20, + parameter [(HOLDOFFBITS-1):0] DEFAULT_HOLDOFF = ((1<<(LGMEM-1))-4) + // }}} + ) ( + // {{{ + // The input signals that we wish to record + input wire i_data_clk, i_ce, i_trigger, + input wire [(BUSW-1):0] i_data, + // The WISHBONE bus for reading and configuring this scope + // {{{ + input wire i_wb_clk, i_wb_cyc, + i_wb_stb, i_wb_we, + input wire i_wb_addr, // One address line only + input wire [(BUSW-1):0] i_wb_data, + input wire [(BUSW/8-1):0] i_wb_sel, + output wire o_wb_stall, o_wb_ack, + output wire [(BUSW-1):0] o_wb_data, + // }}} + // And, finally, for a final flair --- offer to interrupt the + // CPU after our trigger has gone off. This line is equivalent + // to the scope being stopped. It is not maskable here. + output wire o_interrupt + // }}} + ); + + // Signal declarations + // {{{ + wire bus_clock; + wire read_from_data; + wire write_stb; + wire write_to_control; + reg read_address; + wire [31:0] i_bus_data; + reg [(LGMEM-1):0] raddr; + reg [(BUSW-1):0] mem[0:((1< 0) begin @@ -200,7 +231,8 @@ module wbscope(i_data_clk, i_ce, i_trigger, i_data, assign bw_reset_complete = bw_reset_request; end else begin reg r_reset_complete; - (* ASYNC_REG = "TRUE" *) reg [2:0] q_iflags, r_iflags; + (* ASYNC_REG = "TRUE" *) reg [2:0] q_iflags; + reg [2:0] r_iflags; // Resets are synchronous to the bus clock, not the data clock // so do a clock transfer here @@ -230,49 +262,76 @@ module wbscope(i_data_clk, i_ce, i_trigger, i_data, end assign bw_reset_complete = qq_reset_complete; - end endgenerate + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// // // Set up the trigger + // {{{ + //////////////////////////////////////////////////////////////////////// // // - // Write with the i-clk, or input clock. All outputs read with the - // bus clock, or bus_clock as we've called it here. - reg dr_triggered, dr_primed; - wire dw_trigger; + + // dw_trigger -- trigger wire, defined on the data clock + // {{{ + // Write with the i_clk, or input clock. All outputs read with the + // bus clock, or i_wb_clk as we've called it here. assign dw_trigger = (dr_primed)&&( ((i_trigger)&&(!dw_disable_trigger)) ||(dw_manual_trigger)); + // }}} + + // dr_triggered + // {{{ initial dr_triggered = 1'b0; always @(posedge i_data_clk) if (dw_reset) dr_triggered <= 1'b0; else if ((i_ce)&&(dw_trigger)) dr_triggered <= 1'b1; + // }}} // // Determine when memory is full and capture is complete // // Writes take place on the data clock - // The counter is unsigned - (* ASYNC_REG="TRUE" *) reg [(HOLDOFFBITS-1):0] counter; - reg dr_stopped; - initial dr_stopped = 1'b0; + // counter + // {{{ + // The counter is unsigned initial counter = 0; always @(posedge i_data_clk) if (dw_reset) counter <= 0; else if ((i_ce)&&(dr_triggered)&&(!dr_stopped)) counter <= counter + 1'b1; + // }}} + // dr_stopped + // {{{ + initial dr_stopped = 1'b0; always @(posedge i_data_clk) if ((!dr_triggered)||(dw_reset)) dr_stopped <= 1'b0; - else if (HOLDOFFBITS > 1) // if (i_ce) - dr_stopped <= (counter >= br_holdoff); - else if (HOLDOFFBITS <= 1) - dr_stopped <= ((i_ce)&&(dw_trigger)); + else if (!dr_stopped) + begin + if (HOLDOFFBITS > 1) // if (i_ce) + dr_stopped <= (counter >= br_holdoff); + else if (HOLDOFFBITS <= 1) + dr_stopped <= ((i_ce)&&(dw_trigger)); + end + // }}} + + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Write to memory + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + // // Actually do our writes to memory. Record, via 'primed' when @@ -284,7 +343,9 @@ module wbscope(i_data_clk, i_ce, i_trigger, i_data, // The clock transfer on the stopped line handles the clock // transfer for these signals. // - reg [(LGMEM-1):0] waddr; + + // waddr, dr_primed + // {{{ initial waddr = {(LGMEM){1'b0}}; initial dr_primed = 1'b0; always @(posedge i_data_clk) @@ -299,13 +360,14 @@ module wbscope(i_data_clk, i_ce, i_trigger, i_data, if (!dr_primed) dr_primed <= (&waddr); end + // }}} + // wr_piped_data -- delay data to match the trigger + // {{{ // Delay the incoming data so that we can get our trigger // logic to line up with the data. The goal is to have a // hold off of zero place the trigger in the last memory // address. - localparam STOPDELAY = 1; - wire [(BUSW-1):0] wr_piped_data; generate if (STOPDELAY == 0) // No delay ... just assign the wires to our input lines @@ -329,22 +391,28 @@ module wbscope(i_data_clk, i_ce, i_trigger, i_data, data_pipe <= { data_pipe[((STOPDELAY-1)*BUSW-1):0], i_data }; assign wr_piped_data = { data_pipe[(STOPDELAY*BUSW-1):((STOPDELAY-1)*BUSW)] }; end endgenerate + // }}} + // mem[] <= wr_piped_data + // {{{ always @(posedge i_data_clk) if ((i_ce)&&(!dr_stopped)) mem[waddr] <= wr_piped_data; - + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// // - // Clock transfer of the status signals + // Move the status signals back to the bus clock + // {{{ + //////////////////////////////////////////////////////////////////////// // - wire bw_stopped, bw_triggered, bw_primed; - generate - if (SYNCHRONOUS > 0) - begin + generate if (SYNCHRONOUS > 0) + begin : SYNCHRONOUS_RETURN assign bw_stopped = dr_stopped; assign bw_triggered = dr_triggered; assign bw_primed = dr_primed; - end else begin + end else begin : ASYNC_STATUS + // {{{ // These aren't a problem, since none of these are strobe // signals. They goes from low to high, and then stays high // for many clocks. Swapping is thus easy--two flip flops to @@ -355,25 +423,32 @@ module wbscope(i_data_clk, i_ce, i_trigger, i_data, initial q_oflags = 3'h0; initial r_oflags = 3'h0; always @(posedge bus_clock) - if (bw_reset_request) - begin - q_oflags <= 3'h0; - r_oflags <= 3'h0; - end else begin - q_oflags <= { dr_stopped, dr_triggered, dr_primed }; - r_oflags <= q_oflags; - end + if (bw_reset_request) + begin + q_oflags <= 3'h0; + r_oflags <= 3'h0; + end else begin + q_oflags <= { dr_stopped, dr_triggered, dr_primed }; + r_oflags <= q_oflags; + end assign bw_stopped = r_oflags[2]; assign bw_triggered = r_oflags[1]; assign bw_primed = r_oflags[0]; + // }}} end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Read from the memory, using the bus clock. Otherwise respond to bus + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // // Reads use the bus clock - reg br_wb_ack, br_pre_wb_ack; - initial br_wb_ack = 1'b0; - wire bw_cyc_stb; assign bw_cyc_stb = (i_wb_stb); + initial br_pre_wb_ack = 1'b0; initial br_wb_ack = 1'b0; always @(posedge bus_clock) @@ -386,60 +461,79 @@ module wbscope(i_data_clk, i_ce, i_trigger, i_data, br_pre_wb_ack <= bw_cyc_stb; br_wb_ack <= (br_pre_wb_ack)&&(i_wb_cyc); end + assign o_wb_ack = (i_wb_cyc)&&(br_wb_ack); - reg [(LGMEM-1):0] this_addr; always @(posedge bus_clock) if (read_from_data) this_addr <= raddr + waddr + 1'b1; else this_addr <= raddr + waddr; - reg [31:0] nxt_mem; always @(posedge bus_clock) nxt_mem <= mem[this_addr]; - wire [19:0] full_holdoff; + // holdoff sub-register + // {{{ assign full_holdoff[(HOLDOFFBITS-1):0] = br_holdoff; generate if (HOLDOFFBITS < 20) assign full_holdoff[19:(HOLDOFFBITS)] = 0; endgenerate + // }}} - reg [31:0] o_bus_data; - wire [4:0] bw_lgmem; assign bw_lgmem = LGMEM; + + // Bus read + // {{{ always @(posedge bus_clock) - if (!read_address) // Control register read - o_bus_data <= { bw_reset_request, - bw_stopped, - bw_triggered, - bw_primed, - bw_manual_trigger, - bw_disable_trigger, - (raddr == {(LGMEM){1'b0}}), - bw_lgmem, - full_holdoff }; - else if (!bw_stopped) // read, prior to stopping - o_bus_data <= i_data; - else // if (i_wb_addr) // Read from FIFO memory - o_bus_data <= nxt_mem; // mem[raddr+waddr]; + begin + if (!read_address) // Control register read + o_bus_data <= { bw_reset_request, + bw_stopped, + bw_triggered, + bw_primed, + bw_manual_trigger, + bw_disable_trigger, + (raddr == {(LGMEM){1'b0}}), + bw_lgmem, + full_holdoff }; + else if (!bw_stopped) // read, prior to stopping + // + // *WARNING*: THIS READ IS NOT PROTECTED FROM + // ASYNCHRONOUS COHERENCE ISSUES! + // + o_bus_data <= i_data; + else // if (i_wb_addr) // Read from FIFO memory + o_bus_data <= nxt_mem; // mem[raddr+waddr]; + end + // }}} assign o_wb_data = o_bus_data; - - reg br_level_interrupt; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Interrupt generation + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // initial br_level_interrupt = 1'b0; - assign o_interrupt = (bw_stopped)&&(!bw_disable_trigger) - &&(!br_level_interrupt); always @(posedge bus_clock) if ((bw_reset_complete)||(bw_reset_request)) br_level_interrupt<= 1'b0; else br_level_interrupt<= (bw_stopped)&&(!bw_disable_trigger); - // verilator lint_off UNUSED + assign o_interrupt = (bw_stopped)&&(!bw_disable_trigger) + &&(!br_level_interrupt); + // }}} + // Make verilator happy + // {{{ + // verilator lint_off UNUSED wire unused; assign unused = &{ 1'b0, i_bus_data[30:28], i_bus_data[25:0], i_wb_sel }; // verilator lint_on UNUSED + // }}} endmodule diff --git a/rtl/wbuart/rxuartlite.v b/rtl/wbuart/rxuartlite.v index 81237ff..33b8a04 100644 --- a/rtl/wbuart/rxuartlite.v +++ b/rtl/wbuart/rxuartlite.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: rxuartlite.v -// +// {{{ // Project: wbuart32, a full featured UART with simulator // // Purpose: Receive and decode inputs from a single UART line. @@ -20,11 +20,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -37,38 +37,51 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -`define RXUL_BIT_ZERO 4'h0 -`define RXUL_BIT_ONE 4'h1 -`define RXUL_BIT_TWO 4'h2 -`define RXUL_BIT_THREE 4'h3 -`define RXUL_BIT_FOUR 4'h4 -`define RXUL_BIT_FIVE 4'h5 -`define RXUL_BIT_SIX 4'h6 -`define RXUL_BIT_SEVEN 4'h7 -`define RXUL_STOP 4'h8 -`define RXUL_WAIT 4'h9 -`define RXUL_IDLE 4'hf - -module rxuartlite(i_clk, i_uart_rx, o_wr, o_data); - parameter TIMER_BITS = 10; - parameter [(TIMER_BITS-1):0] CLOCKS_PER_BAUD = 868; - localparam TB = TIMER_BITS; - input wire i_clk; - input wire i_uart_rx; - output reg o_wr; - output reg [7:0] o_data; - - +// }}} +module rxuartlite #( + // {{{ + parameter TIMER_BITS = 10, +`ifdef FORMAL + parameter [(TIMER_BITS-1):0] CLOCKS_PER_BAUD = 16, // Necessary for formal proof +`else + parameter [(TIMER_BITS-1):0] CLOCKS_PER_BAUD = 868, // 115200 MBaud at 100MHz +`endif + localparam TB = TIMER_BITS, + // + localparam [3:0] RXUL_BIT_ZERO = 4'h0, + // Verilator lint_off UNUSED + // These are used by the formal solver + localparam [3:0] RXUL_BIT_ONE = 4'h1, + localparam [3:0] RXUL_BIT_TWO = 4'h2, + localparam [3:0] RXUL_BIT_THREE = 4'h3, + localparam [3:0] RXUL_BIT_FOUR = 4'h4, + localparam [3:0] RXUL_BIT_FIVE = 4'h5, + localparam [3:0] RXUL_BIT_SIX = 4'h6, + localparam [3:0] RXUL_BIT_SEVEN = 4'h7, + // Verilator lint_on UNUSED + localparam [3:0] RXUL_STOP = 4'h8, + localparam [3:0] RXUL_WAIT = 4'h9, + localparam [3:0] RXUL_IDLE = 4'hf + // }}} + ) ( + // {{{ + input wire i_clk, + input wire i_uart_rx, + output reg o_wr, + output reg [7:0] o_data + // }}} + ); + + // Signal/register declarations + // {{{ wire [(TB-1):0] half_baud; reg [3:0] state; @@ -76,72 +89,93 @@ module rxuartlite(i_clk, i_uart_rx, o_wr, o_data); reg [(TB-1):0] baud_counter; reg zero_baud_counter; + reg q_uart, qq_uart, ck_uart; + reg [(TB-1):0] chg_counter; + reg half_baud_time; + reg [7:0] data_reg; + // }}} + // ck_uart + // {{{ // Since this is an asynchronous receiver, we need to register our // input a couple of clocks over to avoid any problems with // metastability. We do that here, and then ignore all but the // ck_uart wire. - reg q_uart, qq_uart, ck_uart; initial q_uart = 1'b1; initial qq_uart = 1'b1; initial ck_uart = 1'b1; always @(posedge i_clk) { ck_uart, qq_uart, q_uart } <= { qq_uart, q_uart, i_uart_rx }; + // }}} + // chg_counter + // {{{ // Keep track of the number of clocks since the last change. // // This is used to determine if we are in either a break or an idle // condition, as discussed further below. - reg [(TB-1):0] chg_counter; initial chg_counter = {(TB){1'b1}}; always @(posedge i_clk) if (qq_uart != ck_uart) chg_counter <= 0; else if (chg_counter != { (TB){1'b1} }) chg_counter <= chg_counter + 1; + // }}} + // half_baud_time + // {{{ // Are we in the middle of a baud iterval? Specifically, are we // in the middle of a start bit? Set this to high if so. We'll use // this within our state machine to transition out of the IDLE // state. - reg half_baud_time; initial half_baud_time = 0; always @(posedge i_clk) half_baud_time <= (!ck_uart)&&(chg_counter >= half_baud-1'b1-1'b1); + // }}} - - initial state = `RXUL_IDLE; + // state + // {{{ + initial state = RXUL_IDLE; always @(posedge i_clk) - if (state == `RXUL_IDLE) + if (state == RXUL_IDLE) begin // Idle state, independent of baud counter + // {{{ // By default, just stay in the IDLE state - state <= `RXUL_IDLE; + state <= RXUL_IDLE; if ((!ck_uart)&&(half_baud_time)) // UNLESS: We are in the center of a valid // start bit - state <= `RXUL_BIT_ZERO; - end else if ((state >= `RXUL_WAIT)&&(ck_uart)) - state <= `RXUL_IDLE; + state <= RXUL_BIT_ZERO; + // }}} + end else if ((state >= RXUL_WAIT)&&(ck_uart)) + state <= RXUL_IDLE; else if (zero_baud_counter) begin - if (state <= `RXUL_STOP) + // {{{ + if (state <= RXUL_STOP) // Data arrives least significant bit first. // By the time this is clocked in, it's what // you'll have. state <= state + 1; + // }}} end + // }}} + // data_reg + // {{{ // Data bit capture logic. // // This is drastically simplified from the state machine above, based // upon: 1) it doesn't matter what it is until the end of a captured // byte, and 2) the data register will flush itself of any invalid // data in all other cases. Hence, let's keep it real simple. - reg [7:0] data_reg; always @(posedge i_clk) - if ((zero_baud_counter)&&(state != `RXUL_STOP)) + if ((zero_baud_counter)&&(state != RXUL_STOP)) data_reg <= { qq_uart, data_reg[7:1] }; + // }}} + // o_wr, o_data + // {{{ // Our data bit logic doesn't need nearly the complexity of all that // work above. Indeed, we only need to know if we are at the end of // a stop bit, in which case we copy the data_reg into our output @@ -151,47 +185,58 @@ module rxuartlite(i_clk, i_uart_rx, o_wr, o_data); initial o_wr = 1'b0; initial o_data = 8'h00; always @(posedge i_clk) - if ((zero_baud_counter)&&(state == `RXUL_STOP)&&(ck_uart)) + if ((zero_baud_counter)&&(state == RXUL_STOP)&&(ck_uart)) begin o_wr <= 1'b1; o_data <= data_reg; end else o_wr <= 1'b0; + // }}} - // The baud counter - // + // baud_counter -- The baud counter + // {{{ // This is used as a "clock divider" if you will, but the clock needs // to be reset before any byte can be decoded. In all other respects, // we set ourselves up for CLOCKS_PER_BAUD counts between baud // intervals. initial baud_counter = 0; always @(posedge i_clk) - if (((state==`RXUL_IDLE))&&(!ck_uart)&&(half_baud_time)) + if (((state==RXUL_IDLE))&&(!ck_uart)&&(half_baud_time)) baud_counter <= CLOCKS_PER_BAUD-1'b1; - else if (state == `RXUL_WAIT) + else if (state == RXUL_WAIT) baud_counter <= 0; - else if ((zero_baud_counter)&&(state < `RXUL_STOP)) + else if ((zero_baud_counter)&&(state < RXUL_STOP)) baud_counter <= CLOCKS_PER_BAUD-1'b1; else if (!zero_baud_counter) baud_counter <= baud_counter-1'b1; + // }}} // zero_baud_counter - // + // {{{ // Rather than testing whether or not (baud_counter == 0) within our // (already too complicated) state transition tables, we use // zero_baud_counter to pre-charge that test on the clock // before--cleaning up some otherwise difficult timing dependencies. initial zero_baud_counter = 1'b1; always @(posedge i_clk) - if ((state == `RXUL_IDLE)&&(!ck_uart)&&(half_baud_time)) + if ((state == RXUL_IDLE)&&(!ck_uart)&&(half_baud_time)) zero_baud_counter <= 1'b0; - else if (state == `RXUL_WAIT) + else if (state == RXUL_WAIT) zero_baud_counter <= 1'b1; - else if ((zero_baud_counter)&&(state < `RXUL_STOP)) + else if ((zero_baud_counter)&&(state < RXUL_STOP)) zero_baud_counter <= 1'b0; else if (baud_counter == 1) zero_baud_counter <= 1'b1; - + // }}} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// +// Formal properties +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL // Formal properties for this module are maintained elsewhere `endif diff --git a/rtl/wbuart/txuartlite.v b/rtl/wbuart/txuartlite.v index 1aa89f3..04856bf 100644 --- a/rtl/wbuart/txuartlite.v +++ b/rtl/wbuart/txuartlite.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: txuartlite.v -// +// {{{ // Project: wbuart32, a full featured UART with simulator // // Purpose: Transmit outputs over a single UART line. This particular UART @@ -24,11 +24,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -41,73 +41,87 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // // `default_nettype none -// -`define TXUL_BIT_ZERO 4'h0 -`define TXUL_BIT_ONE 4'h1 -`define TXUL_BIT_TWO 4'h2 -`define TXUL_BIT_THREE 4'h3 -`define TXUL_BIT_FOUR 4'h4 -`define TXUL_BIT_FIVE 4'h5 -`define TXUL_BIT_SIX 4'h6 -`define TXUL_BIT_SEVEN 4'h7 -`define TXUL_STOP 4'h8 -`define TXUL_IDLE 4'hf -// -// -module txuartlite(i_clk, i_wr, i_data, o_uart_tx, o_busy); - parameter [4:0] TIMING_BITS = 5'd24; - localparam TB = TIMING_BITS; - parameter [(TB-1):0] CLOCKS_PER_BAUD = 8; // 24'd868; - input wire i_clk; - input wire i_wr; - input wire [7:0] i_data; - // And the UART input line itself - output reg o_uart_tx; - // A line to tell others when we are ready to accept data. If - // (i_wr)&&(!o_busy) is ever true, then the core has accepted a byte - // for transmission. - output wire o_busy; +// }}} +module txuartlite #( + // {{{ + // TIMING_BITS -- the number of bits required to represent + // the number of clocks per baud. 24 should be sufficient for + // most baud rates, but you can trim it down to save logic if + // you would like. TB is just an abbreviation for TIMING_BITS. + parameter [4:0] TIMING_BITS = 5'd24, + localparam TB = TIMING_BITS, + // CLOCKS_PER_BAUD -- the number of system clocks per baud + // interval. + parameter [(TB-1):0] CLOCKS_PER_BAUD = 8 // 24'd868 + // }}} + ) ( + // {{{ + input wire i_clk, + input wire i_wr, + input wire [7:0] i_data, + // And the UART input line itself + output reg o_uart_tx, + // A line to tell others when we are ready to accept data. If + // (i_wr)&&(!o_busy) is ever true, then the core has accepted + // a byte for transmission. + output wire o_busy + // }}} + ); + + // Register/net declarations + // {{{ + localparam [3:0] TXUL_BIT_ZERO = 4'h0, + // TXUL_BIT_ONE = 4'h1, + // TXUL_BIT_TWO = 4'h2, + // TXUL_BIT_THREE = 4'h3, + // TXUL_BIT_FOUR = 4'h4, + // TXUL_BIT_FIVE = 4'h5, + // TXUL_BIT_SIX = 4'h6, + // TXUL_BIT_SEVEN = 4'h7, + TXUL_STOP = 4'h8, + TXUL_IDLE = 4'hf; reg [(TB-1):0] baud_counter; reg [3:0] state; reg [7:0] lcl_data; reg r_busy, zero_baud_counter; + // }}} // Big state machine controlling: r_busy, state // {{{ // initial r_busy = 1'b1; - initial state = `TXUL_IDLE; + initial state = TXUL_IDLE; always @(posedge i_clk) begin if (!zero_baud_counter) // r_busy needs to be set coming into here r_busy <= 1'b1; - else if (state > `TXUL_STOP) // STATE_IDLE + else if (state > TXUL_STOP) // STATE_IDLE begin - state <= `TXUL_IDLE; + state <= TXUL_IDLE; r_busy <= 1'b0; if ((i_wr)&&(!r_busy)) begin // Immediately start us off with a start bit r_busy <= 1'b1; - state <= `TXUL_BIT_ZERO; + state <= TXUL_BIT_ZERO; end end else begin // One clock tick in each of these states ... r_busy <= 1'b1; - if (state <=`TXUL_STOP) // start bit, 8-d bits, stop-b + if (state <=TXUL_STOP) // start bit, 8-d bits, stop-b state <= state + 1'b1; else - state <= `TXUL_IDLE; + state <= TXUL_IDLE; end end // }}} @@ -122,7 +136,6 @@ module txuartlite(i_clk, i_wr, i_data, o_uart_tx, o_busy); assign o_busy = (r_busy); // }}} - // lcl_data // {{{ // @@ -190,7 +203,7 @@ module txuartlite(i_clk, i_wr, i_data, o_uart_tx, o_busy); // than waiting for the end of the next (fictitious and arbitrary) baud // interval. // - // When (i_wr)&&(!r_busy)&&(state == `TXUL_IDLE) then we're not only in + // When (i_wr)&&(!r_busy)&&(state == TXUL_IDLE) then we're not only in // the idle state, but we also just accepted a command to start writing // the next word. At this point, the baud counter needs to be reset // to the number of CLOCKS_PER_BAUD, and zero_baud_counter set to zero. @@ -203,7 +216,7 @@ module txuartlite(i_clk, i_wr, i_data, o_uart_tx, o_busy); always @(posedge i_clk) begin zero_baud_counter <= (baud_counter == 1); - if (state == `TXUL_IDLE) + if (state == TXUL_IDLE) begin baud_counter <= 0; zero_baud_counter <= 1'b1; @@ -212,22 +225,32 @@ module txuartlite(i_clk, i_wr, i_data, o_uart_tx, o_busy); baud_counter <= CLOCKS_PER_BAUD - 1'b1; zero_baud_counter <= 1'b0; end - end else if ((zero_baud_counter)&&(state == 4'h9)) + end else if (!zero_baud_counter) + baud_counter <= baud_counter - 1'b1; + else if (state > TXUL_STOP) begin baud_counter <= 0; zero_baud_counter <= 1'b1; - end else if (!zero_baud_counter) - baud_counter <= baud_counter - 1'b1; - else + end else if (state == TXUL_STOP) + // Need to complete this state one clock early, so + // we can release busy one clock before the stop bit + // is complete, so we can start on the next byte + // exactly 10*CLOCKS_PER_BAUD clocks after we started + // the last one + baud_counter <= CLOCKS_PER_BAUD - 2; + else // All other states baud_counter <= CLOCKS_PER_BAUD - 1'b1; end // }}} -// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// // // FORMAL METHODS -// -// -// +// {{{ +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// `ifdef FORMAL `endif endmodule diff --git a/rtl/wbuart/ufifo.v b/rtl/wbuart/ufifo.v index 06af310..4eb6d6c 100644 --- a/rtl/wbuart/ufifo.v +++ b/rtl/wbuart/ufifo.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: ufifo.v -// +// {{{ // Project: wbuart32, a full featured UART with simulator // // Purpose: A synchronous data FIFO, designed for supporting the Wishbone @@ -17,11 +17,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -34,31 +34,38 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // // `default_nettype none -// -module ufifo(i_clk, i_reset, i_wr, i_data, o_empty_n, i_rd, o_data, o_status, o_err); - parameter BW=8; // Byte/data width - parameter [3:0] LGFLEN=4; - parameter [0:0] RXFIFO=1'b1; - input wire i_clk, i_reset; - input wire i_wr; - input wire [(BW-1):0] i_data; - output wire o_empty_n; // True if something is in FIFO - input wire i_rd; - output wire [(BW-1):0] o_data; - output wire [15:0] o_status; - output wire o_err; - - localparam FLEN=(1< for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // // @@ -39,118 +40,142 @@ // // `default_nettype none -// -module wbuart(i_clk, i_rst, +// }}} +// `define USE_LITE_UART +module wbuart #( + // {{{ + // 4MB 8N1, when using 100MHz clock + parameter [30:0] INITIAL_SETUP = 31'd25, + parameter [3:0] LGFLEN = 4, + parameter [0:0] HARDWARE_FLOW_CONTROL_PRESENT = 1'b1, + // Perform a simple/quick bounds check on the log FIFO length, + // to make sure its within the bounds we can support with our + // current interface. + localparam [3:0] LCLLGFLEN = (LGFLEN > 4'ha)? 4'ha + : ((LGFLEN < 4'h2) ? 4'h2 : LGFLEN) + // }}} + ) ( + // {{{ + input wire i_clk, i_reset, + // Wishbone inputs + input wire i_wb_cyc, + input wire i_wb_stb, i_wb_we, + input wire [1:0] i_wb_addr, + input wire [31:0] i_wb_data, + input wire [3:0] i_wb_sel, + output wire o_wb_stall, + output reg o_wb_ack, + output reg [31:0] o_wb_data, // - i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, - o_wb_stall, o_wb_ack, o_wb_data, - // - i_uart_rx, o_uart_tx, i_cts_n, o_rts_n, - // - o_uart_rx_int, o_uart_tx_int, - o_uart_rxfifo_int, o_uart_txfifo_int); - parameter [30:0] INITIAL_SETUP = 31'd25; // 4MB 8N1, when using 100MHz clock - parameter [3:0] LGFLEN = 4; - parameter [0:0] HARDWARE_FLOW_CONTROL_PRESENT = 1'b1; - // Perform a simple/quick bounds check on the log FIFO length, to make - // sure its within the bounds we can support with our current - // interface. - localparam [3:0] LCLLGFLEN = (LGFLEN > 4'ha)? 4'ha - : ((LGFLEN < 4'h2) ? 4'h2 : LGFLEN); - localparam [1:0] UART_SETUP = 2'b00, + input wire i_uart_rx, + output wire o_uart_tx, + input wire i_cts_n, + output reg o_rts_n, + output wire o_uart_rx_int, o_uart_tx_int, + o_uart_rxfifo_int, o_uart_txfifo_int + // }}} + ); + + localparam [1:0] UART_SETUP = 2'b00, UART_FIFO = 2'b01, UART_RXREG = 2'b10, UART_TXREG = 2'b11; - // - input wire i_clk, i_rst; - // Wishbone inputs - input wire i_wb_cyc; // We ignore CYC for efficiency - input wire i_wb_stb, i_wb_we; - input wire [1:0] i_wb_addr; - input wire [31:0] i_wb_data; // and only use 30 lines here - output wire o_wb_stall; - output reg o_wb_ack; - output reg [31:0] o_wb_data; - // - input wire i_uart_rx; - output wire o_uart_tx; - // RTS is used for hardware flow control. According to Wikipedia, it - // should probably be renamed RTR for "ready to receive". It tell us - // whether or not the receiving hardware is ready to accept another - // byte. If low, the transmitter will pause. - // - // If you don't wish to use hardware flow control, just set i_cts_n to - // 1'b0 and let the optimizer simply remove this logic. - input wire i_cts_n; - // CTS is the "Clear-to-send" signal. We set it anytime our FIFO - // isn't full. Feel free to ignore this output if you do not wish to - // use flow control. - output reg o_rts_n; - output wire o_uart_rx_int, o_uart_tx_int, - o_uart_rxfifo_int, o_uart_txfifo_int; + // Register and signal declarations + // {{{ wire tx_busy; - + reg [30:0] uart_setup; + // Receiver + wire rx_stb, rx_break, rx_perr, rx_ferr, ck_uart; + wire [7:0] rx_uart_data; + reg rx_uart_reset; + // Receive FIFO + wire rx_empty_n, rx_fifo_err; + wire [7:0] rxf_wb_data; + wire [15:0] rxf_status; + reg rxf_wb_read; // + wire [(LCLLGFLEN-1):0] check_cutoff; + reg r_rx_perr, r_rx_ferr; + wire [31:0] wb_rx_data; + // The transmitter + wire tx_empty_n, txf_err, tx_break; + wire [7:0] tx_data; + wire [15:0] txf_status; + reg txf_wb_write, tx_uart_reset; + reg [7:0] txf_wb_data; + // + wire [31:0] wb_tx_data; + wire [31:0] wb_fifo_data; + reg [1:0] r_wb_addr; + reg r_wb_ack; + // }}} + + // uart_setup + // {{{ // The UART setup parameters: bits per byte, stop bits, parity, and // baud rate are all captured within this uart_setup register. // - reg [30:0] uart_setup; initial uart_setup = INITIAL_SETUP | ((HARDWARE_FLOW_CONTROL_PRESENT==1'b0)? 31'h40000000 : 0); always @(posedge i_clk) - // Under wishbone rules, a write takes place any time i_wb_stb - // is high. If that's the case, and if the write was to the - // setup address, then set us up for the new parameters. - if ((i_wb_stb)&&(i_wb_addr == UART_SETUP)&&(i_wb_we)) - uart_setup <= { - (i_wb_data[30]) + // Under wishbone rules, a write takes place any time i_wb_stb + // is high. If that's the case, and if the write was to the + // setup address, then set us up for the new parameters. + if ((i_wb_stb)&&(i_wb_addr == UART_SETUP)&&(i_wb_we)) + begin + if (i_wb_sel[0]) + uart_setup[7:0] <= i_wb_data[7:0]; + if (i_wb_sel[1]) + uart_setup[15:8] <= i_wb_data[15:8]; + if (i_wb_sel[2]) + uart_setup[23:16] <= i_wb_data[23:16]; + if (i_wb_sel[3]) + uart_setup[30:24] <= { (i_wb_data[30]) ||(!HARDWARE_FLOW_CONTROL_PRESENT), - i_wb_data[29:0] }; - - ///////////////////////////////////////// - // + i_wb_data[29:24] }; + end + // }}} + //////////////////////////////////////////////////////////////////////// // - // First, the UART receiver + // The UART receiver + // {{{ + //////////////////////////////////////////////////////////////////////// // // - ///////////////////////////////////////// - - // First the wires/registers this receiver depends upon - wire rx_stb, rx_break, rx_perr, rx_ferr, ck_uart; - wire [7:0] rx_uart_data; - reg rx_uart_reset; + // The receiver itself + // {{{ // Here's our UART receiver. Basically, it accepts our setup wires, // the UART input, a clock, and a reset line, and produces outputs: // a stb (true when new data is ready), and an 8-bit data out value // valid when stb is high. `ifdef USE_LITE_UART - rxuartlite #(INITIAL_SETUP[23:0]) + // {{{ + rxuartlite #(.CLOCKS_PER_BAUD(INITIAL_SETUP[23:0])) rx(i_clk, i_uart_rx, rx_stb, rx_uart_data); assign rx_break = 1'b0; assign rx_perr = 1'b0; assign rx_ferr = 1'b0; assign ck_uart = 1'b0; + // }}} `else + // {{{ // The full receiver also produces a break value (true during a break // cond.), and parity/framing error flags--also valid when stb is true. - rxuart #(INITIAL_SETUP) rx(i_clk, (i_rst)||(rx_uart_reset), + rxuart #(.INITIAL_SETUP(INITIAL_SETUP)) rx(i_clk, (i_reset)||(rx_uart_reset), uart_setup, i_uart_rx, rx_stb, rx_uart_data, rx_break, rx_perr, rx_ferr, ck_uart); // The real trick is ... now that we have this extra data, what do we do // with it? + // }}} `endif + // }}} - - // We place it into a receiver FIFO. - // - // Here's the declarations for the wires it needs. - wire rx_empty_n, rx_fifo_err; - wire [7:0] rxf_wb_data; - wire [15:0] rxf_status; - reg rxf_wb_read; + // The receive FIFO + // {{{ + // We place new arriving data into a receiver FIFO. // // And here's the FIFO proper. // @@ -164,12 +189,21 @@ module wbuart(i_clk, i_rst, // four status-type values: 1) is it non-empty, 2) is the FIFO over half // full, 3) a 16-bit status register, containing info regarding how full // the FIFO truly is, and 4) an error indicator. - ufifo #(.LGFLEN(LCLLGFLEN), .RXFIFO(1)) - rxfifo(i_clk, (i_rst)||(rx_break)||(rx_uart_reset), - rx_stb, rx_uart_data, - rx_empty_n, - rxf_wb_read, rxf_wb_data, - rxf_status, rx_fifo_err); + ufifo #( + // {{{ + .LGFLEN(LCLLGFLEN), .RXFIFO(1) + // }}} + ) rxfifo( + // {{{ + .i_clk(i_clk), .i_reset((i_reset)||(rx_break)||(rx_uart_reset)), + .i_wr(rx_stb), .i_data(rx_uart_data), + .o_empty_n(rx_empty_n), + .i_rd(rxf_wb_read), .o_data(rxf_wb_data), + .o_status(rxf_status), .o_err(rx_fifo_err) + // }}} + ); + // }}} + assign o_uart_rxfifo_int = rxf_status[1]; // We produce four interrupts. One of the receive interrupts indicates @@ -177,6 +211,8 @@ module wbuart(i_clk, i_rst, // the CPU. assign o_uart_rx_int = rxf_status[0]; + // o_rts_n + // {{{ // The clear to send line, which may be ignored, but which we set here // to be true any time the FIFO has fewer than N-2 items in it. // Why not N-1? Because at N-1 we are totally full, but already so full @@ -184,92 +220,102 @@ module wbuart(i_clk, i_rst, // receive it. (Transmit might've started on the next character by the // time we set this--thus we need to set it to one, one character before // necessary). - wire [(LCLLGFLEN-1):0] check_cutoff; assign check_cutoff = -3; always @(posedge i_clk) o_rts_n <= ((HARDWARE_FLOW_CONTROL_PRESENT) &&(!uart_setup[30]) &&(rxf_status[(LCLLGFLEN+1):2] > check_cutoff)); + // }}} + // rxf_wb_read + // {{{ // If the bus requests that we read from the receive FIFO, we need to // tell this to the receive FIFO. Note that because we are using a // clock here, the output from the receive FIFO will necessarily be // delayed by an extra clock. initial rxf_wb_read = 1'b0; always @(posedge i_clk) - rxf_wb_read <= (i_wb_stb)&&(i_wb_addr[1:0]==UART_RXREG) + rxf_wb_read <= (i_wb_stb)&&(i_wb_addr[1:0]== UART_RXREG) &&(!i_wb_we); + // }}} + // r_rx_perr, r_rx_ferr -- parity and framing errors + // {{{ // Now, let's deal with those RX UART errors: both the parity and frame // errors. As you may recall, these are valid only when rx_stb is // valid, so we need to hold on to them until the user reads them via // a UART read request.. - reg r_rx_perr, r_rx_ferr; initial r_rx_perr = 1'b0; initial r_rx_ferr = 1'b0; always @(posedge i_clk) - if ((rx_uart_reset)||(rx_break)) - begin - // Clear the error - r_rx_perr <= 1'b0; - r_rx_ferr <= 1'b0; - end else if ((i_wb_stb) - &&(i_wb_addr[1:0]==UART_RXREG)&&(i_wb_we)) + if ((rx_uart_reset)||(rx_break)) + begin + // Clear the error + r_rx_perr <= 1'b0; + r_rx_ferr <= 1'b0; + end else if ((i_wb_stb) + &&(i_wb_addr[1:0]== UART_RXREG)&&(i_wb_we)) + begin + // Reset the error lines if a '1' is ever written to + // them, otherwise leave them alone. + // + if (i_wb_sel[1]) begin - // Reset the error lines if a '1' is ever written to - // them, otherwise leave them alone. - // r_rx_perr <= (r_rx_perr)&&(~i_wb_data[9]); r_rx_ferr <= (r_rx_ferr)&&(~i_wb_data[10]); - end else if (rx_stb) - begin - // On an rx_stb, capture any parity or framing error - // indications. These aren't kept with the data rcvd, - // but rather kept external to the FIFO. As a result, - // if you get a parity or framing error, you will never - // know which data byte it was associated with. - // For now ... that'll work. - r_rx_perr <= (r_rx_perr)||(rx_perr); - r_rx_ferr <= (r_rx_ferr)||(rx_ferr); end + end else if (rx_stb) + begin + // On an rx_stb, capture any parity or framing error + // indications. These aren't kept with the data rcvd, + // but rather kept external to the FIFO. As a result, + // if you get a parity or framing error, you will never + // know which data byte it was associated with. + // For now ... that'll work. + r_rx_perr <= (r_rx_perr)||(rx_perr); + r_rx_ferr <= (r_rx_ferr)||(rx_ferr); + end + // }}} + // rx_uart_reset + // {{{ initial rx_uart_reset = 1'b1; always @(posedge i_clk) - if ((i_rst)||((i_wb_stb)&&(i_wb_addr[1:0]==UART_SETUP)&&(i_wb_we))) - // The receiver reset, always set on a master reset - // request. - rx_uart_reset <= 1'b1; - else if ((i_wb_stb)&&(i_wb_addr[1:0]==UART_RXREG)&&(i_wb_we)) - // Writes to the receive register will command a receive - // reset anytime bit[12] is set. - rx_uart_reset <= i_wb_data[12]; - else - rx_uart_reset <= 1'b0; + if ((i_reset)||((i_wb_stb)&&(i_wb_addr[1:0]== UART_SETUP)&&(i_wb_we))) + // The receiver reset, always set on a master reset + // request. + rx_uart_reset <= 1'b1; + else if ((i_wb_stb)&&(i_wb_addr[1:0]== UART_RXREG)&&(i_wb_we)&&i_wb_sel[1]) + // Writes to the receive register will command a receive + // reset anytime bit[12] is set. + rx_uart_reset <= i_wb_data[12]; + else + rx_uart_reset <= 1'b0; + // }}} + // wb_rx_data + // {{{ // Finally, we'll construct a 32-bit value from these various wires, // to be returned over the bus on any read. These include the data // that would be read from the FIFO, an error indicator set upon // reading from an empty FIFO, a break indicator, and the frame and // parity error signals. - wire [31:0] wb_rx_data; assign wb_rx_data = { 16'h00, 3'h0, rx_fifo_err, rx_break, rx_ferr, r_rx_perr, !rx_empty_n, rxf_wb_data}; - - ///////////////////////////////////////// - // + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// // - // Then the UART transmitter + // The UART transmitter + // {{{ + //////////////////////////////////////////////////////////////////////// // // - ///////////////////////////////////////// - wire tx_empty_n, txf_err, tx_break; - wire [7:0] tx_data; - wire [15:0] txf_status; - reg txf_wb_write, tx_uart_reset; - reg [7:0] txf_wb_data; + // txf_wb_write, txf_wb_data + // {{{ // Unlike the receiver which goes from RXUART -> UFIFO -> WB, the // transmitter basically goes WB -> UFIFO -> TXUART. Hence, to build // support for the transmitter, we start with the command to write data @@ -282,12 +328,13 @@ module wbuart(i_clk, i_rst, always @(posedge i_clk) begin txf_wb_write <= (i_wb_stb)&&(i_wb_addr == UART_TXREG) - &&(i_wb_we); + &&(i_wb_we)&&(i_wb_sel[0]); txf_wb_data <= i_wb_data[7:0]; end + // }}} // Transmit FIFO - // + // {{{ // Most of this is just wire management. The TX FIFO is identical in // implementation to the RX FIFO (theyre both UFIFOs), but the TX // FIFO is fed from the WB and read by the transmitter. Some key @@ -295,12 +342,23 @@ module wbuart(i_clk, i_rst, // break. We read from the FIFO any time the UART transmitter is idle. // and ... we just set the values (above) for controlling writing into // this. - ufifo #(.LGFLEN(LGFLEN), .RXFIFO(0)) - txfifo(i_clk, (tx_break)||(tx_uart_reset), - txf_wb_write, txf_wb_data, - tx_empty_n, - (!tx_busy)&&(tx_empty_n), tx_data, - txf_status, txf_err); + ufifo #( + // {{{ + .LGFLEN(LGFLEN), .RXFIFO(0) + // }}} + ) txfifo( + // {{{ + .i_clk(i_clk), .i_reset((tx_break)||(tx_uart_reset)), + .i_wr(txf_wb_write), .i_data(txf_wb_data), + .o_empty_n(tx_empty_n), + .i_rd((!tx_busy)&&(tx_empty_n)), .o_data(tx_data), + .o_status(txf_status), .o_err(txf_err) + // }}} + ); + // }}} + + // Transmit interrupts + // {{{ // Let's create two transmit based interrupts from the FIFO for the CPU. // The first will be true any time the FIFO has at least one open // position within it. @@ -309,10 +367,11 @@ module wbuart(i_clk, i_rst, // full, allowing us a change to always keep it (near) fully // charged. assign o_uart_txfifo_int = txf_status[1]; + // }}} -`ifndef USE_LITE_UART // Break logic - // +`ifndef USE_LITE_UART + // {{{ // A break in a UART controller is any time the UART holds the line // low for an extended period of time. Here, we capture the wb_data[9] // wire, on writes, as an indication we wish to break. As long as you @@ -322,36 +381,51 @@ module wbuart(i_clk, i_rst, reg r_tx_break; initial r_tx_break = 1'b0; always @(posedge i_clk) - if (i_rst) - r_tx_break <= 1'b0; - else if ((i_wb_stb)&&(i_wb_addr[1:0]==UART_TXREG)&&(i_wb_we)) - r_tx_break <= i_wb_data[9]; + if (i_reset) + r_tx_break <= 1'b0; + else if ((i_wb_stb)&&(i_wb_addr[1:0]== UART_TXREG)&&(i_wb_we) + &&(i_wb_sel[1])) + r_tx_break <= i_wb_data[9]; + assign tx_break = r_tx_break; + // }}} `else + // {{{ assign tx_break = 1'b0; + // }}} `endif // TX-Reset logic - // + // {{{ // This is nearly identical to the RX reset logic above. Basically, // any time someone writes to bit [12] the transmitter will go through // a reset cycle. Keep bit [12] low, and everything will proceed as // normal. initial tx_uart_reset = 1'b1; always @(posedge i_clk) - if((i_rst)||((i_wb_stb)&&(i_wb_addr == UART_SETUP)&&(i_wb_we))) - tx_uart_reset <= 1'b1; - else if ((i_wb_stb)&&(i_wb_addr[1:0]==UART_TXREG)&&(i_wb_we)) - tx_uart_reset <= i_wb_data[12]; - else - tx_uart_reset <= 1'b0; + if((i_reset)||((i_wb_stb)&&(i_wb_addr == UART_SETUP)&&(i_wb_we))) + tx_uart_reset <= 1'b1; + else if ((i_wb_stb)&&(i_wb_addr[1:0]== UART_TXREG)&&(i_wb_we) && i_wb_sel[1]) + tx_uart_reset <= i_wb_data[12]; + else + tx_uart_reset <= 1'b0; + // }}} + // The actuall transmitter itself `ifdef USE_LITE_UART - txuartlite #(INITIAL_SETUP[23:0]) tx(i_clk, (tx_empty_n), tx_data, + // {{{ + txuartlite #(.CLOCKS_PER_BAUD(INITIAL_SETUP[23:0])) tx(i_clk, (tx_empty_n), tx_data, o_uart_tx, tx_busy); + // }}} `else + // cts_n + // {{{ wire cts_n; assign cts_n = (HARDWARE_FLOW_CONTROL_PRESENT)&&(i_cts_n); + // }}} + + // The *full* transmitter impleemntation + // {{{ // Finally, the UART transmitter module itself. Note that we haven't // connected the reset wire. Transmitting is as simple as setting // the stb value (here set to tx_empty_n) and the data. When these @@ -361,76 +435,103 @@ module wbuart(i_clk, i_rst, // we read it here. (You might notice above, we register a read any // time (tx_empty_n) and (!tx_busy) are both true---the condition for // starting to transmit a new byte.) - txuart #(INITIAL_SETUP) tx(i_clk, 1'b0, uart_setup, + txuart #(.INITIAL_SETUP(INITIAL_SETUP)) tx(i_clk, 1'b0, uart_setup, r_tx_break, (tx_empty_n), tx_data, cts_n, o_uart_tx, tx_busy); + // }}} `endif + // wb_tx_data + // {{{ // Now that we are done with the chain, pick some wires for the user // to read on any read of the transmit port. // // This port is different from reading from the receive port, since // there are no side effects. (Reading from the receive port advances // the receive FIFO, here only writing to the transmit port advances the - // transmit FIFO--hence the read values are free for ... whatever.) + // transmit FIFO--hence the read values are free for ... whatever.) // We choose here to provide information about the transmit FIFO // (txf_err, txf_half_full, txf_full_n), information about the current // voltage on the line (o_uart_tx)--and even the voltage on the receive // line (ck_uart), as well as our current setting of the break and // whether or not we are actively transmitting. - wire [31:0] wb_tx_data; assign wb_tx_data = { 16'h00, - i_cts_n, txf_status[1:0], txf_err, - ck_uart, o_uart_tx, tx_break, (tx_busy|txf_status[0]), - (tx_busy|txf_status[0])?txf_wb_data:8'b00}; + i_cts_n, txf_status[1:0], txf_err, + ck_uart, o_uart_tx, tx_break, (tx_busy|txf_status[0]), + (tx_busy|txf_status[0])?txf_wb_data:8'b00}; + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Bus / register handling + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // wb_fifo_data + // {{{ // Each of the FIFO's returns a 16 bit status value. This value tells - // us both how big the FIFO is, as well as how much of the FIFO is in + // us both how big the FIFO is, as well as how much of the FIFO is in // use. Let's merge those two status words together into a word we // can use when reading about the FIFO. - wire [31:0] wb_fifo_data; assign wb_fifo_data = { txf_status, rxf_status }; + // }}} + // r_wb_addr + // {{{ // You may recall from above that reads take two clocks. Hence, we - // need to delay the address decoding for a clock until the data is + // need to delay the address decoding for a clock until the data is // ready. We do that here. - reg [1:0] r_wb_addr; always @(posedge i_clk) r_wb_addr <= i_wb_addr; + // }}} - // Likewise, the acknowledgement is delayed by one clock. - reg r_wb_ack; - + // r_wb_ack + // {{{ initial r_wb_ack = 1'b0; always @(posedge i_clk) // We'll ACK in two clocks - r_wb_ack <= i_wb_stb; + r_wb_ack <= (!i_reset)&&(i_wb_stb); + // }}} + // o_wb_ack + // {{{ initial o_wb_ack = 1'b0; always @(posedge i_clk) // Okay, time to set the ACK - o_wb_ack <= i_wb_cyc && r_wb_ack; + o_wb_ack <= (!i_reset)&&(r_wb_ack)&&(i_wb_cyc); + // }}} + // o_wb_data + // {{{ // Finally, set the return data. This data must be valid on the same // clock o_wb_ack is high. On all other clocks, it is irrelelant--since // no one cares, no one is reading it, it gets lost in the mux in the // interconnect, etc. For this reason, we can just simplify our logic. always @(posedge i_clk) - casez(r_wb_addr) - UART_SETUP: o_wb_data <= { 1'b0, uart_setup }; - UART_FIFO: o_wb_data <= wb_fifo_data; - UART_RXREG: o_wb_data <= wb_rx_data; - UART_TXREG: o_wb_data <= wb_tx_data; - endcase + casez(r_wb_addr) + UART_SETUP: o_wb_data <= { 1'b0, uart_setup }; + UART_FIFO: o_wb_data <= wb_fifo_data; + UART_RXREG: o_wb_data <= wb_rx_data; + UART_TXREG: o_wb_data <= wb_tx_data; + endcase + // }}} + // o_wb_stall + // {{{ // This device never stalls. Sure, it takes two clocks, but they are // pipelined, and nothing stalls that pipeline. (Creates FIFO errors, // perhaps, but doesn't stall the pipeline.) Hence, we can just // set this value to zero. assign o_wb_stall = 1'b0; + // }}} + // }}} // Make verilator happy + // {{{ // verilator lint_off UNUSED - wire [33:0] unused; - assign unused = { i_rst, i_wb_cyc, i_wb_data }; + wire unused; + assign unused = &{ 1'b0, i_wb_data[31] }; // verilator lint_on UNUSED - + // }}} endmodule diff --git a/rtl/wbubus/wbconsole.v b/rtl/wbubus/wbconsole.v index 7f5aa14..583d8cf 100644 --- a/rtl/wbubus/wbconsole.v +++ b/rtl/wbubus/wbconsole.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbconsole.v -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Unlilke wbuart-insert.v, this is a full blown wishbone core @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,30 +30,52 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbconsole(i_clk, i_rst, - // - i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, i_wb_sel, - o_wb_stall, o_wb_ack, o_wb_data, +// }}} +module wbconsole #( + parameter [3:0] LGFLEN = 4 + ) ( + // {{{ + input wire i_clk, i_reset, + // Wishbone inputs + // {{{ + input wire i_wb_cyc, i_wb_stb, i_wb_we, + input wire [1:0] i_wb_addr, + input wire [31:0] i_wb_data, + input wire [3:0] i_wb_sel, + output wire o_wb_stall, + output reg o_wb_ack, + output reg [31:0] o_wb_data, + // }}} + // TX + // {{{ + output wire o_uart_stb, + output wire [6:0] o_uart_data, + input wire i_uart_busy, + // }}} + // RX + // {{{ + input wire i_uart_stb, + input wire [6:0] i_uart_data, + // }}} + // Interrupts + output wire o_uart_rx_int, o_uart_tx_int, + o_uart_rxfifo_int, o_uart_txfifo_int, // - o_uart_stb, o_uart_data, i_uart_busy, - i_uart_stb, i_uart_data, - // - o_uart_rx_int, o_uart_tx_int, - o_uart_rxfifo_int, o_uart_txfifo_int, - // - o_debug); - parameter [3:0] LGFLEN = 4; - parameter [0:0] HARDWARE_FLOW_CONTROL_PRESENT = 1'b1; + output reg [31:0] o_debug + // }}} + ); + + // Local declarations + // {{{ + // parameter [0:0] HARDWARE_FLOW_CONTROL_PRESENT = 1'b1; // Perform a simple/quick bounds check on the log FIFO length, to make // sure its within the bounds we can support with our current // interface. @@ -65,47 +87,35 @@ module wbconsole(i_clk, i_rst, UART_FIFO = 2'b01, UART_RXREG = 2'b10, UART_TXREG = 2'b11; - // - input wire i_clk, i_rst; - // Wishbone inputs - input wire i_wb_cyc, i_wb_stb, i_wb_we; - input wire [1:0] i_wb_addr; - input wire [31:0] i_wb_data; - input wire [3:0] i_wb_sel; - output wire o_wb_stall; - output reg o_wb_ack; - output reg [31:0] o_wb_data; - // - output wire o_uart_stb; - output wire [6:0] o_uart_data; - input wire i_uart_busy; - // - input wire i_uart_stb; - input wire [6:0] i_uart_data; - // - output wire o_uart_rx_int, o_uart_tx_int, - o_uart_rxfifo_int, o_uart_txfifo_int; - // - output reg [31:0] o_debug; - ///////////////////////////////////////// - // - // - // First, the receiver - // - // - ///////////////////////////////////////// - - - // We place it into a receiver FIFO. - // - // Here's the declarations for the wires it needs. reg rx_uart_reset; wire rx_empty_n, rx_fifo_err; wire [6:0] rxf_wb_data; wire [15:0] rxf_status; reg rxf_wb_read; + + wire [31:0] wb_rx_data; + + wire tx_empty_n, txf_err; + wire [15:0] txf_status; + reg txf_wb_write, tx_uart_reset; + reg [6:0] txf_wb_data; + + wire [31:0] wb_tx_data; + wire [31:0] wb_fifo_data; + reg [1:0] r_wb_addr; + reg r_wb_ack; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // First, the receiver + // {{{ + //////////////////////////////////////////////////////////////////////// + // // + + // We place it into a receiver FIFO. + // And here's the FIFO proper. // // Note that the FIFO will be cleared upon any reset: either if there's @@ -118,12 +128,18 @@ module wbconsole(i_clk, i_rst, // four status-type values: 1) is it non-empty, 2) is the FIFO over half // full, 3) a 16-bit status register, containing info regarding how full // the FIFO truly is, and 4) an error indicator. - ufifo #(.LGFLEN(LCLLGFLEN), .BW(7), .RXFIFO(1)) - rxfifo(i_clk, (i_rst)||(rx_uart_reset), + ufifo #( + .LGFLEN(LCLLGFLEN), .BW(7), .RXFIFO(1) + ) rxfifo( + // {{{ + i_clk, (i_reset)||(rx_uart_reset), i_uart_stb, i_uart_data, rx_empty_n, rxf_wb_read, rxf_wb_data, - rxf_status, rx_fifo_err); + rxf_status, rx_fifo_err + // }}} + ); + assign o_uart_rxfifo_int = rxf_status[1]; // We produce four interrupts. One of the receive interrupts indicates @@ -142,7 +158,7 @@ module wbconsole(i_clk, i_rst, initial rx_uart_reset = 1'b1; always @(posedge i_clk) - if ((i_rst)||((i_wb_stb)&&(i_wb_addr[1:0]==UART_SETUP)&&(i_wb_we))) + if ((i_reset)||((i_wb_stb)&&(i_wb_addr[1:0]==UART_SETUP)&&(i_wb_we))) // The receiver reset, always set on a master reset // request. rx_uart_reset <= 1'b1; @@ -158,23 +174,18 @@ module wbconsole(i_clk, i_rst, // that would be read from the FIFO, an error indicator set upon // reading from an empty FIFO, a break indicator, and the frame and // parity error signals. - wire [31:0] wb_rx_data; assign wb_rx_data = { 16'h00, 3'h0, rx_fifo_err, 1'b0, 1'b0, 1'b0, !rx_empty_n, 1'b0, rxf_wb_data}; - - ///////////////////////////////////////// - // + // }}} + //////////////////////////////////////////////////////////////////////// // // Then the UART transmitter + // {{{ + //////////////////////////////////////////////////////////////////////// // // - ///////////////////////////////////////// - wire tx_empty_n, txf_err; - wire [15:0] txf_status; - reg txf_wb_write, tx_uart_reset; - reg [6:0] txf_wb_data; // Unlike the receiver which goes from RXUART -> UFIFO -> WB, the // transmitter basically goes WB -> UFIFO -> TXUART. Hence, to build @@ -193,7 +204,7 @@ module wbconsole(i_clk, i_rst, end // Transmit FIFO - // + // {{{ // Most of this is just wire management. The TX FIFO is identical in // implementation to the RX FIFO (theyre both UFIFOs), but the TX // FIFO is fed from the WB and read by the transmitter. Some key @@ -201,12 +212,18 @@ module wbconsole(i_clk, i_rst, // break. We read from the FIFO any time the UART transmitter is idle. // and ... we just set the values (above) for controlling writing into // this. - ufifo #(.LGFLEN(LGFLEN), .BW(7), .RXFIFO(0)) - txfifo(i_clk, (tx_uart_reset), + ufifo #( + .LGFLEN(LGFLEN), .BW(7), .RXFIFO(0) + ) txfifo( + // {{{ + i_clk, (tx_uart_reset), txf_wb_write, txf_wb_data, tx_empty_n, (!i_uart_busy)&&(tx_empty_n), o_uart_data, - txf_status, txf_err); + txf_status, txf_err + // }}} + ); + // }}} assign o_uart_stb = tx_empty_n; @@ -220,19 +237,20 @@ module wbconsole(i_clk, i_rst, assign o_uart_txfifo_int = txf_status[1]; // TX-Reset logic - // + // {{{ // This is nearly identical to the RX reset logic above. Basically, // any time someone writes to bit [12] the transmitter will go through // a reset cycle. Keep bit [12] low, and everything will proceed as // normal. initial tx_uart_reset = 1'b1; always @(posedge i_clk) - if((i_rst)||((i_wb_stb)&&(i_wb_addr == UART_SETUP)&&(i_wb_we))) + if((i_reset)||((i_wb_stb)&&(i_wb_addr == UART_SETUP)&&(i_wb_we))) tx_uart_reset <= 1'b1; else if ((i_wb_stb)&&(i_wb_addr[1:0]==UART_TXREG)&&(i_wb_we)) tx_uart_reset <= i_wb_data[12]; else tx_uart_reset <= 1'b0; + // }}} // Now that we are done with the chain, pick some wires for the user // to read on any read of the transmit port. @@ -246,29 +264,33 @@ module wbconsole(i_clk, i_rst, // voltage on the line (o_uart_tx)--and even the voltage on the receive // line (ck_uart), as well as our current setting of the break and // whether or not we are actively transmitting. - wire [31:0] wb_tx_data; assign wb_tx_data = { 16'h00, 1'b0, txf_status[1:0], txf_err, 1'b0, o_uart_stb, 1'b0, (i_uart_busy|tx_empty_n), 1'b0,(i_uart_busy|tx_empty_n)?txf_wb_data:7'h0}; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Bus return handling + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // // Each of the FIFO's returns a 16 bit status value. This value tells // us both how big the FIFO is, as well as how much of the FIFO is in // use. Let's merge those two status words together into a word we // can use when reading about the FIFO. - wire [31:0] wb_fifo_data; assign wb_fifo_data = { txf_status, rxf_status }; // You may recall from above that reads take two clocks. Hence, we // need to delay the address decoding for a clock until the data is // ready. We do that here. - reg [1:0] r_wb_addr; always @(posedge i_clk) r_wb_addr <= i_wb_addr; // Likewise, the acknowledgement is delayed by one clock. - reg r_wb_ack; always @(posedge i_clk) // We'll ACK in two clocks r_wb_ack <= i_wb_stb; always @(posedge i_clk) // Okay, time to set the ACK @@ -291,6 +313,14 @@ module wbconsole(i_clk, i_rst, // perhaps, but doesn't stall the pipeline.) Hence, we can just // set this value to zero. assign o_wb_stall = 1'b0; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // A debug return + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // always @(posedge i_clk) begin @@ -313,11 +343,14 @@ module wbconsole(i_clk, i_rst, else o_debug[6:0] <= rxf_wb_data; end + // }}} // Make verilator happy + // {{{ // verilator lint_off UNUSED wire unused; assign unused = &{ 1'b0, i_wb_cyc, i_wb_data[31:13], i_wb_data[11:7], i_wb_sel }; // verilator lint_on UNUSED + // }}} endmodule diff --git a/rtl/wbubus/wbubus.v b/rtl/wbubus/wbubus.v index 5232284..3240733 100644 --- a/rtl/wbubus/wbubus.v +++ b/rtl/wbubus/wbubus.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbubus.v -// +// {{{ // Project: FPGA library // // Purpose: This is the top level file for the entire JTAG-USB to Wishbone @@ -15,11 +15,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -32,104 +32,181 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbubus(i_clk, i_rx_stb, i_rx_data, - o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, - i_wb_stall, i_wb_ack, i_wb_data, i_wb_err, - i_interrupt, - o_tx_stb, o_tx_data, i_tx_busy); - parameter LGWATCHDOG=19, - LGINPUT_FIFO=6, - LGOUTPUT_FIFO=10; - parameter [0:0] CMD_PORT_OFF_UNTIL_ACCESSED = 1'b1; - input wire i_clk; - input wire i_rx_stb; - input wire [7:0] i_rx_data; - output wire o_wb_cyc, o_wb_stb, o_wb_we; - output wire [31:0] o_wb_addr, o_wb_data; - input wire i_wb_stall, i_wb_ack; - input wire [31:0] i_wb_data; - input wire i_wb_err; - input wire i_interrupt; - output wire o_tx_stb; - output wire [7:0] o_tx_data; - input wire i_tx_busy; - // output wire o_dbg; - - +// }}} +module wbubus #( + // {{{ + parameter LGWATCHDOG=19, + LGINPUT_FIFO=6, + LGOUTPUT_FIFO=10, + parameter [0:0] CMD_PORT_OFF_UNTIL_ACCESSED = 1'b1 + // }}} + ) ( + // {{{ + input wire i_clk, + input wire i_reset, + // RX + // {{{ + input wire i_rx_stb, + input wire [7:0] i_rx_data, + // }}} + // Wishbone master + // {{{ + output wire o_wb_cyc, o_wb_stb, o_wb_we, + output wire [31:0] o_wb_addr, o_wb_data, + input wire i_wb_stall, i_wb_ack, + input wire [31:0] i_wb_data, + input wire i_wb_err, + // }}} + input wire i_interrupt, + // TX + // {{{ + output wire o_tx_stb, + output wire [7:0] o_tx_data, + input wire i_tx_busy + // }}} + // , output wire o_dbg; + // }}} + ); + + // Local declarations + // {{{ + wire soft_reset; reg r_wdt_reset; - reg cmd_port_active; + wire cmd_port_active, w_tx_stb; + wire in_stb; + wire [35:0] in_word; + wire w_bus_busy, fifo_in_stb, exec_stb, w_bus_reset; + wire [35:0] fifo_in_word, exec_word; + wire ofifo_err; + reg [(LGWATCHDOG-1):0] r_wdt_timer; + // wire [30:0] out_dbg; + // }}} + // cmd_port_active + // {{{ generate if (CMD_PORT_OFF_UNTIL_ACCESSED) begin - initial cmd_port_active = 1'b0; + reg r_cmd_port_active; + + initial r_cmd_port_active = 1'b0; always @(posedge i_clk) if (i_rx_stb && i_rx_data[7]) - cmd_port_active <= 1'b1; + r_cmd_port_active <= 1'b1; + + assign cmd_port_active = r_cmd_port_active; end else begin - always @(*) - cmd_port_active = 1'b1; + assign cmd_port_active = 1'b1; end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // // Decode ASCII input requests into WB bus cycle requests - wire in_stb; - wire [35:0] in_word; - wbuinput getinput(i_clk, i_rx_stb, i_rx_data, in_stb, in_word); - - wire w_bus_busy, fifo_in_stb, exec_stb, w_bus_reset; - wire [35:0] fifo_in_word, exec_word; - - generate - if (LGINPUT_FIFO < 2) + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + wbuinput + getinput( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + .i_stb(i_rx_stb), .i_byte(i_rx_data), + .o_soft_reset(soft_reset), + .o_stb(in_stb), .o_codword(in_word) + // }}} + ); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // The input FIFO + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + generate if (LGINPUT_FIFO < 2) begin : NO_INPUT_FIFO - assign fifo_in_stb = in_stb; - assign fifo_in_word = in_word; - assign w_bus_reset = 1'b0; + assign fifo_in_stb = in_stb; + assign fifo_in_word = in_word; + assign w_bus_reset = soft_reset; end else begin : INPUT_FIFO wire ififo_empty_n, ififo_err; + assign fifo_in_stb = (~w_bus_busy)&&(ififo_empty_n); assign w_bus_reset = r_wdt_reset; - wbufifo #(36,LGINPUT_FIFO) padififo(i_clk, w_bus_reset, - in_stb, in_word, fifo_in_stb, fifo_in_word, - ififo_empty_n, ififo_err); + + wbufifo #( + // {{{ + .BW(36),.LGFLEN(LGINPUT_FIFO) + // }}} + ) padififo( + // {{{ + .i_clk(i_clk), .i_reset(w_bus_reset), + .i_wr(in_stb), .i_data(in_word), + .i_rd(fifo_in_stb), .o_data(fifo_in_word), + .o_empty_n(ififo_empty_n), .o_err(ififo_err) + // }}} + ); // verilator lint_off UNUSED wire gen_unused; assign gen_unused = ififo_err; // verilator lint_on UNUSED end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Run the bus + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // // Take requests in, Run the bus, send results out // This only works if no requests come in while requests // are pending. - wbuexec runwb(i_clk, r_wdt_reset, fifo_in_stb, fifo_in_word, w_bus_busy, - o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, - i_wb_stall, i_wb_ack, i_wb_err, i_wb_data, - exec_stb, exec_word); - - wire ofifo_err, w_tx_stb; - // wire [30:0] out_dbg; - wbuoutput #(LGOUTPUT_FIFO) wroutput(i_clk, w_bus_reset, - exec_stb, exec_word, - o_wb_cyc, i_interrupt, exec_stb, - w_tx_stb, o_tx_data, i_tx_busy || !cmd_port_active, ofifo_err); - assign o_tx_stb = cmd_port_active & w_tx_stb; + wbuexec + runwb( + // {{{ + .i_clk(i_clk), .i_reset(r_wdt_reset), + .i_stb(fifo_in_stb), .i_codword(fifo_in_word), + .o_busy(w_bus_busy), + .o_wb_cyc(o_wb_cyc), .o_wb_stb(o_wb_stb), .o_wb_we(o_wb_we), + .o_wb_addr(o_wb_addr), .o_wb_data(o_wb_data), + .i_wb_stall(i_wb_stall), .i_wb_ack(i_wb_ack), + .i_wb_data(i_wb_data), .i_wb_err(i_wb_err), + .o_stb(exec_stb), .o_codword(exec_word) + // }}} + ); + // }}} + + wbuoutput #( + LGOUTPUT_FIFO + ) wroutput( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), .i_soft_reset(w_bus_reset), + .i_stb(exec_stb), .i_codword(exec_word), + .i_wb_cyc(o_wb_cyc), .i_int(i_interrupt), .i_bus_busy(exec_stb), + .o_stb(w_tx_stb), .o_char(o_tx_data), .i_tx_busy(i_tx_busy), + .o_fifo_err(ofifo_err) + // }}} + ); + + assign o_tx_stb = w_tx_stb && cmd_port_active; // verilator lint_off UNUSED wire ofifo_unused; @@ -137,10 +214,11 @@ module wbubus(i_clk, i_rx_stb, i_rx_data, // verilator lint_on UNUSED // Add in a watchdog timer to the bus - reg [(LGWATCHDOG-1):0] r_wdt_timer; + // {{{ initial r_wdt_reset = 1'b0; initial r_wdt_timer = 0; always @(posedge i_clk) + begin if ((~o_wb_cyc)||(i_wb_ack)) begin r_wdt_timer <= 0; @@ -154,5 +232,10 @@ module wbubus(i_clk, i_rx_stb, i_rx_data, r_wdt_reset <= 1'b0; end + if (soft_reset) + r_wdt_reset <= 1'b1; + end + // }}} + endmodule diff --git a/rtl/wbubus/wbucompactlines.v b/rtl/wbubus/wbucompactlines.v index 76958f2..2d79001 100644 --- a/rtl/wbubus/wbucompactlines.v +++ b/rtl/wbubus/wbucompactlines.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbucompactlines.v -// +// {{{ // Project: FPGA library // // Purpose: Removes 'end of line' characters placed at the end of every @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,8 +31,9 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // // @@ -54,88 +55,124 @@ // last word. If the last word was a // `default_nettype none -// -module wbucompactlines(i_clk, i_stb, i_nl_hexbits, o_stb, o_nl_hexbits, - i_bus_busy, i_tx_busy, o_busy); +// }}} +module wbucompactlines ( + // {{{ + input wire i_clk, i_reset, i_stb, + input wire [6:0] i_nl_hexbits, + output reg o_stb, + output reg [6:0] o_nl_hexbits, + input wire i_bus_busy, + input wire i_tx_busy, + output wire o_busy + // }}} + ); + + // Local declarations + // {{{ localparam [6:0] MAX_LINE_LENGTH = 79; localparam [6:0] TRIGGER_LENGTH = (MAX_LINE_LENGTH-6); - input wire i_clk, i_stb; - input wire [6:0] i_nl_hexbits; - output reg o_stb; - output reg [6:0] o_nl_hexbits; - input wire i_bus_busy; - input wire i_tx_busy; - output wire o_busy; - reg last_out_nl, last_in_nl, full_line, r_busy; reg [6:0] linelen; + // }}} + // last_out_nl + // {{{ initial last_out_nl = 1'b1; always @(posedge i_clk) - if ((!i_tx_busy)&&(o_stb)) + if (i_reset) + last_out_nl <= 1'b1; + else if ((!i_tx_busy)&&(o_stb)) last_out_nl <= (o_nl_hexbits[6]); + // }}} + // last_in_nl + // {{{ initial last_in_nl = 1'b1; always @(posedge i_clk) - if ((i_stb)&&(!o_busy)) + if (i_reset) + last_in_nl <= 1'b1; + else if ((i_stb)&&(!o_busy)) last_in_nl <= (i_nl_hexbits[6]); + // }}} + // linelen + // {{{ // Now, let's count how long our lines are initial linelen = 7'h00; always @(posedge i_clk) - if ((!i_tx_busy)&&(o_stb)) + if (i_reset) + linelen <= 0; + else if ((!i_tx_busy)&&(o_stb)) begin if (o_nl_hexbits[6]) linelen <= 0; else linelen <= linelen + 7'h1; end + // }}} + // full_line + // {{{ initial full_line = 1'b0; always @(posedge i_clk) - if ((!i_tx_busy)&&(o_stb)) + if (i_reset) + full_line <= 0; + else if ((!i_tx_busy)&&(o_stb)) begin if (o_nl_hexbits[6]) full_line <= 0; else full_line <= (linelen >= TRIGGER_LENGTH); end + // }}} - + // o_stb, o_nl_hexbits + // {{{ // Now that we know whether or not the last character was a newline, // and indeed how many characters we have in any given line, we can // selectively remove newlines from our output stream. initial o_stb = 1'b0; always @(posedge i_clk) - if ((i_stb)&&(!o_busy)) begin - // Only accept incoming newline requests if our line is already - // full, otherwise quietly suppress them - o_stb <= (full_line)||(!i_nl_hexbits[6]); - o_nl_hexbits <= i_nl_hexbits; - end else if (!o_busy) - begin // Send an EOL if we are idle + if ((i_stb)&&(!o_busy)) + begin + // Only accept incoming newline requests if our line is + // already full, otherwise quietly suppress them + o_stb <= (full_line)||(!i_nl_hexbits[6]); + o_nl_hexbits <= i_nl_hexbits; + end else if (!o_busy) + begin // Send an EOL if we are idle - // Without a request, we'll add a newline, but only if - // 1. There's nothing coming down the channel (!bus_busy) - // 2. What we last sent wasn't a new-line - // 3. The last thing that came in was a newline - // In otherwords, we can resurrect one of the newlines we - // squashed above - o_stb <= (!i_tx_busy)&&(!i_bus_busy)&&(!last_out_nl)&&(last_in_nl); - o_nl_hexbits <= 7'h40; - end else if (!i_tx_busy) - o_stb <= 1'b0; + // Without a request, we'll add a newline, but only if + // 1. There's nothing coming down the channel + // (!bus_busy) + // 2. What we last sent wasn't a new-line + // 3. The last thing that came in was a newline + // In otherwords, we can resurrect one of the newlines + // we squashed above + o_stb <= (!i_tx_busy)&&(!i_bus_busy)&&(!last_out_nl)&&(last_in_nl); + o_nl_hexbits <= 7'h40; + end else if (!i_tx_busy) + o_stb <= 1'b0; + + if (i_reset) + o_stb <= 1'b0; + end + // }}} + // o_busy + // {{{ initial r_busy = 1'b0; always @(posedge i_clk) r_busy <= (o_stb)&&(i_tx_busy); assign o_busy = (r_busy)||(o_stb); + // }}} /* output wire [27:0] o_dbg; - assign o_dbg = { o_stb, o_nl_hexbits, o_busy, r_busy, full_line, + assign o_dbg = { o_stb, o_nl_hexbits, o_busy, r_busy, full_line, i_bus_busy, linelen, i_tx_busy, i_stb, i_nl_hexbits }; */ endmodule diff --git a/rtl/wbubus/wbucompress.v b/rtl/wbubus/wbucompress.v index 0154e0e..ea7ff0b 100644 --- a/rtl/wbubus/wbucompress.v +++ b/rtl/wbubus/wbucompress.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbucompress.v -// +// {{{ // Project: FPGA library // // Purpose: When reading many words that are identical, it makes no sense @@ -25,11 +25,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -42,30 +42,33 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none // // All input words are valid codewords. If we can, we make them // better here. -module wbucompress(i_clk, i_reset, i_stb, i_codword, i_busy, o_stb, o_cword, - o_busy); - parameter DW=32, CW=36, TBITS=10; - input wire i_clk, i_reset, i_stb; - input wire [(CW-1):0] i_codword; - input wire i_busy; - output reg o_stb; - output wire [(CW-1):0] o_cword; - output wire o_busy; - - // - // +// }}} +module wbucompress #( + parameter DW=32, CW=36, TBITS=10 + ) ( + // {{{ + input wire i_clk, i_reset, i_stb, + input wire [(CW-1):0] i_codword, + input wire i_busy, + output reg o_stb, + output wire [(CW-1):0] o_cword, + output wire o_busy + // }}} + ); + + // Local declarations + // {{{ // First stage is to compress the address. // This stage requires one clock. // @@ -83,7 +86,7 @@ module wbucompress(i_clk, i_reset, i_stb, i_codword, i_busy, o_stb, o_cword, wire [31:0] w_addr; wire [3:0] addr_zcheck; reg tbl_busy; - + // }}} //////////////////////////////////////////////////////////////////////// // // Address compression stage diff --git a/rtl/wbubus/wbuconsole.v b/rtl/wbubus/wbuconsole.v index 077b041..0ddba8b 100644 --- a/rtl/wbubus/wbuconsole.v +++ b/rtl/wbubus/wbuconsole.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbuconsole.v -// +// {{{ // Project: FPGA library // // Purpose: This is the top level file for the entire JTAG-USB to Wishbone @@ -25,11 +25,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -42,130 +42,213 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbuconsole(i_clk, i_rx_stb, i_rx_data, - o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, - i_wb_stall, i_wb_ack, i_wb_err, i_wb_data, - i_interrupt, - o_tx_stb, o_tx_data, i_tx_busy, - i_console_stb, i_console_data, o_console_busy, - o_console_stb, o_console_data, - o_dbg); - parameter LGWATCHDOG=19, - LGINPUT_FIFO=6, - LGOUTPUT_FIFO=10; - parameter [0:0] CMD_PORT_OFF_UNTIL_ACCESSED = 1'b1; - input wire i_clk; - input wire i_rx_stb; - input wire [7:0] i_rx_data; - output wire o_wb_cyc, o_wb_stb, o_wb_we; - output wire [31:0] o_wb_addr, o_wb_data; - input wire i_wb_stall, i_wb_ack, i_wb_err; - input wire [31:0] i_wb_data; - input wire i_interrupt; - output wire o_tx_stb; - output wire [7:0] o_tx_data; - input wire i_tx_busy; - // - input wire i_console_stb; - input wire [6:0] i_console_data; - output wire o_console_busy; - // - output reg o_console_stb; - output reg [6:0] o_console_data; - // - output wire o_dbg; - - - +// }}} +module wbuconsole #( + // {{{ + parameter LGWATCHDOG=19, + LGINPUT_FIFO=6, + LGOUTPUT_FIFO=10, + parameter [0:0] CMD_PORT_OFF_UNTIL_ACCESSED = 1'b1 + // }}} + ) ( + // {{{ + input wire i_clk, + input wire i_reset, + // RX + // {{{ + input wire i_rx_stb, + input wire [7:0] i_rx_data, + // }}} + // Wishbone master + // {{{ + output wire o_wb_cyc, o_wb_stb, o_wb_we, + output wire [31:0] o_wb_addr, o_wb_data, + input wire i_wb_stall, i_wb_ack, + input wire [31:0] i_wb_data, + input wire i_wb_err, + // }}} + input wire i_interrupt, + // TX + // {{{ + output wire o_tx_stb, + output wire [7:0] o_tx_data, + input wire i_tx_busy, + // }}} + // CONSOLE + // {{{ + input wire i_console_stb, + input wire [6:0] i_console_data, + output wire o_console_busy, + // + output reg o_console_stb, + output reg [6:0] o_console_data, + // }}} + output wire o_dbg + // }}} + ); + // Local declarations + // {{{ + wire soft_reset; + reg r_wdt_reset; + wire cmd_port_active; + wire in_stb; + wire [35:0] in_word; + wire w_bus_busy, fifo_in_stb, exec_stb, w_bus_reset; + wire [35:0] fifo_in_word, exec_word; + reg ps_full; + reg [7:0] ps_data; + wire wbu_tx_stb; + wire [7:0] wbu_tx_data; + wire ofifo_err; + reg [(LGWATCHDOG-1):0] r_wdt_timer; + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Forward console inputs to the console + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + initial o_console_stb = 1'b0; always @(posedge i_clk) o_console_stb <= (i_rx_stb)&&(i_rx_data[7] == 1'b0); + always @(posedge i_clk) o_console_data <= i_rx_data[6:0]; + // }}} - - - reg r_wdt_reset, cmd_port_active; - + // cmd_port_active + // {{{ generate if (CMD_PORT_OFF_UNTIL_ACCESSED) begin - initial cmd_port_active = 1'b0; + reg r_cmd_port_active; + + initial r_cmd_port_active = 1'b0; always @(posedge i_clk) if (i_rx_stb && i_rx_data[7]) - cmd_port_active <= 1'b1; - + r_cmd_port_active <= 1'b1; + + assign cmd_port_active = r_cmd_port_active; + end else begin - always @(*) - cmd_port_active = 1'b1; - + assign cmd_port_active = 1'b1; + end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // // Decode ASCII input requests into WB bus cycle requests - wire in_stb; - wire [35:0] in_word; - wbuinput getinput(i_clk, (i_rx_stb)&&(i_rx_data[7]), { 1'b0, i_rx_data[6:0] }, in_stb, in_word); - - wire w_bus_busy, fifo_in_stb, exec_stb, w_bus_reset; - wire [35:0] fifo_in_word, exec_word; + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + wbuinput + getinput( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), + .i_stb(i_rx_stb && i_rx_data[7]), .i_byte({ 1'b0, i_rx_data[6:0] }), + .o_soft_reset(soft_reset), + .o_stb(in_stb), .o_codword(in_word) + // }}} + ); + // }}} + //////////////////////////////////////////////////////////////////////// + // + // The input FIFO + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + generate if (LGINPUT_FIFO < 2) + begin : NO_INPUT_FIFO - generate - if (LGINPUT_FIFO < 2) - begin assign fifo_in_stb = in_stb; assign fifo_in_word = in_word; - assign w_bus_reset = 1'b0; - end else begin + assign w_bus_reset = soft_reset; + + end else begin : INPUT_FIFO wire ififo_empty_n, ififo_err; + assign fifo_in_stb = (~w_bus_busy)&&(ififo_empty_n); assign w_bus_reset = r_wdt_reset; - wbufifo #(36,LGINPUT_FIFO) - padififo(i_clk, w_bus_reset, - in_stb, in_word, fifo_in_stb, fifo_in_word, - ififo_empty_n, ififo_err); - // Make verilator happy + wbufifo #( + // {{{ + .BW(36),.LGFLEN(LGINPUT_FIFO) + // }}} + ) padififo( + // {{{ + .i_clk(i_clk), .i_reset(w_bus_reset), + .i_wr(in_stb), .i_data(in_word), + .i_rd(fifo_in_stb), .o_data(fifo_in_word), + .o_empty_n(ififo_empty_n), .o_err(ififo_err) + // }}} + ); + // verilator lint_off UNUSED - wire unused_fifo; - assign unused_fifo = ififo_err; + wire gen_unused; + assign gen_unused = ififo_err; // verilator lint_on UNUSED end endgenerate + // }}} + //////////////////////////////////////////////////////////////////////// + // + // Run the bus + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // // Take requests in, Run the bus, send results out // This only works if no requests come in while requests // are pending. - wbuexec runwb(i_clk, r_wdt_reset, fifo_in_stb, fifo_in_word, w_bus_busy, - o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, - i_wb_stall, i_wb_ack, i_wb_err, i_wb_data, - exec_stb, exec_word); + wbuexec + runwb( + // {{{ + .i_clk(i_clk), .i_reset(r_wdt_reset), + .i_stb(fifo_in_stb), .i_codword(fifo_in_word), + .o_busy(w_bus_busy), + .o_wb_cyc(o_wb_cyc), .o_wb_stb(o_wb_stb), .o_wb_we(o_wb_we), + .o_wb_addr(o_wb_addr), .o_wb_data(o_wb_data), + .i_wb_stall(i_wb_stall), .i_wb_ack(i_wb_ack), + .i_wb_data(i_wb_data), .i_wb_err(i_wb_err), + .o_stb(exec_stb), .o_codword(exec_word) + // }}} + ); + // }}} - reg ps_full; - reg [7:0] ps_data; - wire wbu_tx_stb; - wire [7:0] wbu_tx_data; + wbuoutput #( + LGOUTPUT_FIFO + ) wroutput( + // {{{ + .i_clk(i_clk), .i_reset(i_reset), .i_soft_reset(w_bus_reset), + .i_stb(exec_stb), .i_codword(exec_word), + .i_wb_cyc(o_wb_cyc), .i_int(i_interrupt), .i_bus_busy(exec_stb), + .o_stb(wbu_tx_stb), .o_char(wbu_tx_data), .i_tx_busy(ps_full), + .o_fifo_err(ofifo_err) + // }}} + ); - wire ofifo_err; - // wire [30:0] out_dbg; - wbuoutput #(LGOUTPUT_FIFO) - wroutput(i_clk, w_bus_reset, - exec_stb, exec_word, - o_wb_cyc, i_interrupt, exec_stb, - wbu_tx_stb, wbu_tx_data, - ps_full && cmd_port_active, ofifo_err); - - // Let's now arbitrate between the two outputs + //////////////////////////////////////////////////////////////////////// + // + // Arbitrate between the two outputs, console and dbg bus + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // initial ps_full = 1'b0; always @(posedge i_clk) if (!ps_full) @@ -185,24 +268,39 @@ module wbuconsole(i_clk, i_rx_stb, i_rx_data, assign o_tx_stb = ps_full; assign o_tx_data = ps_data; assign o_console_busy = (wbu_tx_stb && cmd_port_active)||(ps_full); - + // }}} + //////////////////////////////////////////////////////////////////////// + // // Add in a watchdog timer to the bus - reg [(LGWATCHDOG-1):0] r_wdt_timer; - initial r_wdt_reset = 1'b0; + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + initial r_wdt_reset = 1'b1; initial r_wdt_timer = 0; always @(posedge i_clk) - if ((~o_wb_cyc)||(i_wb_ack)) - begin - r_wdt_timer <= 0; - r_wdt_reset <= 1'b0; - end else if (&r_wdt_timer) - begin - r_wdt_reset <= 1'b1; - r_wdt_timer <= 0; - end else begin - r_wdt_timer <= r_wdt_timer+{{(LGWATCHDOG-1){1'b0}},1'b1}; - r_wdt_reset <= 1'b0; - end + if (i_reset || soft_reset) + begin + r_wdt_timer <= 0; + r_wdt_reset <= 1'b1; + end else if ((~o_wb_cyc)||(i_wb_ack)) + begin + // We're inactive, or the bus has responded: reset the timer + // {{{ + r_wdt_timer <= 0; + r_wdt_reset <= 1'b0; + // }}} + end else if (&r_wdt_timer) + begin // TIMEOUT!!! + // {{{ + r_wdt_reset <= 1'b1; + r_wdt_timer <= 0; + // }}} + end else begin // Tick-tock ... + r_wdt_timer <= r_wdt_timer+{{(LGWATCHDOG-1){1'b0}},1'b1}; + r_wdt_reset <= 1'b0; + end + // }}} assign o_dbg = w_bus_reset; diff --git a/rtl/wbubus/wbudecompress.v b/rtl/wbubus/wbudecompress.v index cd6391e..fe49c4e 100644 --- a/rtl/wbubus/wbudecompress.v +++ b/rtl/wbubus/wbudecompress.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbudecompress.v -// +// {{{ // Project: FPGA library // // Purpose: Compression via this interface is simply a lookup table. @@ -16,11 +16,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -33,22 +33,26 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbudecompress(i_clk, i_stb, i_word, o_stb, o_word); - input wire i_clk, i_stb; - input wire [35:0] i_word; - output reg o_stb; - output reg [35:0] o_word; - +// }}} +module wbudecompress ( + // {{{ + input wire i_clk, i_reset, i_stb, + input wire [35:0] i_word, + output reg o_stb, + output reg [35:0] o_word + // }}} + ); + + // Local declarations + // {{{ reg [7:0] wr_addr; reg [31:0] compression_tbl [0:255]; reg [35:0] r_word; @@ -59,36 +63,64 @@ module wbudecompress(i_clk, i_stb, i_word, o_stb, o_word); reg [31:0] cword; reg [2:0] r_stb; wire cmd_write_not_compressed; - + // }}} + //////////////////////////////////////////////////////////////////////// + // // Clock zero // { o_stb, r_stb } = 0 + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // assign cmd_write_not_compressed = (i_word[35:33] == 3'h3); // Clock one: { o_stb, r_stb } = 4'h1 when done + + // wr_addr + // {{{ initial wr_addr = 8'h0; always @(posedge i_clk) - if ((i_stb)&&(cmd_write_not_compressed)) + if (i_reset) + wr_addr <= 8'h0; + else if ((i_stb)&&(cmd_write_not_compressed)) wr_addr <= wr_addr + 8'h1; + // }}} + // Write to compression_tbl + // {{{ always @(posedge i_clk) if (i_stb) compression_tbl[wr_addr] <= { i_word[32:31], i_word[29:0] }; + // }}} + // r_word + // {{{ always @(posedge i_clk) if (i_stb) r_word <= i_word; - - + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // // Clock two, calculate the table address ... 1 is the smallest address // { o_stb, r_stb } = 4'h2 when done + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // cmd_addr + // {{{ always @(posedge i_clk) if (i_stb) cmd_addr <= wr_addr - { i_word[32:31], i_word[29:24] }; + // }}} + // r_addr + // {{{ // Let's also calculate the address, in case this is a compressed // address word - always @(posedge i_clk) if (i_stb) case(i_word[32:30]) @@ -103,7 +135,10 @@ module wbudecompress(i_clk, i_stb, i_word, o_stb, o_word); endcase assign w_addr = { {(7){r_addr[24]}}, r_addr }; + // }}} + // rd_len + // {{{ always @(posedge i_clk) if (i_stb) begin @@ -112,27 +147,55 @@ module wbudecompress(i_clk, i_stb, i_word, o_stb, o_word); else rd_len <= 10'h09 + { 1'b0,i_word[33:31],i_word[29:24] }; end - + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // // Clock three, read the table value // { o_stb, r_stb } = 4'h4 when done // Maintaining ... // r_word (clock 1) // r_addr, rd_len (clock 2) + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // always @(posedge i_clk) cword <= compression_tbl[cmd_addr]; - + // r_stb + // {{{ // Pipeline the strobe signal to create an output strobe, 3 clocks later initial r_stb = 0; always @(posedge i_clk) + if (i_reset) + r_stb <= 0; + else r_stb <= { r_stb[1:0], i_stb }; - + // }}} + // }}} + //////////////////////////////////////////////////////////////////////// + // // Clock four, now that the table value is valid, let's set our output // word. // { o_stb, r_stb } = 4'h8 when done + // {{{ + //////////////////////////////////////////////////////////////////////// + // + // + + // o_stb + // {{{ initial o_stb = 0; always @(posedge i_clk) + if (i_reset) + o_stb <= 0; + else o_stb <= r_stb[2]; + // }}} + + // o_word + // {{{ // Maintaining ... // r_word (clock 1) // r_addr, rd_len (clock 2) @@ -162,6 +225,6 @@ module wbudecompress(i_clk, i_stb, i_word, o_stb, o_word); // 6'b1?????: o_word <= { 5'b11000, r_word[30], 20'h00, rd_len }; default: o_word <= r_word; endcase - + // }}} + // }}} endmodule - diff --git a/rtl/wbubus/wbudeword.v b/rtl/wbubus/wbudeword.v index f34b297..7f875f3 100644 --- a/rtl/wbubus/wbudeword.v +++ b/rtl/wbubus/wbudeword.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbudeword.v -// +// {{{ // Project: FPGA library // // Purpose: Once a word has come from the bus, undergone compression, had @@ -15,11 +15,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -32,26 +32,33 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbudeword(i_clk, i_stb, i_word, i_tx_busy, o_stb, o_nl_hexbits, o_busy); - input wire i_clk, i_stb; - input wire [35:0] i_word; - input wire i_tx_busy; - output reg o_stb; - output reg [6:0] o_nl_hexbits; - output reg o_busy; - +// }}} +module wbudeword ( + // {{{ + input wire i_clk, i_reset, i_stb, + input wire [35:0] i_word, + input wire i_tx_busy, + output reg o_stb, + output reg [6:0] o_nl_hexbits, + output reg o_busy + // }}} + ); + // Local declarations + // {{{ wire [2:0] w_len; + reg [2:0] r_len; + reg [29:0] r_word; + // }}} + assign w_len = (i_word[35:33]==3'b000)? 3'b001 : (i_word[35:32]==4'h2)? 3'b110 : (i_word[35:32]==4'h3)? (3'b010+{1'b0,i_word[31:30]}) @@ -59,8 +66,6 @@ module wbudeword(i_clk, i_stb, i_word, i_tx_busy, o_stb, o_nl_hexbits, o_busy); : (i_word[35:34]==2'b10)? 3'b001 : 3'b110; - reg [2:0] r_len; - reg [29:0] r_word; initial o_stb = 1'b0; initial o_busy = 1'b0; initial o_nl_hexbits = 7'h40; @@ -85,7 +90,9 @@ module wbudeword(i_clk, i_stb, i_word, i_tx_busy, o_stb, o_nl_hexbits, o_busy); initial o_stb = 1'b0; always @(posedge i_clk) - if (i_stb && !o_busy) + if (i_reset) + o_stb <= 1'b0; + else if (i_stb && !o_busy) o_stb <= 1'b1; else if (r_len == 0 && !i_tx_busy) o_stb <= 1'b0; diff --git a/rtl/wbubus/wbuexec.v b/rtl/wbubus/wbuexec.v index f2647c6..147d261 100644 --- a/rtl/wbubus/wbuexec.v +++ b/rtl/wbubus/wbuexec.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbuexec.v -// +// {{{ // Project: FPGA library // // Purpose: This is the part of the USB-JTAG to wishbone conversion that @@ -15,11 +15,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -32,24 +32,51 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbuexec(i_clk, i_reset, i_stb, i_codword, o_busy, - o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, - i_wb_stall, i_wb_ack, i_wb_err, i_wb_data, - o_stb, o_codword); - parameter AW = 32, DW = 32; - // - // - localparam [5:0] END_OF_WRITE = 6'h2e; +// }}} +module wbuexec #( + parameter AW = 32, DW = 32 + ) ( + // {{{ + input wire i_clk, i_reset, + // The command inputs + // {{{ + input wire i_stb, + input wire [35:0] i_codword, + output reg o_busy, + // }}} + // Wishbone outputs + // {{{ + output reg o_wb_cyc, + output reg o_wb_stb, + output reg o_wb_we, + output reg [AW-1:0] o_wb_addr, + output reg [DW-1:0] o_wb_data, + // }}} + // Wishbone inputs + // {{{ + input wire i_wb_stall, i_wb_ack, + input wire [DW-1:0] i_wb_data, + input wire i_wb_err, + // }}} + // And our codeword outputs + // {{{ + output reg o_stb, + output reg [35:0] o_codword + // }}} + // }}} + ); + + // Local declarations + // {{{ + // localparam [5:0] END_OF_WRITE = 6'h2e; localparam [1:0] WB_IDLE = 2'b00, WB_READ_REQUEST = 2'b01, WB_WRITE_REQUEST = 2'b10, @@ -57,32 +84,28 @@ module wbuexec(i_clk, i_reset, i_stb, i_codword, o_busy, WB_FLUSH_WRITE_REQUESTS = 2'b11; localparam [1:0] WRITE_PREFIX = 2'b01; - // - input wire i_clk, i_reset; - // The command inputs - input wire i_stb; - input wire [35:0] i_codword; - output reg o_busy; - // Wishbone outputs - output reg o_wb_cyc; - output reg o_wb_stb; - output reg o_wb_we; - output reg [31:0] o_wb_addr, o_wb_data; - // Wishbone inputs - input wire i_wb_stall, i_wb_ack, i_wb_err; - input wire [31:0] i_wb_data; - // And our codeword outputs - output reg o_stb; - output reg [35:0] o_codword; - wire [31:0] w_cod_data; - assign w_cod_data={ i_codword[32:31], i_codword[29:0] }; + +// wire w_accept, w_eow, w_newwr, w_new_err; +// // wire w_newad, w_newrd; +// assign w_accept = (i_stb)&&(!o_busy); +// // assign w_newad = (w_accept)&&(i_codword[35:34] == 2'b00); +// assign w_newwr = (w_accept)&&(i_codword[35:34] == WRITE_PREFIX); +// assign w_eow = (w_accept)&&(i_codword[35:30] == END_OF_WRITE); +// // assign w_newrd = (w_accept)&&(i_codword[35:34] == 2'b11); +// assign w_new_err = ((w_accept) +// &&(i_codword[35:33] != 3'h3) +// &&(i_codword[35:30] != END_OF_WRITE)); reg [1:0] wb_state; reg [9:0] r_acks_needed, r_len; reg r_inc, r_new_addr, last_read_request, last_ack, zero_acks; - reg single_read_request; + assign w_cod_data={ i_codword[32:31], i_codword[29:0] }; + // }}} + + // CYC, STB, wb_state and o_busy + // {{{ initial wb_state = WB_IDLE; initial o_wb_cyc = 1'b0; initial o_wb_stb = 1'b0; @@ -90,14 +113,18 @@ module wbuexec(i_clk, i_reset, i_stb, i_codword, o_busy, always @(posedge i_clk) if (i_reset) begin + // {{{ wb_state <= WB_IDLE; o_wb_cyc <= 1'b0; o_wb_stb <= 1'b0; o_busy <= 1'b0; + // }}} end else case(wb_state) WB_IDLE: begin + // {{{ o_wb_cyc <= 1'b0; o_wb_stb <= 1'b0; + o_busy <= 1'b0; // The new instruction. The following // don't matter if we're not running, @@ -110,7 +137,7 @@ module wbuexec(i_clk, i_reset, i_stb, i_codword, o_busy, // starting a write. Hence, let's always set it as // though we were about to start a write. // - o_wb_data <= w_cod_data; + // o_wb_data <= w_cod_data; // if (i_stb) begin @@ -146,7 +173,9 @@ module wbuexec(i_clk, i_reset, i_stb, i_codword, o_busy, ; endcase end end + // }}} WB_READ_REQUEST: begin + // {{{ o_wb_cyc <= 1'b1; // o_wb_stb <= 1'b1; @@ -156,7 +185,7 @@ module wbuexec(i_clk, i_reset, i_stb, i_codword, o_busy, if (!i_wb_stall) // Deal with the strobe line begin // Strobe was accepted, busy should be '1' here - if ((single_read_request)||(last_read_request)) + if (last_read_request) o_wb_stb <= 1'b0; end @@ -174,43 +203,39 @@ module wbuexec(i_clk, i_reset, i_stb, i_codword, o_busy, wb_state <= WB_IDLE; o_busy <= 1'b0; end end + // }}} WB_WRITE_REQUEST: begin + // {{{ o_wb_cyc <= 1'b1; - // o_wb_stb <= 1'b1; o_busy <= 1'b1; - // - - // Don't need to worry about accepting anything new - // here, since we'll always be busy while in this state. - // Hence, we cannot accept new write requests. - // if (!i_wb_stall) o_wb_stb <= 1'b0; - if (!o_busy) - o_wb_data <= w_cod_data; - - if (i_stb && !o_wb_stb) + if (i_stb && (!o_wb_stb || !i_wb_stall)) begin if (o_busy && (i_codword[35:34] != WRITE_PREFIX) && zero_acks) begin o_wb_cyc <= 1'b0; - // o_busy <= 1'b0; + o_busy <= 1'b0; wb_state <= WB_IDLE; - end - - if (!o_busy) // && w_newwr must be true + end else if (!o_busy) // && w_newwr must be true begin o_wb_stb <= 1'b1; o_busy <= 1'b1; - end else if ((i_codword[35:34] == WRITE_PREFIX) - ||zero_acks) - // (r_acks_needed == (i_wb_ack ? 1:0))) + end else if (i_codword[35:34] == WRITE_PREFIX) o_busy <= 1'b0; end + if (!i_stb && !o_wb_stb && o_busy && zero_acks) + begin + o_wb_cyc <= 1'b0; + o_wb_stb <= 1'b0; + wb_state <= WB_IDLE; + o_busy <= 1'b0; + end + if (i_wb_err) // Bus returns an error begin o_wb_cyc <= 1'b0; @@ -218,7 +243,9 @@ module wbuexec(i_clk, i_reset, i_stb, i_codword, o_busy, wb_state <= WB_FLUSH_WRITE_REQUESTS; o_busy <= 1'b1; end end + // }}} WB_FLUSH_WRITE_REQUESTS: begin + // {{{ // We come in here after an error within a write // We need to wait until the command cycle finishes // issuing all its write commands before we can go back @@ -237,18 +264,26 @@ module wbuexec(i_clk, i_reset, i_stb, i_codword, o_busy, wb_state <= WB_IDLE; o_busy <= 1'b0; end end + // }}} default: begin + // {{{ wb_state <= WB_IDLE; o_wb_cyc <= 1'b0; o_wb_stb <= 1'b0; o_busy <= 1'b0; end + // }}} endcase + // }}} + + always @(posedge i_clk) + if (!o_busy) + o_wb_data <= w_cod_data; always @(posedge i_clk) if (wb_state == WB_IDLE) // Will this be a write? - o_wb_we <= (!i_codword[35]); + o_wb_we <= !i_codword[35]; always @(posedge i_clk) if (i_stb && !o_busy && i_codword[35:32] == 4'h0) @@ -335,10 +370,6 @@ module wbuexec(i_clk, i_reset, i_stb, i_codword, o_busy, 2'b01: zero_acks <= (r_acks_needed == 10'h01); default: begin end endcase -`ifdef FORMAL - always @(*) - assert(zero_acks == (r_acks_needed == 0)); -`endif always @(posedge i_clk) if (!o_wb_cyc) // (!o_wb_cyc)&&(i_codword[35:34] == 2'b11)) @@ -346,21 +377,8 @@ module wbuexec(i_clk, i_reset, i_stb, i_codword, o_busy, else if ((o_wb_stb)&&(!i_wb_stall)&&(|r_len)) r_len <= r_len - 10'h01; -// single_read_request and last_read_request used to be ... -// always @(posedge i_clk) -// begin -// single_read_request <= (!o_wb_cyc)&&(i_codword[9:0] <= 10'h01); -// // When there is one read request left, it will be the last one -// // will be the last one -// last_read_request <= (o_wb_stb)&&(r_len[9:2] == 8'h00) -// &&((!r_len[1]) -// ||((!r_len[0])&&(!i_wb_stall))); -// end - always @(posedge i_clk) begin - if (!o_wb_cyc) - single_read_request <= (i_codword[9:0] <= 10'h01); // When there is one read request left, it will be the last one // will be the last one if (!o_wb_cyc) diff --git a/rtl/wbubus/wbufifo.v b/rtl/wbubus/wbufifo.v index 9845164..76ccc8e 100644 --- a/rtl/wbubus/wbufifo.v +++ b/rtl/wbubus/wbufifo.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbufifo.v -// +// {{{ // Project: FPGA library // // Purpose: This was once a FIFO for a UART ... but now it works as a @@ -12,11 +12,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -36,19 +36,24 @@ // //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbufifo(i_clk, i_reset, i_wr, i_data, i_rd, o_data, o_empty_n, o_err); - parameter BW=66, LGFLEN=10; - input wire i_clk, i_reset; - input wire i_wr; - input wire [(BW-1):0] i_data; - input wire i_rd; - output reg [(BW-1):0] o_data; - output reg o_empty_n; - output wire o_err; - +// }}} +module wbufifo #( + parameter BW=66, LGFLEN=10 + ) ( + // {{{ + input wire i_clk, i_reset, + input wire i_wr, + input wire [(BW-1):0] i_data, + input wire i_rd, + output reg [(BW-1):0] o_data, + output reg o_empty_n, + output wire o_err + // }}} + ); + + // Local declarations + // {{{ localparam FLEN=(1< for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbuidleint(i_clk, i_stb, i_codword, i_cyc, i_busy, i_int, - o_stb, o_codword, o_busy, - i_tx_busy); +// }}} +module wbuidleint( + // {{{ + input wire i_clk, i_reset, + // From the FIFO following the bus executor + input wire i_stb, + input wire [35:0] i_codword, + // From the rest of the board + input wire i_cyc, i_busy, i_int, + // To the next stage + output reg o_stb, + output reg [35:0] o_codword, + output reg o_busy, + // Is the next stage busy? + input wire i_tx_busy + // }}} + ); + + // Local declarations + // {{{ localparam CW_INTERRUPT = { 6'h4, 30'h0000 }; // interrupt codeword localparam CW_BUSBUSY = { 6'h1, 30'h0000 }; // bus busy, ow idle localparam CW_IDLE = { 6'h0, 30'h0000 }; // idle codeword - - input wire i_clk; - // From the FIFO following the bus executor - input wire i_stb; - input wire [35:0] i_codword; - // From the rest of the board - input wire i_cyc, i_busy, i_int; - // To the next stage - output reg o_stb; - output reg [35:0] o_codword; - output reg o_busy; - // Is the next stage busy? - input wire i_tx_busy; +`ifdef VERILATOR + localparam IDLEBITS = 22; +`else + localparam IDLEBITS = 31; +`endif reg int_request, int_sent; wire idle_expired; reg idle_state; reg [IDLEBITS-1:0] idle_counter; + // }}} + // int_request + // {{{ initial int_request = 1'b0; always @(posedge i_clk) - if((o_stb)&&(!i_tx_busy)&&(o_codword[35:30]==6'h4)) - int_request <= i_int; - else - int_request <= (int_request)||(i_int); + if (i_reset) + int_request <= 0; + else if (i_int) + int_request <= 1; + else if((o_stb)&&(!i_tx_busy)&&(o_codword[35:30]==CW_INTERRUPT[35:30])) + int_request <= 0; + // }}} -`ifdef VERILATOR - localparam IDLEBITS = 22; -`else - localparam IDLEBITS = 31; -`endif + // idle_counter + // {{{ // Now, for the idle counter initial idle_counter = 0; always @(posedge i_clk) - if ((i_stb)||(o_stb)||(i_busy)) + if (i_reset || (i_stb)||(o_stb)||(i_busy)) idle_counter <= 0; else if (!idle_counter[IDLEBITS-1]) idle_counter <= idle_counter + 1; + // }}} + // idle_state + // {{{ initial idle_state = 1'b0; always @(posedge i_clk) - if ((o_stb)&&(!i_tx_busy)&&(o_codword[35:30]==CW_IDLE[35:30])) + if (i_reset) + // We are now idle, and can rest + idle_state <= 1'b1; + else if ((o_stb)&&(!i_tx_busy)&&(o_codword[35:30]==CW_IDLE[35:30])) // We are now idle, and can rest idle_state <= 1'b1; else if (!idle_counter[IDLEBITS-1]) // We became active, and can rest no longer idle_state <= 1'b0; + // }}} assign idle_expired = (!idle_state)&&(idle_counter[IDLEBITS-1]); + // o_stb, o_codword + // {{{ initial o_stb = 1'b0; always @(posedge i_clk) - if(!o_stb || !i_tx_busy) begin - if (i_stb) - begin // On a valid output, just send it out - // We'll open this strobe, even if the transmitter - // is busy, just 'cause we might otherwise lose it - o_stb <= 1'b1; - o_codword <= i_codword; - end else begin - // Our indicators take a clock to reset, hence - // we'll idle for one clock before sending either an - // interrupt or an idle indicator. The bus busy - // indicator is really only ever used to let us know - // that something's broken. - o_stb <= (!o_stb)&&(int_request || idle_expired); - - if (int_request && !int_sent) - o_codword[35:30] <= CW_INTERRUPT[35:30]; - else begin - o_codword[35:30] <= CW_IDLE[35:30]; - if (i_cyc) - o_codword[35:30] <= CW_BUSBUSY[35:30]; + if(!o_stb || !i_tx_busy) + begin + if (i_stb) + begin + // On a valid output, just send it out + // We'll open this strobe, even if the + // transmitter is busy, just 'cause we might + // otherwise lose it + o_stb <= 1'b1; + o_codword <= i_codword; + end else begin + // Our indicators take a clock to reset, hence + // we'll idle for one clock before sending + // either an interrupt or an idle indicator. + // The bus busy indicator is really only ever + // used to let us know that something's broken. + o_stb <= (!o_stb)&&(int_request || idle_expired); + + if (int_request && !int_sent) + o_codword[35:30] <= CW_INTERRUPT[35:30]; + else begin + o_codword[35:30] <= CW_IDLE[35:30]; + if (i_cyc) + o_codword[35:30] <= CW_BUSBUSY[35:30]; + end end end + + if (i_reset) + o_stb <= 1'b0; end + // }}} always @(*) o_busy = o_stb; + // int_sent + // {{{ initial int_sent = 1'b0; always @(posedge i_clk) - if ((int_request)&&((!o_stb)&&(!o_busy)&&(!i_stb))) + if (i_reset) + int_sent <= 1'b0; + else if ((int_request)&&((!o_stb)&&(!o_busy)&&(!i_stb))) int_sent <= 1'b1; - else if (~i_int) + else if (!i_int) int_sent <= 1'b0; - + // }}} endmodule diff --git a/rtl/wbubus/wbuinput.v b/rtl/wbubus/wbuinput.v index 2f4418b..d36c0b0 100644 --- a/rtl/wbubus/wbuinput.v +++ b/rtl/wbubus/wbuinput.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbuinput.v -// +// {{{ // Project: FPGA library // // Purpose: Coordinates the receiption of bytes, which are then translated @@ -15,11 +15,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -32,30 +32,36 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbuinput(i_clk, i_stb, i_byte, o_stb, o_codword); - input wire i_clk, i_stb; - input wire [7:0] i_byte; - output wire o_stb; - output wire [35:0] o_codword; +// }}} +module wbuinput ( + // {{{ + input wire i_clk, i_reset, + i_stb, + input wire [7:0] i_byte, + output wire o_soft_reset, + output wire o_stb, + output wire [35:0] o_codword + // }}} + ); wire hx_stb, hx_valid; wire [5:0] hx_hexbits; - wbutohex tobits(i_clk, i_stb, i_byte, - hx_stb, hx_valid, hx_hexbits); wire cw_stb; wire [35:0] cw_word; - wbureadcw formcw(i_clk, hx_stb, hx_valid, hx_hexbits, + + wbutohex tobits(i_clk, i_reset, i_stb, i_byte, o_soft_reset, + hx_stb, hx_valid, hx_hexbits); + + wbureadcw formcw(i_clk, i_reset, hx_stb, hx_valid, hx_hexbits, cw_stb, cw_word); // `define DEBUGGING @@ -63,7 +69,8 @@ module wbuinput(i_clk, i_stb, i_byte, o_stb, o_codword); assign o_stb = cw_stb; assign o_codword = cw_word; `else - wbudecompress unpack(i_clk,cw_stb,cw_word, o_stb, o_codword); + wbudecompress unpack(i_clk, i_reset, + cw_stb, cw_word, o_stb, o_codword); `endif endmodule diff --git a/rtl/wbubus/wbuoutput.v b/rtl/wbubus/wbuoutput.v index 55a7143..d4a726d 100644 --- a/rtl/wbubus/wbuoutput.v +++ b/rtl/wbubus/wbuoutput.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbuoutput.v -// +// {{{ // Project: FPGA library // // Purpose: Converts 36-bit codewords into bytes to be placed on the serial @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,32 +31,35 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbuoutput(i_clk, i_rst, i_stb, i_codword, - i_wb_cyc, i_int, i_bus_busy, - o_stb, o_char, i_tx_busy, o_fifo_err); - parameter LGOUTPUT_FIFO = 10; - input wire i_clk, i_rst; - input wire i_stb; - input wire [35:0] i_codword; - // Not Idle indicators - input wire i_wb_cyc, i_int, i_bus_busy; - // Outputs to our UART transmitter - output wire o_stb; - output wire [7:0] o_char; - // Miscellaneous I/O: UART transmitter busy, and fifo error - input wire i_tx_busy; - output wire o_fifo_err; +// }}} +module wbuoutput #( + parameter LGOUTPUT_FIFO = 10 + ) ( + // {{{ + input wire i_clk, i_reset, i_soft_reset, + input wire i_stb, + input wire [35:0] i_codword, + // Not Idle indicators + input wire i_wb_cyc, i_int, i_bus_busy, + // Outputs to our UART transmitter + output wire o_stb, + output wire [7:0] o_char, + // Miscellaneous I/O: UART transmitter busy, and fifo error + input wire i_tx_busy, + output wire o_fifo_err + // }}} + ); + // Local declarations + // {{{ wire fifo_rd, dw_busy, fifo_empty_n, fifo_err; wire [35:0] fifo_codword; @@ -64,7 +67,10 @@ module wbuoutput(i_clk, i_rst, i_stb, i_codword, cp_busy, byte_busy; wire [35:0] cw_codword, cp_word; wire [6:0] dw_bits, ln_bits; + // }}} + // Output FIFO + // {{{ generate if (LGOUTPUT_FIFO < 2) begin : NO_OUTBOUND_FIFO @@ -77,15 +83,16 @@ module wbuoutput(i_clk, i_rst, i_stb, i_codword, assign fifo_rd = (fifo_empty_n)&&(!cw_busy); wbufifo #(36,LGOUTPUT_FIFO) - busoutfifo(i_clk, i_rst, i_stb, i_codword, + busoutfifo(i_clk, i_soft_reset, i_stb, i_codword, fifo_rd, fifo_codword, fifo_empty_n, fifo_err); end endgenerate assign o_fifo_err = fifo_err; + // }}} - wbuidleint buildcw(i_clk, fifo_rd, fifo_codword, + wbuidleint buildcw(i_clk, i_soft_reset, fifo_rd, fifo_codword, i_wb_cyc, i_bus_busy, i_int, cw_stb, cw_codword, cw_busy, cp_stb && cp_busy); // assign o_dbg = dw_busy; // Always asserted ... ??? @@ -105,18 +112,18 @@ module wbuoutput(i_clk, i_rst, i_stb, i_codword, assign cp_word = cw_codword; assign cp_busy = dw_busy; `else - wbucompress packit(i_clk, 1'b0, cw_stb, cw_codword, dw_busy, + wbucompress packit(i_clk, i_soft_reset,cw_stb, cw_codword, dw_busy, cp_stb, cp_word, cp_busy); `endif - wbudeword deword(i_clk, cp_stb, cp_word, ln_busy, + wbudeword deword(i_clk, i_reset, cp_stb, cp_word, ln_busy, dw_stb, dw_bits, dw_busy); - wbucompactlines linepacker(i_clk, dw_stb, dw_bits, + wbucompactlines linepacker(i_clk, i_reset, dw_stb, dw_bits, ln_stb, ln_bits, (i_wb_cyc||i_bus_busy||fifo_empty_n||cw_busy), byte_busy, ln_busy); - wbusixchar mkbytes(i_clk, ln_stb, ln_bits, o_stb, o_char, byte_busy, i_tx_busy); + wbusixchar mkbytes(i_clk, i_reset, ln_stb, ln_bits, o_stb, o_char, byte_busy, i_tx_busy); endmodule diff --git a/rtl/wbubus/wbureadcw.v b/rtl/wbubus/wbureadcw.v index 5fd2d8c..dd0d20a 100644 --- a/rtl/wbubus/wbureadcw.v +++ b/rtl/wbubus/wbureadcw.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbureadcw.v -// +// {{{ // Project: FPGA library // // Purpose: Read bytes from a serial port (i.e. the jtagser) and translate @@ -16,11 +16,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -33,26 +33,28 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none // // Goal: single clock pipeline, 50 slices or less -// -module wbureadcw(i_clk, i_stb, i_valid, i_hexbits, - o_stb, o_codword); - input wire i_clk, i_stb, i_valid; - input wire [5:0] i_hexbits; - output reg o_stb; - output reg [35:0] o_codword; - +// }}} +module wbureadcw( + // {{{ + input wire i_clk, i_reset, i_stb, i_valid, + input wire [5:0] i_hexbits, + output reg o_stb, + output reg [35:0] o_codword + // }}} + ); + // Local declarations + // {{{ // Timing: // Clock 0: i_stb is high, i_valid is low // Clock 1: shiftreg[5:0] is valid, cw_len is valid @@ -64,27 +66,35 @@ module wbureadcw(i_clk, i_stb, i_valid, i_hexbits, // cw_len = 0, // r_len = 0 (unless i_stb) // Ready for next word - reg [2:0] r_len, cw_len; reg [1:0] lastcw; wire w_stb; + reg [35:0] shiftreg; + + // }}} + assign w_stb = ((r_len == cw_len)&&(cw_len != 0)) ||((i_stb)&&(!i_valid)&&(lastcw == 2'b01)); + // r_len + // {{{ // r_len is the length of the codeword as it exists // in our register initial r_len = 3'h0; always @(posedge i_clk) - if ((i_stb)&&(!i_valid)) // Newline reset + if (i_reset) + r_len <= 0; + else if ((i_stb)&&(!i_valid)) // Newline reset r_len <= 0; else if (w_stb) // reset/restart w/o newline r_len <= (i_stb)? 3'h1:3'h0; else if (i_stb) //in middle of word r_len <= r_len + 3'h1; + // }}} - reg [35:0] shiftreg; - + // shiftreg + // {{{ initial shiftreg = 0; always @(posedge i_clk) if (w_stb) @@ -98,21 +108,34 @@ module wbureadcw(i_clk, i_stb, i_valid, i_hexbits, 3'b101: shiftreg[ 5: 0] <= i_hexbits; default: begin end endcase + // }}} + // lastcw + // {{{ initial lastcw = 2'b00; always @(posedge i_clk) if (o_stb) lastcw <= o_codword[35:34]; + // }}} + + // o_codword + // {{{ always @(posedge i_clk) if ((i_stb)&&(!i_valid)&&(lastcw == 2'b01)) + // End of write signal o_codword[35:30] <= 6'h2e; else o_codword <= shiftreg; + // }}} + // cw_len + // {{{ // How long do we expect this codeword to be? initial cw_len = 3'b000; always @(posedge i_clk) - if ((i_stb)&&(!i_valid)) + if (i_reset) + cw_len <= 0; + else if ((i_stb)&&(!i_valid)) cw_len <= 0; else if ((i_stb)&&((cw_len == 0)||(w_stb))) begin @@ -128,10 +151,13 @@ module wbureadcw(i_clk, i_stb, i_valid, i_hexbits, cw_len <= 3'h6; end else if (w_stb) cw_len <= 0; + // }}} + // o_stb + // {{{ initial o_stb = 1'b0; always @(posedge i_clk) - o_stb <= w_stb; - + o_stb <= w_stb && !i_reset; + // }}} endmodule diff --git a/rtl/wbubus/wbusixchar.v b/rtl/wbubus/wbusixchar.v index df36513..9215c4f 100644 --- a/rtl/wbubus/wbusixchar.v +++ b/rtl/wbubus/wbusixchar.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbusixchar.v -// +// {{{ // Project: FPGA library // // Purpose: Supports a conversion from a six digit bus to a printable @@ -21,11 +21,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -38,33 +38,45 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbusixchar(i_clk, i_stb, i_bits, o_stb, o_char, o_busy, i_busy); - input wire i_clk; - input wire i_stb; - input wire [6:0] i_bits; - output reg o_stb; - output reg [7:0] o_char; - output wire o_busy; - input wire i_busy; +// }}} +module wbusixchar( + // {{{ + input wire i_clk, i_reset, + input wire i_stb, + input wire [6:0] i_bits, + output reg o_stb, + output reg [7:0] o_char, + output wire o_busy, + input wire i_busy + // }}} + ); + // Local declarations + // {{{ + reg [6:0] remap [0:127]; + reg [6:0] newv; + // }}} + + // o_stb + // {{{ initial o_stb = 1'b0; always @(posedge i_clk) - if (!o_stb || !i_busy) + if (i_reset) + o_stb <= 1'b0; + else if (!o_stb || !i_busy) o_stb <= i_stb; + // }}} - reg [6:0] remap [0:127]; - reg [6:0] newv; - + // newv + // {{{ integer k; always @(*) begin for(k=0; k<128; k=k+1) @@ -93,13 +105,16 @@ module wbusixchar(i_clk, i_stb, i_bits, o_stb, o_char, o_busy, i_busy); remap[k] = newv; end end + // }}} + // o_char + // {{{ initial o_char = 8'h00; always @(posedge i_clk) if (!o_busy) o_char <= { 1'b0, remap[i_bits] }; + // }}} - assign o_busy = o_stb; - + assign o_busy = o_stb && i_busy; endmodule diff --git a/rtl/wbubus/wbutohex.v b/rtl/wbubus/wbutohex.v index 6e4c54d..1480996 100644 --- a/rtl/wbubus/wbutohex.v +++ b/rtl/wbubus/wbutohex.v @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbutohex.v -// +// {{{ // Project: FPGA library // // Purpose: Supports a printable character conversion from a printable @@ -21,11 +21,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -38,31 +38,41 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// `default_nettype none -// -module wbutohex(i_clk, i_stb, i_byte, o_stb, o_valid, o_hexbits); - input wire i_clk, i_stb; - input wire [7:0] i_byte; - output reg o_stb, o_valid; - output reg [5:0] o_hexbits; - - initial o_stb = 1'b0; - always @(posedge i_clk) - o_stb <= i_stb; +// }}} +module wbutohex ( + // {{{ + input wire i_clk, i_reset, i_stb, + input wire [7:0] i_byte, + output reg o_soft_reset, + output reg o_stb, o_valid, + output reg [5:0] o_hexbits + // }}} + ); + // Local declarations + // {{{ reg [6:0] remap [0:127]; - integer k; reg [6:0] newv; + // }}} + + // o_stb + // {{{ + initial o_stb = 1'b0; + always @(posedge i_clk) + o_stb <= i_stb && !i_reset; + // }}} + // newv, remap + // {{{ always @(*) // initial begin @@ -91,14 +101,26 @@ module wbutohex(i_clk, i_stb, i_byte, o_stb, o_valid, o_hexbits); remap[k] = newv; end end - + // }}} + + // o_valid + // {{{ always @(posedge i_clk) begin { o_valid, o_hexbits } <= remap[i_byte[6:0]]; if (i_byte[7]) o_valid <= 0; end + // }}} - + // o_soft_reset + // {{{ + initial o_soft_reset = 1'b1; + always @(posedge i_clk) + if (i_reset) + o_soft_reset <= 1; + else + o_soft_reset <= i_stb && (i_byte[6:0] == 7'h3); + // }}} endmodule diff --git a/rtl/wbxbar.v b/rtl/wbxbar.v index e2b71bc..e10964c 100644 --- a/rtl/wbxbar.v +++ b/rtl/wbxbar.v @@ -52,7 +52,7 @@ // //////////////////////////////////////////////////////////////////////////////// // }}} -// Copyright (C) 2019-2020, Gisselquist Technology, LLC +// Copyright (C) 2019-2022, Gisselquist Technology, LLC // {{{ // This file is part of the WB2AXIP project. // @@ -116,10 +116,6 @@ module wbxbar #( parameter [0:0] OPT_STARVATION_TIMEOUT = 1'b0 && (OPT_TIMEOUT > 0), // - // TIMEOUT_WIDTH is the number of bits in counter used to check - // on a timeout. - localparam TIMEOUT_WIDTH = $clog2(OPT_TIMEOUT), - // // OPT_DBLBUFFER is used to register all of the outputs, and // thus avoid adding additional combinational latency through // the core that might require a slower clock speed. @@ -130,18 +126,7 @@ module wbxbar #( // that could be used to reduce the logic count of the device. // Hence, OPT_LOWPOWER will use more logic, but it won't drive // bus wires unless there's a value to drive onto them. - parameter [0:0] OPT_LOWPOWER = 1'b1, - // - // LGNM is the log (base two) of the number of bus masters - // connecting to this crossbar - localparam LGNM = (NM>1) ? $clog2(NM) : 1, - // - // LGNM is the log (base two) of the number of slaves plus one - // come out of the system. The extra "plus one" is used for a - // pseudo slave representing the case where the given address - // doesn't connect to any of the slaves. This address will - // generate a bus error. - localparam LGNS = $clog2(NS+1) + parameter [0:0] OPT_LOWPOWER = 1'b1 // }}} ) ( // {{{ @@ -178,6 +163,21 @@ module wbxbar #( // // Register declarations // {{{ + // + // TIMEOUT_WIDTH is the number of bits in counter used to check + // on a timeout. + localparam TIMEOUT_WIDTH = $clog2(OPT_TIMEOUT); + // + // LGNM is the log (base two) of the number of bus masters + // connecting to this crossbar + localparam LGNM = (NM>1) ? $clog2(NM) : 1; + // + // LGNS is the log (base two) of the number of slaves plus one + // come out of the system. The extra "plus one" is used for a + // pseudo slave representing the case where the given address + // doesn't connect to any of the slaves. This address will + // generate a bus error. + localparam LGNS = $clog2(NS+1); // At one time I used o_macc and o_sacc to put into the outgoing // trace file, just enough logic to tell me if a transaction was // taking place on the given clock. @@ -200,26 +200,27 @@ module wbxbar #( // Verilator lint_off UNUSED wire [LGMAXBURST-1:0] w_mpending [0:NM-1]; // Verilator lint_on UNUSED - reg [NM-1:0] mfull, mnearfull, mempty, timed_out; + reg [NM-1:0] mfull, mnearfull, mempty; + wire [NM-1:0] timed_out; localparam NMFULL = (NM > 1) ? (1< 0) begin deadlock_timer <= deadlock_timer - 1; - timed_out[N] <= (deadlock_timer <= 1); + r_timed_out <= (deadlock_timer <= 1); end + + assign timed_out[N] = r_timed_out; end // }}} end else begin : NO_TIMEOUT // {{{ - always @(*) - timed_out = 0; + assign timed_out = 0; // }}} end endgenerate // }}} @@ -1199,7 +1207,7 @@ module wbxbar #( // Double check the timeout flags for consistency generate for(N=0; N for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## ## ################################################################################ ## -## +## }}} .PHONY: all # Make certain the "all" target is the first and therefore the default target all: CXX := g++ OBJDIR := obj-pc -RTLD := ../../rtl +RTLD := ../rtl VOBJDR := $(RTLD)/obj_dir VERILATOR_ROOT ?= $(shell bash -c 'verilator -V|grep VERILATOR_ROOT | head -1 | sed -e " s/^.*=\s*//"') VROOT := $(VERILATOR_ROOT) VDEFS := $(shell ./vversion.sh) VINCD := $(VROOT)/include VINC := -I$(VINCD) -I$(VINCD)/vltstd -I$(VOBJDR) -INCS := -I../../sw/host -I$(RTLD) $(VINC) -CFLAGS := -Og -g -Wall $(INCS) +INCS := -I../sw/host -I$(RTLD) $(VINC) +CFLAGS := -Og -g -Wall -faligned-new -DVL_THREADED $(INCS) # CFLAGS := -Og -g -Wall $(INCS) -faligned-new # # A list of our sources and headers @@ -60,7 +61,7 @@ CFLAGS := -Og -g -Wall $(INCS) SIMSOURCES:= flashsim.cpp sdspisim.cpp dbluartsim.cpp zipelf.cpp byteswap.cpp SIMOBJECTS:= $(addprefix $(OBJDIR)/,$(subst .cpp,.o,$(SIMSOURCES))) SIMHEADERS:= $(foreach header,$(subst .cpp,.h,$(SIMSOURCES)),$(wildcard $(header))) -VOBJS := $(OBJDIR)/verilated.o $(OBJDIR)/verilated_vcd_c.o +VOBJS := $(OBJDIR)/verilated.o $(OBJDIR)/verilated_vcd_c.o $(OBJDIR)/verilated_threads.o SIMOBJ := $(subst .cpp,.o,$(SIMSOURCES)) SIMOBJS:= $(addprefix $(OBJDIR)/,$(SIMOBJ)) $(VOBJS) @@ -82,7 +83,7 @@ $(OBJDIR)/%.o: $(VINCD)/%.cpp MAINOBJS := $(OBJDIR)/main_tb.o $(OBJDIR)/automaster_tb.o main_tb: $(MAINOBJS) $(SIMOBJECTS) $(VOBJS) $(VOBJDR)/Vmain__ALL.a - $(CXX) $(INCS) $(VDEFS) $^ $(VOBJDR)/Vmain__ALL.a -lelf -o $@ + $(CXX) $(INCS) $(VDEFS) $^ $(VOBJDR)/Vmain__ALL.a -lelf -lpthread -o $@ # # Build a 64M SD-card image file that we can use for testing @@ -94,7 +95,7 @@ sdcard.img: # .PHONY: test test: - ./main_tb -d ../../sw/board/hello + ./main_tb -d ../sw/board/hello # # The "clean" target, removing any and all remaining build products diff --git a/sim/verilated/automaster_tb.cpp b/sim/automaster_tb.cpp similarity index 78% rename from sim/verilated/automaster_tb.cpp rename to sim/automaster_tb.cpp index c1a1ed1..6b1d2e6 100644 --- a/sim/verilated/automaster_tb.cpp +++ b/sim/automaster_tb.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: automaster_tb.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This file calls and accesses the main.v function via the @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include @@ -46,16 +46,19 @@ #include "verilated.h" #include "design.h" -#include "cpudefs.h" #include "testb.h" // #include "twoc.h" #include "port.h" +// #define R_ZIPCTRL 0x000 +// #define R_ZIPPC 0x0bc + #include "main_tb.cpp" void usage(void) { + // {{{ fprintf(stderr, "USAGE: main_tb [zipcpu-elf-file]\n"); fprintf(stderr, #ifdef SDSPI_ACCESS @@ -70,8 +73,11 @@ void usage(void) { "\t\tbe a vcd file\n" ); } +// }}} int main(int argc, char **argv) { + // Variable declaration and initialization + // {{{ #ifdef OLED_ACCESS Gtk::Main main_instance(argc, argv); #endif @@ -83,11 +89,14 @@ int main(int argc, char **argv) { #endif *profile_file = NULL, *trace_file = NULL; // "trace.vcd"; - bool debug_flag = false, willexit = false; + bool debug_flag = false, willexit = false, verbose_flag = false; FILE *profile_fp; MAINTB *tb = new MAINTB; + // }}} + // Process arguments + // {{{ for(int argn=1; argn < argc; argn++) { if (argv[argn][0] == '-') for(int j=1; (j<512)&&(argv[argn][j]);j++) { @@ -102,6 +111,7 @@ int main(int argc, char **argv) { case 'f': profile_file = "pfile.bin"; break; case 't': trace_file = argv[++argn]; j=1000; break; case 'h': usage(); exit(0); break; + case 'v': verbose_flag = true; break; default: fprintf(stderr, "ERR: Unexpected flag, -%c\n\n", argv[argn][j]); @@ -120,10 +130,13 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } } + // }}} + // Setup + // {{{ if (elfload) willexit = true; - if (debug_flag) { + if (debug_flag && verbose_flag) { printf("Opening design with\n"); printf("\tDebug Access port = %d\n", FPGAPORT); // fpga_port); printf("\tSerial Console = %d\n", FPGAPORT+1); @@ -146,13 +159,15 @@ int main(int argc, char **argv) { } } else profile_fp = NULL; - + // }}} tb->reset(); #ifdef SDSPI_ACCESS tb->setsdcard(sdimage_file); #endif + // Load the ZipCPU + // {{{ if (elfload) { #ifndef INCLUDE_ZIPCPU fprintf(stderr, "ERR: Design has no ZipCPU\n"); @@ -166,61 +181,48 @@ int main(int argc, char **argv) { elfread(elfload, entry, secpp); free(secpp); - printf("Attempting to start from 0x%08x\n", entry); - tb->m_core->cpu_ipc = entry; - - tb->m_core->cpu_cmd_halt = 0; - tb->m_core->cpu_reset = 0; - tb->tick(); - - tb->m_core->cpu_ipc = entry; - tb->m_core->cpu_new_pc = 1; - tb->m_core->cpu_pf_pc = entry; - tb->m_core->cpu_cmd_halt = 1; - tb->m_core->cpu_reset = 0; - // - // tb->m_core->alu_wR = 1; - tb->m_core->CPUVAR(_alu_reg) = 15; - tb->m_core->CPUVAR(_dbgv) = 1; - tb->m_core->CPUVAR(_dbg_val) = entry; - tb->m_core->CPUVAR(_dbg_clear_pipe) = 1; - // - tb->tick(); - tb->m_core->cpu_cmd_halt = 0; - tb->m_core->VVAR(_swic__DOT__cmd_reset) = 0; + if (verbose_flag) + printf("Attempting to start ZipCPU from 0x%08x\n", entry); + + tb->sim_write(R_ZIPCTRL, 0x11); tb->tick(); + tb->sim_write(R_ZIPCTRL, 0x11); tb->tick(); + tb->sim_write(R_ZIPPC, entry); tb->tick(); + tb->sim_write(R_ZIPCTRL, 0); tb->tick(); + tb->sim_write(R_ZIPCTRL, 0); tb->tick(); } + // }}} + // Main while(1) loop + // {{{ #ifdef OLED_ACCESS Gtk::Main::run(tb->m_oled); #else - if (profile_fp) { - unsigned long last_instruction_tick = 0, now = 0; + if (profile_fp) { // Profile the ZipCPU + // {{{ while((!willexit)||(!tb->done())) { - unsigned long iticks; unsigned buf[2]; - now++; tb->tick(); - if (((tb->m_core->cpu_alu_pc_valid) - ||(tb->m_core->cpu_mem_pc_valid)) - &&(!tb->m_core->cpu_alu_phase) - &&(!tb->m_core->cpu_new_pc)) { - iticks = now - last_instruction_tick; - buf[0] = tb->m_core->cpu_alu_pc; - buf[1] = (unsigned)iticks; + if (tb->m_core->o_prof_stb) { + buf[0] = tb->m_core->o_prof_addr; + buf[1] = tb->m_core->o_prof_ticks; fwrite(buf, sizeof(unsigned), 2, profile_fp); - - last_instruction_tick = now; } } - } else if (willexit) { + // }}} + } else if (willexit) { // Run until an exit call + // {{{ while(!tb->done()) tb->tick(); - } else + // }}} + } else // Run forever + // {{{ while(true) tb->tick(); + // }}} #endif + // }}} tb->close(); delete tb; diff --git a/sim/verilated/busmaster_tb.cpp b/sim/busmaster_tb.cpp similarity index 98% rename from sim/verilated/busmaster_tb.cpp rename to sim/busmaster_tb.cpp index 939abee..a725c1c 100644 --- a/sim/verilated/busmaster_tb.cpp +++ b/sim/busmaster_tb.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: busmaster_tb.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This is piped version of the testbench for the busmaster @@ -15,11 +15,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2017, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -32,14 +32,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include diff --git a/sim/verilated/byteswap.cpp b/sim/byteswap.cpp similarity index 93% rename from sim/verilated/byteswap.cpp rename to sim/byteswap.cpp index c723d5a..c62771f 100644 --- a/sim/verilated/byteswap.cpp +++ b/sim/byteswap.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: byteswap.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: To convert between little endian and big endian byte orders, @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include "byteswap.h" diff --git a/sim/verilated/byteswap.h b/sim/byteswap.h similarity index 93% rename from sim/verilated/byteswap.h rename to sim/byteswap.h index 0e7e131..7b0f011 100644 --- a/sim/verilated/byteswap.h +++ b/sim/byteswap.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: byteswap.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: To convert between little endian and big endian byte orders, @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef BYTESWAP_H #define BYTESWAP_H diff --git a/sim/verilated/dblpipecmdr.h b/sim/dblpipecmdr.h similarity index 98% rename from sim/verilated/dblpipecmdr.h rename to sim/dblpipecmdr.h index 51c7553..05ae7f7 100644 --- a/sim/verilated/dblpipecmdr.h +++ b/sim/dblpipecmdr.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: dblpipecmdr.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This program attaches to a Verilated Verilog IP core. @@ -21,11 +21,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2017, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -38,14 +38,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef DBLPIPECMDR_H #define DBLPIPECMDR_H diff --git a/sim/verilated/dbluartsim.cpp b/sim/dbluartsim.cpp similarity index 98% rename from sim/verilated/dbluartsim.cpp rename to sim/dbluartsim.cpp index 970bf55..4ede535 100644 --- a/sim/verilated/dbluartsim.cpp +++ b/sim/dbluartsim.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: dbluartsim.cpp -// +// {{{ // Project: wbuart32, a full featured UART with simulator // // Purpose: To forward a Verilator simulated UART link over a pair of @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include @@ -99,7 +99,7 @@ int DBLUARTSIM::setup_listener(const int port) { DBLUARTSIM::DBLUARTSIM(const int port, const bool copy_to_stdout) : m_copy(copy_to_stdout) { - m_debug = true; + m_debug = false; m_con = m_cmd = -1; m_skt = setup_listener(port); m_console = setup_listener(port+1); diff --git a/sim/verilated/dbluartsim.h b/sim/dbluartsim.h similarity index 96% rename from sim/verilated/dbluartsim.h rename to sim/dbluartsim.h index 59f25c6..83635af 100644 --- a/sim/verilated/dbluartsim.h +++ b/sim/dbluartsim.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: dbluartsim.h -// +// {{{ // Project: wbuart32, a full featured UART with simulator // // Purpose: To forward a Verilator simulated UART link, demultiplexed over @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef DBLUARTSIM_H #define DBLUARTSIM_H diff --git a/sim/verilated/flashsim.cpp b/sim/flashsim.cpp similarity index 98% rename from sim/verilated/flashsim.cpp rename to sim/flashsim.cpp index c2694e1..cdad964 100644 --- a/sim/verilated/flashsim.cpp +++ b/sim/flashsim.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: flashsim.cpp -// +// {{{ // Project: A Set of Wishbone Controlled SPI Flash Controllers // // Purpose: This library simulates the operation of a Quad-SPI commanded @@ -15,9 +15,9 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015,2017-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This file is part of the set of Wishbone controlled SPI flash controllers // project // @@ -35,14 +35,14 @@ // along with this program. (It's in the $(ROOT)/doc directory. Run make // with no target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: LGPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/lgpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include @@ -437,8 +437,9 @@ int FLASHSIM::operator()(const int csn, const int sck, const int dat) { m_state = QSPIF_QUAD_READ_CMD; m_mode = FM_QSPI; break; - case 0x000: - case 0x0ff: + case 0x000: // Dummy implementation of CMD 8'h00 + case 0x031: // Dummy implementation of CMD 8'h31 + case 0x0ff: // Dummy implementation of CMD 8'hff m_state = QSPIF_IDLE; m_mode = FM_SPI; break; @@ -582,7 +583,7 @@ int FLASHSIM::operator()(const int csn, const int sck, const int dat) { } } else if ((m_count >= 24+4*NDUMMY)&&(0 == (m_sreg&0x01))) { QOREG(m_mem[m_addr++]); - if (m_debug) printf("QSPIF[%08x]/QR = %02x\n", m_addr-1, m_oreg & 0x0ff); + // if (m_debug) printf("QSPIF[%08x]/QR = %02x\n", m_addr-1, m_oreg & 0x0ff); } else m_oreg = 0; break; case QSPIF_PP: diff --git a/sim/verilated/flashsim.h b/sim/flashsim.h similarity index 96% rename from sim/verilated/flashsim.h rename to sim/flashsim.h index c6f8bbb..4089afc 100644 --- a/sim/verilated/flashsim.h +++ b/sim/flashsim.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: flashsim.h -// +// {{{ // Project: A Set of Wishbone Controlled SPI Flash Controllers // // Purpose: This library simulates the operation of a Quad-SPI commanded @@ -13,9 +13,9 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This file is part of the set of Wishbone controlled SPI flash controllers // project // @@ -33,14 +33,14 @@ // along with this program. (It's in the $(ROOT)/doc directory. Run make // with no target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: LGPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/lgpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef FLASHSIM_H #define FLASHSIM_H @@ -111,7 +111,7 @@ class FLASHSIM { void load(const unsigned addr, const char *fname); void load(const uint32_t offset, const char *data, const uint32_t len); bool write_protect(void) { return ((m_sreg & QSPIF_WEL_FLAG)==0); } - bool write_in_progress(void) { return ((m_sreg | QSPIF_WIP_FLAG)!=0); } + bool write_in_progress(void) { return ((m_sreg & QSPIF_WIP_FLAG)!=0); } bool xip_mode(void) { return (QSPIF_QUAD_READ_IDLE == m_state); } bool deep_sleep(bool newval); bool deep_sleep(void) const; diff --git a/sim/verilated/main_tb.cpp b/sim/main_tb.cpp similarity index 64% rename from sim/verilated/main_tb.cpp rename to sim/main_tb.cpp index d7bce0c..2a765ad 100644 --- a/sim/verilated/main_tb.cpp +++ b/sim/main_tb.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: ./main_tb.cpp -// +// {{{ // Project: ZBasic, a generic toplevel implementation using the full ZipCPU // // DO NOT EDIT THIS FILE! @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} // // SIM.INCLUDE // @@ -53,75 +53,73 @@ #include "design.h" #include "regdefs.h" #include "testb.h" -#include "dbluartsim.h" -#include "byteswap.h" -#include "flashsim.h" #include "zipelf.h" +#include "byteswap.h" +#include "dbluartsim.h" #include "sdspisim.h" +#include "flashsim.h" // // SIM.DEFINES // // This tag is useful fr pasting in any #define values that // might then control the simulation following. // -#ifndef VVAR -#ifdef NEW_VERILATOR -#define VVAR(A) main__DOT_ ## A -#else -#define VVAR(A) v__DOT_ ## A -#endif -#endif - -#define block_ram VVAR(_bkrami__DOT__mem) +//////////////////////////////////////////////////////////////////////////////// +// +// ZipCPU simulation defines +// {{{ #ifndef VVAR -#ifdef NEW_VERILATOR +#ifdef ROOT_VERILATOR + +#include "Vmain___024root.h" +#define VVAR(A) rootp->main__DOT_ ## A + +#elif defined(NEW_VERILATOR) #define VVAR(A) main__DOT_ ## A #else #define VVAR(A) v__DOT_ ## A #endif #endif -#define CPUVAR(A) VVAR(_swic__DOT__thecpu__DOT_ ## A) +#define OPT_PIPELINED +#define CPUVAR(A) VVAR(_swic__DOT__thecpu__DOT__core__DOT_ ## A) -#define cpu_reset VVAR(_swic__DOT__cmd_reset) #define cpu_break VVAR(_swic__DOT__cpu_break) -#define cpu_cmd_halt VVAR(_swic__DOT__cmd_halt) -#define cpu_ipc CPUVAR(_ipc) -#define cpu_pf_pc CPUVAR(_pf_pc) -#define cpu_upc CPUVAR(_SET_USER_PC__DOT__r_upc) + #define cpu_gie CPUVAR(_SET_GIE__DOT__r_gie) -#define cpu_iflags CPUVAR(_w_iflags) -#define cpu_uflags CPUVAR(_w_uflags) -#define cpu_regs CPUVAR(_regset) -#define cpu_cmd_addr VVAR(_swic__DOT__cmd_addr) -#define cpu_bus_err CPUVAR(_bus_err) -#define cpu_ibus_err CPUVAR(_ibus_err_flag) -#define cpu_ubus_err CPUVAR(_r_ubus_err_flag) -#define cpu_mem_rdaddr CPUVAR(_domem__DOT__rdaddr) -#define cpu_mem_wraddr CPUVAR(_domem__DOT__wraddr) -#define cpu_sim CPUVAR(_alu_sim) -#define cpu_op_valid CPUVAR(_op_valid) -#define cpu_alu_ce CPUVAR(_alu_ce) -#define cpu_new_pc CPUVAR(_new_pc) -#define cpu_sim_immv CPUVAR(_alu_sim_immv) -#define cpu_alu_pc_valid CPUVAR(_alu_pc_valid) -#define cpu_mem_pc_valid CPUVAR(_mem_pc_valid) -#ifdef OPT_PIPELINED -#define cpu_alu_pc CPUVAR(_GEN_ALU_PC__DOT__r_alu_pc) +#define cpu_regs CPUVAR(_regset.m_storage) +// }}} +#ifndef VVAR +#ifdef NEW_VERILATOR +#define VVAR(A) main__DOT_ ## A #else -#define cpu_alu_pc CPUVAR(_op_pc) +#define VVAR(A) v__DOT_ ## A #endif -#ifdef OPT_CIS -#define cpu_alu_phase CPUVAR(_GEN_ALU_PHASE__DOT__r_alu_phase) #endif -#define cpu_wr_ce CPUVAR(_wr_reg_ce) -#define cpu_wr_reg_id CPUVAR(_wr_reg_id) -#define cpu_wr_gpreg CPUVAR(_wr_gpreg_vl) +#ifdef NEW_VERILATOR +#define block_ram rootp->main__DOT__bkrami__DOT__mem.m_storage +#else +#define block_ram VVAR(_bkrami__DOT__mem) +#endif + +//////////////////////////////////////////////////////////////////////////////// +// +// SD SPI Defines +// {{{ +//////////////////////////////////////////////////////////////////////////////// +// +// #ifndef VVAR -#ifdef NEW_VERILATOR +#ifdef ROOT_VERILATOR + +#include "Vmain___024root.h" +#define VVAR(A) rootp->main__DOT_ ## A + +#elif defined(NEW_VERILATOR) + #define VVAR(A) main__DOT_ ## A #else #define VVAR(A) v__DOT_ ## A @@ -155,7 +153,7 @@ #define sd_ll_fifo_wr_complete VVAR(_sdcard__DOT__ll_fifo_wr_complete) #define sd_use_fifo VVAR(_sdcard__DOT__r_use_fifo) #define sd_fifo_wr VVAR(_sdcard__DOT__r_fifo_wr) - +// }}} class MAINTB : public TESTB { public: // SIM.DEFNS @@ -163,14 +161,14 @@ class MAINTB : public TESTB { // If you have any simulation components, create a // SIM.DEFNS tag to have those components defined here // as part of the main_tb.cpp function. - DBLUARTSIM *m_wbu; -#ifdef FLASH_ACCESS - FLASHSIM *m_flash; -#endif // FLASH_ACCESS int m_cpu_bombed; + DBLUARTSIM *m_wbu; #ifdef SDSPI_ACCESS SDSPISIM m_sdcard; #endif // SDSPI_ACCESS +#ifdef FLASH_ACCESS + FLASHSIM *m_flash; +#endif // FLASH_ACCESS MAINTB(void) { // SIM.INIT // @@ -178,19 +176,19 @@ class MAINTB : public TESTB { // create a SIM.INIT tag. That tag's value will be pasted // here. // + // From zip + m_cpu_bombed = 0; // From wbu m_wbu = new DBLUARTSIM(); m_wbu->setup(100); - // From flash -#ifdef FLASH_ACCESS - m_flash = new FLASHSIM(FLASHLGLEN, false, 0, 6); -#endif // FLASH_ACCESS - // From zip - m_cpu_bombed = 0; // From sdcard #ifdef SDSPI_ACCESS m_sdcard.debug(false); #endif // SDSPI_ACCESS + // From flash +#ifdef FLASH_ACCESS + m_flash = new FLASHSIM(FLASHLGLEN, false, 0, 6); +#endif // FLASH_ACCESS } void reset(void) { @@ -231,29 +229,14 @@ class MAINTB : public TESTB { // following any falling edge of clock clk virtual void sim_clk_tick(void) { // Default clock tick - bool writeout; - // // SIM.TICK tags go here for SIM.CLOCK=clk // - // SIM.TICK from wbu - m_core->i_wbu_uart_rx = (*m_wbu)(m_core->o_wbu_uart_tx); - // SIM.TICK from flash -#ifdef FLASH_ACCESS - m_core->i_qspi_dat = m_flash->simtick( - m_core->o_qspi_cs_n, - m_core->o_qspi_sck, - m_core->o_qspi_dat, - m_core->o_qspi_mod); -#endif // FLASH_ACCESS // SIM.TICK from zip #ifdef INCLUDE_ZIPCPU // ZipCPU Sim instruction support - if ((m_core->cpu_sim) - &&(!m_core->cpu_new_pc)) { - // - execsim(m_core->cpu_sim_immv); - } + // {{{ + // Sim instructions are handled in Verilog if (m_cpu_bombed) { if (m_cpu_bombed++ > 12) @@ -263,9 +246,14 @@ class MAINTB : public TESTB { m_cpu_bombed++; dump(m_core->cpu_regs); } + // }}} #endif // INCLUDE_ZIPCPU + // SIM.TICK from wbu + m_core->i_wbu_uart_rx = (*m_wbu)(m_core->o_wbu_uart_tx); // SIM.TICK from sdcard + // SD Card simulation + // {{{ #ifdef SDSPI_ACCESS m_core->i_sd_data = m_sdcard((m_core->o_sd_data&8)?1:0, m_core->o_sd_sck, m_core->o_sd_cmd); @@ -273,72 +261,15 @@ class MAINTB : public TESTB { m_core->i_sd_data |= (m_core->o_sd_data&0x0e); m_core->i_sd_detect = 1; #endif // SDSPI_ACCESS - writeout = false; - // - // SIM.DBGCONDITION - // Set writeout to true here for debug by printf access - // to this routine - // - if (writeout) { - // - // SIM.DEBUG tags can print here, supporting - // any attempts to debug by printf. Following any - // code you place here, a newline will close the - // debug section. - // - // SIM.DEBUG from sdcard - /* - printf(" SDSPI[%d,%d(%d),(%d)]", - m_core->sd_cmd_busy, - m_core->sd_clk, - m_core->sd_cmd_state, - m_core->sd_rsp_state); - printf(" LL[%d,%2x->CK=%d/%2x,%s,ST=%2d,TX=%2x,RX=%2x->%d,%2x] ", - m_core->sd_ll_cmd_stb, - m_core->sd_ll_cmd_dat, - m_core->sd_ll_z_counter, - // (m_core->sd_ll_clk_counter==0)?1:0, - m_core->sd_ll_clk_counter, - (m_core->sd_ll_idle)?"IDLE":" ", - m_core->sd_ll_state, - m_core->sd_ll_byte, - m_core->sd_ll_ireg, - m_core->sd_ll_out_stb, - m_core->sd_ll_out_dat - ); - printf(" CRC=%02x/%2d", - m_core->sd_cmd_crc, - m_core->sd_cmd_crc_cnt); - printf(" SPI(%d,%d,%d/%d,%d)->?", - m_core->o_sf_cs_n, - m_core->o_sd_cs_n, - m_core->o_spi_sck, - m_core->v__DOT__sdcard_sck, - m_core->o_spi_mosi); - - printf(" CK=%d,LN=%d", - m_core->sd_clk, - m_core->sd_lgblklen); - - - if (m_core->sd_use_fifo){ - printf(" FIFO"); - if (m_core->sd_fifo_wr) - printf("-WR(%04x,%d,%d,%d)", - m_core->sd_fifo_rd_crc, - m_core->sd_fifo_rd_crc_stb, - m_core->sd_ll_fifo_pkt_state, - m_core->sd_have_data_response_token); - else - printf("-RD(%04x,%d,%d,%d)", - m_core->sd_fifo_wr_crc, - m_core->sd_fifo_wr_crc_stb, - m_core->sd_ll_fifo_wr_state, - m_core->sd_ll_fifo_wr_complete - ); - } - */ - } + // }}} + // SIM.TICK from flash +#ifdef FLASH_ACCESS + m_core->i_qspi_dat = m_flash->simtick( + m_core->o_qspi_cs_n, + m_core->o_qspi_sck, + m_core->o_qspi_dat, + m_core->o_qspi_mod); +#endif // FLASH_ACCESS } inline void tick_clk(void) { tick(); } @@ -371,11 +302,30 @@ class MAINTB : public TESTB { wlen = (wlen+3)&(-4); // Need to byte swap data to get it into the memory - char *bswapd = new char[len+8]; - memcpy(bswapd, &buf[offset], wlen); - byteswapbuf(len>>2, (uint32_t *)bswapd); - memcpy(&m_core->block_ram[start], bswapd, wlen); - delete bswapd; + if (32 == 32) { + char *bswapd = new char[len+8]; + memcpy(bswapd, &buf[offset], wlen); + byteswapbuf(len>>2, (uint32_t *)bswapd); + memcpy(&m_core->block_ram[start], bswapd, wlen); + delete[] bswapd; + } else { + for(unsigned jk=0; jkblock_ram[word_addr]; + cp = (char *)wp; + + subword_addr = start+jk; + subword_addr = ~subword_addr; + subword_addr &= 32/8-1; + // subword_addr = 32/8-subword_addr; + cp[subword_addr] = buf[offset+jk]; + } + } // AUTOFPGA::Now clean up anything else // Was there more to write than we wrote? if (addr + len > base + adrln) @@ -430,7 +380,10 @@ class MAINTB : public TESTB { // it will be pasated here. // #ifdef INCLUDE_ZIPCPU + // ZipCPU Access functions + // {{{ void loadelf(const char *elfname) { + // {{{ ELFSECTION **secpp, *secp; uint32_t entry; @@ -451,13 +404,25 @@ class MAINTB : public TESTB { } } free(secpp); } + // }}} + + void sim_write(unsigned addr, unsigned value) { + m_core->i_sim_write = 1; + m_core->i_sim_addr = (addr >> 2) & 0x07f; + m_core->i_sim_data = value; + tick(); + m_core->i_sim_write = 0; + m_core->i_sim_addr = 0; + m_core->i_sim_data = 0; + } bool gie(void) { return (m_core->cpu_gie); } void dump(const uint32_t *regp) { + // {{{ uint32_t uccv, iccv, ipc, upc; fflush(stderr); fflush(stdout); @@ -468,10 +433,15 @@ class MAINTB : public TESTB { printf("Supervisor mode\n"); printf("\n"); - iccv = m_core->cpu_iflags; - uccv = m_core->cpu_uflags; - ipc = m_core->cpu_ipc; - upc = m_core->cpu_upc; + iccv = 0; + uccv = 0; + ipc = 0; + upc = 0; + + // iccv = m_core->cpu_iflags; + // uccv = m_core->cpu_uflags; + // ipc = m_core->cpu_ipc; + // upc = m_core->cpu_upc; printf("sR0 : %08x ", regp[0]); printf("sR1 : %08x ", regp[1]); @@ -512,96 +482,9 @@ class MAINTB : public TESTB { fflush(stderr); fflush(stdout); } + // }}} - - void execsim(const uint32_t imm) { - uint32_t *regp = m_core->cpu_regs; - int rbase; - rbase = (gie())?16:0; - - fflush(stdout); - if ((imm & 0x03fffff)==0) - return; - // fprintf(stderr, "SIM-INSN(0x%08x)\n", imm); - if ((imm & 0x0fffff)==0x00100) { - // SIM Exit(0) - close(); - exit(0); - } else if ((imm & 0x0ffff0)==0x00310) { - // SIM Exit(User-Reg) - int rcode, rnum; - rnum = (imm&0x0f)+16; - rcode = regp[rnum] & 0x0ff; - if ((m_core->cpu_wr_ce)&&(m_core->cpu_wr_reg_id==rnum)) - rcode = m_core->cpu_wr_gpreg; - close(); - exit(rcode); - } else if ((imm & 0x0ffff0)==0x00300) { - // SIM Exit(Reg) - int rcode, rnum; - rnum = (imm&0x0f)+rbase; - rcode = regp[rnum] & 0x0ff; - if ((m_core->cpu_wr_ce)&&(m_core->cpu_wr_reg_id==rnum)) - rcode = m_core->cpu_wr_gpreg; - close(); - exit(rcode); - } else if ((imm & 0x0fff00)==0x00100) { - // SIM Exit(Imm) - int rcode; - rcode = imm & 0x0ff; - close(); - exit(rcode); - } else if ((imm & 0x0fffff)==0x002ff) { - // Full/unconditional dump - printf("SIM-DUMP\n"); - dump(regp); - } else if ((imm & 0x0ffff0)==0x00200) { - // Dump a register - int rcode, rnum; - rnum = (imm&0x0f)+rbase; - rcode = regp[rnum]; - if ((m_core->cpu_wr_ce)&&(m_core->cpu_wr_reg_id==rnum)) - rcode = m_core->cpu_wr_gpreg; - printf("%8lu @%08x R[%2d] = 0x%08x\n", m_time_ps/1000, - m_core->cpu_ipc, rnum, rcode); - } else if ((imm & 0x0ffff0)==0x00210) { - // Dump a user register - int rcode, rnum; - rnum = (imm&0x0f)+16; - rcode = regp[rnum] & 0x0ff; - if ((m_core->cpu_wr_ce)&&(m_core->cpu_wr_reg_id==rnum)) - rcode = m_core->cpu_wr_gpreg; - printf("%8lu @%08x uR[%2d] = 0x%08x\n", m_time_ps/1000, - m_core->cpu_ipc, rnum, rcode); - } else if ((imm & 0x0ffff0)==0x00230) { - // SOUT[User Reg] - int rcode, rnum; - rnum = (imm&0x0f)+16; - rcode = regp[rnum]; - if ((m_core->cpu_wr_ce)&&(m_core->cpu_wr_reg_id==rnum)) - rcode = m_core->cpu_wr_gpreg; - printf("%c", rcode&0x0ff); - } else if ((imm & 0x0fffe0)==0x00220) { - // SOUT[User Reg] - int rcode, rnum; - rnum = (imm&0x0f)+rbase; - rcode = regp[rnum]; - if ((m_core->cpu_wr_ce)&&(m_core->cpu_wr_reg_id==rnum)) - rcode = m_core->cpu_wr_gpreg; - printf("%c", rcode&0x0ff); - } else if ((imm & 0x0fff00)==0x00400) { - // SOUT[Imm] - printf("%c", imm&0x0ff); - } else { // if ((insn & 0x0f7c00000)==0x77800000) - uint32_t immv = imm & 0x03fffff; - // Simm instruction that we dont recognize - // if (imm) - // printf("SIM 0x%08x\n", immv); - printf("SIM 0x%08x (ipc = %08x, upc = %08x)\n", immv, - m_core->cpu_ipc, - m_core->cpu_upc); - } fflush(stdout); - } + // }}} #endif // INCLUDE_ZIPCPU #ifdef SDSPI_ACCESS void setsdcard(const char *fn) { diff --git a/sim/verilated/memsim.cpp b/sim/memsim.cpp similarity index 95% rename from sim/verilated/memsim.cpp rename to sim/memsim.cpp index bf25d95..7fac2bb 100644 --- a/sim/verilated/memsim.cpp +++ b/sim/memsim.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: memsim.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This creates a memory like device to act on a WISHBONE bus. @@ -18,11 +18,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -35,14 +35,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include diff --git a/sim/verilated/memsim.h b/sim/memsim.h similarity index 93% rename from sim/verilated/memsim.h rename to sim/memsim.h index 6873dfc..bf95fc1 100644 --- a/sim/verilated/memsim.h +++ b/sim/memsim.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: memsim.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This creates a memory like device to act on a WISHBONE bus. @@ -18,11 +18,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -35,14 +35,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef MEMSIM_H #define MEMSIM_H diff --git a/sim/verilated/pipecmdr.h b/sim/pipecmdr.h similarity index 97% rename from sim/verilated/pipecmdr.h rename to sim/pipecmdr.h index 03d0d59..11b039b 100644 --- a/sim/verilated/pipecmdr.h +++ b/sim/pipecmdr.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: pipecmdr.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This program attaches to a Verilated Verilog IP core. @@ -16,11 +16,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2017, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -33,14 +33,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef PIPECMDR_H #define PIPECMDR_H diff --git a/sim/verilated/port.h b/sim/port.h similarity index 91% rename from sim/verilated/port.h rename to sim/port.h index 7cfaab9..2ad1f0a 100644 --- a/sim/verilated/port.h +++ b/sim/port.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: port.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Defines the communication parameters necessary for communicating @@ -12,11 +12,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2018, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -29,14 +29,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef PORT_H #define PORT_H diff --git a/sim/verilated/sdspisim.cpp b/sim/sdspisim.cpp similarity index 98% rename from sim/verilated/sdspisim.cpp rename to sim/sdspisim.cpp index 2018d7a..8250d2c 100644 --- a/sim/verilated/sdspisim.cpp +++ b/sim/sdspisim.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: sdspisim.cpp -// +// {{{ // Project: Wishbone Controlled SD-Card Controller over SPI port // // Purpose: This library simulates the operation of a SPI commanded SD-Card, @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include diff --git a/sim/verilated/sdspisim.h b/sim/sdspisim.h similarity index 94% rename from sim/verilated/sdspisim.h rename to sim/sdspisim.h index 200174d..7a7dfd3 100644 --- a/sim/verilated/sdspisim.h +++ b/sim/sdspisim.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: sdspisim.h -// +// {{{ // Project: Wishbone Controlled SD-Card Controller over SPI port // // Purpose: This library simulates the operation of a SPI commanded SD-Card, @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef SDSPISIM_H #define SDSPISIM_H diff --git a/sim/verilated/spansion.hex b/sim/spansion.hex similarity index 100% rename from sim/verilated/spansion.hex rename to sim/spansion.hex diff --git a/sim/verilated/tbclock.h b/sim/tbclock.h similarity index 96% rename from sim/verilated/tbclock.h rename to sim/tbclock.h index 18fde63..26eab57 100644 --- a/sim/verilated/tbclock.h +++ b/sim/tbclock.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: tbclock.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: TBCLOCK is a class originally developed for the VideoZIP project @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef TBCLOCK_H #define TBCLOCK_H diff --git a/sim/verilated/testb.h b/sim/testb.h similarity index 94% rename from sim/verilated/testb.h rename to sim/testb.h index 615ccd4..5c5434c 100644 --- a/sim/verilated/testb.h +++ b/sim/testb.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: ./testb.h -// +// {{{ // Project: ZBasic, a generic toplevel implementation using the full ZipCPU // // DO NOT EDIT THIS FILE! @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef TESTB_H #define TESTB_H @@ -76,6 +76,7 @@ template class TESTB { // TESTB(void) { + // {{{ m_core = new VA; m_time_ps = 0ul; m_trace = NULL; @@ -83,14 +84,19 @@ template class TESTB { m_paused_trace = false; Verilated::traceEverOn(true); } + // }}} + virtual ~TESTB(void) { + // {{{ if (m_trace) m_trace->close(); delete m_core; m_core = NULL; } + // }}} // // opentrace() + // {{{ // // Useful for beginning a (VCD) trace. To open such a trace, just call // opentrace() with the name of the VCD file you'd like to trace @@ -105,19 +111,21 @@ template class TESTB { m_paused_trace = false; } } + // }}} // // trace() - // + // {{{ // A synonym for opentrace() above. // void trace(const char *vcdname) { opentrace(vcdname); } + // }}} // // pausetrace(pause) - // + // {{{ // Set/clear a flag telling us whether or not to write to the VCD trace // file. The default is to write to the file, but this can be changed // by calling pausetrace. pausetrace(false) will resume tracing, @@ -128,20 +136,22 @@ template class TESTB { m_paused_trace = pausetrace; return m_paused_trace; } + // }}} // // pausetrace() - // + // {{{ // Like pausetrace(bool) above, except that pausetrace() will return // the current status of the pausetrace flag above. Specifically, it // will return true if the trace has been paused or false otherwise. virtual bool pausetrace(void) { return m_paused_trace; } + // }}} // // closetrace() - // + // {{{ // Closes the open trace file. No more information will be written // to it virtual void closetrace(void) { @@ -151,10 +161,11 @@ template class TESTB { m_trace = NULL; } } + // }}} // // eval() - // + // {{{ // This is a synonym for Verilator's eval() function. It evaluates all // of the logic within the design. AutoFPGA based designs shouldn't // need to be calling this, they should call tick() instead. However, @@ -164,10 +175,11 @@ template class TESTB { virtual void eval(void) { m_core->eval(); } + // }}} // // tick() - // + // {{{ // tick() is the main entry point into this helper core. In general, // tick() will advance the clock by one clock tick. In a multiple clock // design, this will advance the clocks up until the nearest clock @@ -203,15 +215,19 @@ template class TESTB { // to advance their inputs based upon this clock sim_clk_tick(); } + // }}} virtual void sim_clk_tick(void) { + // {{{ // AutoFPGA will override this method within main_tb.cpp if any // @SIM.TICK key is present within a design component also // containing a @SIM.CLOCK key identifying this clock. That // component must also set m_changed to true. m_changed = false; } + // }}} virtual bool done(void) { + // {{{ if (m_done) return true; @@ -220,10 +236,11 @@ template class TESTB { return m_done; } + // }}} // // reset() - // + // {{{ // Sets the i_reset input for one clock tick. It's really just a // function for the capabilies shown below. You'll want to reset any // external input values before calling this though. @@ -233,6 +250,7 @@ template class TESTB { m_core->i_reset = 0; // printf("RESET\n"); } + // }}} }; #endif // TESTB diff --git a/sim/verilated/twoc.cpp b/sim/twoc.cpp similarity index 90% rename from sim/verilated/twoc.cpp rename to sim/twoc.cpp index 429b88f..4174fde 100644 --- a/sim/verilated/twoc.cpp +++ b/sim/twoc.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: twoc.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Some various two's complement related C++ helper routines. @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include "twoc.h" long sbits(const long val, const int bits) { diff --git a/sim/verilated/twoc.h b/sim/twoc.h similarity index 90% rename from sim/verilated/twoc.h rename to sim/twoc.h index cf48564..68fb544 100644 --- a/sim/verilated/twoc.h +++ b/sim/twoc.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: twoc.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Some various two's complement related C++ helper routines. @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef TWOC_H #define TWOC_H diff --git a/sim/verilated/uartsim.cpp b/sim/uartsim.cpp similarity index 97% rename from sim/verilated/uartsim.cpp rename to sim/uartsim.cpp index 45efb2e..0cc4506 100644 --- a/sim/verilated/uartsim.cpp +++ b/sim/uartsim.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: uartsim.cpp -// +// {{{ // Project: wbuart32, a full featured UART with simulator // // Purpose: To forward a Verilator simulated UART link over a TCP/IP pipe. @@ -10,11 +10,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -27,14 +27,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include diff --git a/sim/verilated/uartsim.h b/sim/uartsim.h similarity index 96% rename from sim/verilated/uartsim.h rename to sim/uartsim.h index 3e5f5bb..a953145 100644 --- a/sim/verilated/uartsim.h +++ b/sim/uartsim.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: uartsim.h -// +// {{{ // Project: wbuart32, a full featured UART with simulator // // Purpose: To forward a Verilator simulated UART link over a TCP/IP pipe. @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef UARTSIM_H #define UARTSIM_H diff --git a/sim/verilated/vversion.sh b/sim/vversion.sh similarity index 69% rename from sim/verilated/vversion.sh rename to sim/vversion.sh index c2a2178..7c51b28 100755 --- a/sim/verilated/vversion.sh +++ b/sim/vversion.sh @@ -2,7 +2,7 @@ ################################################################################ ## ## Filename: vversion.sh -## +## {{{ ## Project: ZBasic, a generic toplevel impl using the full ZipCPU ## ## Purpose: To determine whether or not the verilator prefix for internal @@ -14,11 +14,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2018, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -31,15 +31,15 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## -if [[ -x ${VERILATOR_ROOT}/bin/verilator ]]; +## }}} +if [[ x${VERILATOR_ROOT} != "x" && -x ${VERILATOR_ROOT}/bin/verilator ]]; then export VERILATOR=${VERILATOR_ROOT}/bin/verilator fi @@ -58,7 +58,19 @@ VVER=`echo ${VVERLINE} | cut -d " " -f 2` LATER=`echo $VVER \>= 3.9 | bc` if [[ $LATER > 0 ]]; then - echo "-DNEW_VERILATOR" + RLATER=`echo $VVER \>= 4.2 | bc` + if [[ $RLATER > 0 ]]; + then + ## I'm not quite certain when Verilator started requiring a further + ## subreference through rootp-> and including the Vdesgin___024root.h + ## include file. My best guess is that it is Verilator 4.2, but I don't + ## know that for certain. What I do know is that on the development + ## verrsion 4.211, it requires different semantics to peek at register + ## names. This is our attempt to capture that dependency. + echo "-DROOT_VERILATOR" + else + echo "-DNEW_VERILATOR" + fi else echo "-DOLD_VERILATOR" fi diff --git a/sim/verilated/zipelf.cpp b/sim/zipelf.cpp similarity index 98% rename from sim/verilated/zipelf.cpp rename to sim/zipelf.cpp index f29d0ff..b15a3f9 100644 --- a/sim/verilated/zipelf.cpp +++ b/sim/zipelf.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zipelf.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: @@ -11,9 +11,9 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or // modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at @@ -28,15 +28,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// - +// }}} #include #include #include diff --git a/sim/verilated/zipelf.h b/sim/zipelf.h similarity index 89% rename from sim/verilated/zipelf.h rename to sim/zipelf.h index 467731b..4c01ad2 100644 --- a/sim/verilated/zipelf.h +++ b/sim/zipelf.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zipelf.h -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,14 +28,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef ZIPELF_H #define ZIPELF_H diff --git a/sw/board/.gitignore b/sw/board/.gitignore index 1cfe38f..bf25344 100644 --- a/sw/board/.gitignore +++ b/sw/board/.gitignore @@ -6,6 +6,10 @@ contest contest.txt hello hello.txt +hellostep +hellostep.txt +lockcheck +lockcheck.txt sdtest sdtest.txt gpiotoggle diff --git a/sw/board/Makefile b/sw/board/Makefile index 9637317..cfd5630 100644 --- a/sw/board/Makefile +++ b/sw/board/Makefile @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: Makefile -## +## {{{ ## Project: ZBasic, a generic toplevel impl using the full ZipCPU ## ## Purpose: @@ -10,11 +10,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2016-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2016-2021, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -27,16 +27,16 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} .PHONY: all -PROGRAMS := hello sdtest cputest gpiotoggle contest +PROGRAMS := hello sdtest cputest gpiotoggle contest hellostep lockcheck hellosim all: $(PROGRAMS) # # @@ -51,7 +51,7 @@ SUBMAKE:= $(MAKE) --no-print-directory -C # # TTTT := tttt -SOURCES := hello.c sdtest.c cputest.c gpiotoggle.c contest.c +SOURCES := hello.c sdtest.c cputest.c gpiotoggle.c contest.c # rintftst.c HEADERS := DUMPRTL := -fdump-rtl-all DUMPTREE:= -fdump-tree-all @@ -96,7 +96,8 @@ $(OBJDIR)/cputest.s: cputest.c $(CC) $(CFLAGS) -S -Wa,-cis -fno-builtin $< -o $@ cputest: $(OBJDIR)/cputestcis.o board.ld $(LIB) - $(CC) $(CFLAGS) $(LFLAGS) $< $(LIBS) -o $@ + # $(CC) $(CFLAGS) $(LFLAGS) $< $(LIBS) -o $@ + $(CC) $(CFLAGS) $(LBKRAM) $< $(LIBS) -o $@ $(OBJDIR)/contest.o: contest.c @@ -108,6 +109,8 @@ contest: $(OBJDIR)/contest.o $(OBJDIR)/txfns.o board.ld $(LIB) $(OBJDIR)/hello.o: hello.c $(CC) -O3 -c $< -o $@ +$(OBJDIR)/rintftst.o: rintftst.c + $(CC) $(CFLAGS) -O3 -c $< -o $@ $(OBJDIR)/gettysburg.o: gettysburg.c $(CC) -O3 -c $< -o $@ $(OBJDIR)/gpiotoggle.o: gpiotoggle.c @@ -116,12 +119,21 @@ $(OBJDIR)/gpiotoggle.o: gpiotoggle.c hello: $(OBJDIR)/hello.o board.ld $(LIB) $(CC) $(CFLAGS) $(LFLAGS) $< $(LIBS) -o $@ +rintftst: $(OBJDIR)/rintftst.o $(OBJDIR)/txfns.o board.ld $(LIB) + $(CC) $(CFLAGS) $(LFLAGS) $< $(OBJDIR)/txfns.o $(LIBS) -o $@ + sdtest: $(OBJDIR)/sdtest.o board.ld $(LIB) $(CC) $(CFLAGS) $(LFLAGS) $< $(LIBS) -o $@ gpiotoggle: $(OBJDIR)/gpiotoggle.o bkram.ld $(LIB) $(CC) $(CFLAGS) $(LFLAGS) $< $(LIBS) -o $@ +hellostep: $(OBJDIR)/hellostep.o bkram.ld $(OBJDIR)/txfns.o $(LIB) + $(CC) $(CFLAGS) $(LFLAGS) $< $(OBJDIR)/txfns.o $(LIBS) -o $@ + +lockcheck: $(OBJDIR)/lockcheck.o $(OBJDIR)/txfns.o bkram.ld $(LIB) + $(CC) $(CFLAGS) $(LFLAGS) $< $(OBJDIR)/txfns.o $(LIBS) -o $@ + .PHONY: iftttt ifeq (,$(wildcard tttt/)) iftttt: diff --git a/sw/board/bkram.ld b/sw/board/bkram.ld index 216d0ab..a7b5db8 100644 --- a/sw/board/bkram.ld +++ b/sw/board/bkram.ld @@ -1,7 +1,7 @@ /******************************************************************************* * * Filename: bkram.ld -* +* {{{ * Project: ZBasic, a generic toplevel implementation using the full ZipCPU * * DO NOT EDIT THIS FILE! @@ -14,11 +14,11 @@ * Gisselquist Technology, LLC * /******************************************************************************* -* -* Copyright (C) 2017-2020, Gisselquist Technology, LLC -* +* }}} +* Copyright (C) 2017-2022, Gisselquist Technology, LLC +* {{{ * This program is free software (firmware): you can redistribute it and/or -* modify it under the terms of the GNU General Public License as published +* modify it under the terms of the GNU General Public License as published * by the Free Software Foundation, either version 3 of the License, or (at * your option) any later version. * @@ -31,14 +31,14 @@ * with this program. (It's in the $(ROOT)/doc directory. Run make with no * target there if the PDF file isn't present.) If not, see * for a copy. -* +* }}} * License: GPL, v3, as defined and found on www.gnu.org, +* {{{ * http://www.gnu.org/licenses/gpl.html * -* /******************************************************************************* * -* +* }}} */ ENTRY(_start) @@ -48,7 +48,9 @@ MEMORY * has a defined name in its @LD.NAME tag, it will be listed here * under that name, otherwise it will be listed under it's * @$(PREFIX) tag with an underscore prepended to it. The permissions - * are given by the @LD.PERM tag. If no permission tag exists, a + * are given by the @LD.PERM tag. Allowable permissions include + * 'r' (read only), 'rx' (read and execute, but no writes), + * 'wx' (read, write, and execute). If no permission tag exists, a * permission of 'r' will be assumed. */ bkram(wx) : ORIGIN = 0x00c00000, LENGTH = 0x00100000 diff --git a/sw/board/board.ld b/sw/board/board.ld index bba997a..c528ba3 100644 --- a/sw/board/board.ld +++ b/sw/board/board.ld @@ -1,7 +1,7 @@ /******************************************************************************* * * Filename: board.ld -* +* {{{ * Project: ZBasic, a generic toplevel implementation using the full ZipCPU * * DO NOT EDIT THIS FILE! @@ -14,11 +14,11 @@ * Gisselquist Technology, LLC * /******************************************************************************* -* -* Copyright (C) 2017-2020, Gisselquist Technology, LLC -* +* }}} +* Copyright (C) 2017-2022, Gisselquist Technology, LLC +* {{{ * This program is free software (firmware): you can redistribute it and/or -* modify it under the terms of the GNU General Public License as published +* modify it under the terms of the GNU General Public License as published * by the Free Software Foundation, either version 3 of the License, or (at * your option) any later version. * @@ -31,14 +31,14 @@ * with this program. (It's in the $(ROOT)/doc directory. Run make with no * target there if the PDF file isn't present.) If not, see * for a copy. -* +* }}} * License: GPL, v3, as defined and found on www.gnu.org, +* {{{ * http://www.gnu.org/licenses/gpl.html * -* /******************************************************************************* * -* +* }}} */ ENTRY(_start) @@ -48,7 +48,9 @@ MEMORY * has a defined name in its @LD.NAME tag, it will be listed here * under that name, otherwise it will be listed under it's * @$(PREFIX) tag with an underscore prepended to it. The permissions - * are given by the @LD.PERM tag. If no permission tag exists, a + * are given by the @LD.PERM tag. Allowable permissions include + * 'r' (read only), 'rx' (read and execute, but no writes), + * 'wx' (read, write, and execute). If no permission tag exists, a * permission of 'r' will be assumed. */ bkram(wx) : ORIGIN = 0x00c00000, LENGTH = 0x00100000 diff --git a/sw/board/contest.c b/sw/board/contest.c index 42de449..95a354f 100644 --- a/sw/board/contest.c +++ b/sw/board/contest.c @@ -1,21 +1,22 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: contest.c -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // -// Purpose: Quick test of the interconnect, make sure everything responds -// like we are expecting it to. +// Purpose: A "connection-test". This is a quick test of the interconnect, +// just to make sure everything on the bus responds like we are +// expecting it to. // // Creator: Dan Gisselquist, Ph.D. // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2019, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2019-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,59 +29,21 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include "board.h" #include "txfns.h" #include "zipcpu.h" #define SKIP_BOOTLOADER -/* -asm("\t.section\t.start,\"ax\",@progbits\n" - "\t.global\t_start\n" -"_start:" "\t; Here's the global ZipCPU entry point upon reset/reboot\n" - "\tLDI\t_top_of_stack,SP" "\t; Set up our supervisor stack ptr\n" - "\tMOV\t_kernel_is_dead(PC),uPC" "\t; Set user PC pointer to somewhere valid\n" -#ifndef SKIP_BOOTLOADER - "\tMOV\t_after_bootloader(PC),R0" " ; JSR to the bootloader routine\n" - "\tBRA\t_bootloader\n" -"_after_bootloader:\n" - "\tLDI\t_top_of_stack,SP" "\t; Set up our supervisor stack ptr\n" - "\tOR\t0xc000,CC" "\t; Clear the both caches\n" -#endif -#ifdef __USE_INIT_FINIT - "\tJSR\tinit" "\t; Initialize any constructor code\n" - // - "\tLDI\tfini,R1" "\t; \n" - "\tJSR\t_atexit" "\t; Initialize any constructor code\n" -#endif - // - "\tCLR\tR1" "\t; argc = 0\n" - "\tMOV\t_argv(PC),R2" "\t; argv = &0\n" -// "\tLDI\t__env,R3" "\t; env = NULL\n" - "\tJSR\tmain" "\t; Call the user main() function\n" - // -"_graceful_kernel_exit:" "\t; Halt on any return from main--gracefully\n" -// "\tJSR\texit\n" "\t; Call the _exit as part of exiting\n" -"\t.global\t_exit\n" -"_exit:\n" - "\tNEXIT\tR1\n" "\t; If in simulation, call an exit function\n" -"_kernel_is_dead:" "\t; Halt the CPU\n" - "\tHALT\n" "\t; We should *never* continue following a\n" - "\tBRA\t_kernel_is_dead" "\t; halt, do something useful if so ??\n" -"_argv:\n" - "\t.WORD\t0,0\n" - "\t.section\t.text"); -*/ - -void rwcheckm(const char *str, volatile unsigned *const a, const int mask){ +void rwcheckm(const char *str, volatile unsigned *const a, const int mask) { const unsigned NONCE = 0x12345678; unsigned original, failed; volatile char *const cp = (volatile char *const)a; diff --git a/sw/board/cputest.c b/sw/board/cputest.c index 32b4b88..2e4ee42 100644 --- a/sw/board/cputest.c +++ b/sw/board/cputest.c @@ -1,23 +1,25 @@ -/////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// // // Filename: cputest.c -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: To test the CPU, it's instructions, cache, and pipeline, to make // certain that it works. This includes testing that each of the // instructions works, as well as any strange instruction combinations. // +// This test does not check the LOCK instruction. This capability is +// tested via the lockcheck program also found in the same directory. // // Creator: Dan Gisselquist, Ph.D. // Gisselquist Technology, LLC // -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +32,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // +//////////////////////////////////////////////////////////////////////////////// // -/////////////////////////////////////////////////////////////////////////////// -// -// +// }}} #include #include "board.h" #include "zipcpu.h" @@ -82,7 +84,7 @@ unsigned zip_ucc(void); unsigned zip_cc(void); -void zip_save_context(int *); +void zip_save_context(void *); void zip_halt(void); @@ -106,6 +108,7 @@ void tx4hex(int num); extern int run_test(void *pc, void *stack); +// {{{ asm("\t.text\n\t.global\trun_test\n" "\t.type\trun_test,@function\n" "run_test:\n" @@ -132,8 +135,10 @@ asm("\t.text\n\t.global\trun_test\n" "\tAND\t0xffffffdf,CC\n" // Works with 5 NOOPS, works with 3 NOOPS, works with 1 NOOP "\tJMP\tR0\n"); +// }}} extern int idle_test(void); +// {{{ asm("\t.text\n\t.global\tidle_test\n" "\t.type\tidle_test,@function\n" "idle_test:\n" @@ -163,8 +168,10 @@ asm("\t.text\n\t.global\tidle_test\n" "idle_loop:\n" "\tWAIT\n" "\tBRA idle_loop\n"); +// }}} void break_one(void); +// {{{ asm("\t.text\n\t.global\tbreak_one\n" "\t.type\tbreak_one,@function\n" "break_one:\n" @@ -172,8 +179,10 @@ asm("\t.text\n\t.global\tbreak_one\n" "\tBREAK\n" "\tLDI\t1,R1\n" // Test fails "\tJMP\tR0"); +// }}} void break_two(void); +// {{{ // Can we stop a break before we hit it? asm("\t.text\n\t.global\tbreak_two\n" "\t.type\tbreak_two,@function\n" @@ -181,8 +190,10 @@ asm("\t.text\n\t.global\tbreak_two\n" "\tLDI\t0,R1\n" "\tJMP\tR0\n" "\tBREAK\n"); +// }}} void break_three(void); +// {{{ // Can we jump to a break, and still have the uPC match asm("\t.text\n\t.global\tbreak_three\n" "\t.type\tbreak_three,@function\n" @@ -190,8 +201,10 @@ asm("\t.text\n\t.global\tbreak_three\n" // we had succeeded. "break_three:\n" "\tBREAK\n"); +// }}} void early_branch_test(void); +// {{{ asm("\t.text\n\t.global\tearly_branch_test\n" "\t.type\tearly_branch_test,@function\n" "early_branch_test:\n" @@ -236,8 +249,10 @@ asm("\t.text\n\t.global\tearly_branch_test\n" "_eb_f:\n" "\tLDI\t0,R1\n" "\tJMP\tR0"); +// }}} void trap_test_and(void); +// {{{ asm("\t.text\n\t.global\ttrap_test_and\n" "\t.type\ttrap_test_and,@function\n" "trap_test_and:\n" @@ -245,8 +260,10 @@ asm("\t.text\n\t.global\ttrap_test_and\n" "\tAND\t0xffffffdf,CC\n" "\tLDI\t1,R1\n" // Test fails "\tJMP\tR0"); +// }}} void trap_test_clr(void); +// {{{ asm("\t.text\n\t.global\ttrap_test_clr\n" "\t.type\ttrap_test_clr,@function\n" "trap_test_clr:\n" @@ -254,8 +271,10 @@ asm("\t.text\n\t.global\ttrap_test_clr\n" "\tCLR\tCC\n" "\tLDI\t1,R1\n" // Test fails "\tJMP\tR0"); +// }}} void overflow_test(void); +// {{{ asm("\t.text\n\t.global\toverflow_test\n" "\t.type\toverflow_test,@function\n" "overflow_test:\n" @@ -287,9 +306,10 @@ asm("\t.text\n\t.global\toverflow_test\n" "\tOR\tR3,R1\n" // And return the results "\tJMP\tR0"); - +// }}} void carry_test(void); +// {{{ asm("\t.text\n\t.global\tcarry_test\n" "\t.type\tcarry_test,@function\n" "carry_test:\n" @@ -317,8 +337,10 @@ asm("\t.text\n\t.global\tcarry_test\n" "\tXOR\t31,R3\n" "\tOR\tR3,R1\n" "\tJMP\tR0"); +// }}} void loop_test(void); +// {{{ asm("\t.text\n\t.global\tloop_test\n" "\t.type\tloop_test,@function\n" "loop_test:\n" @@ -365,10 +387,12 @@ asm("\t.text\n\t.global\tloop_test\n" "\tADD\t4,SP\n" // "\tJMP\tR0\n"); +// }}} +void shift_test(void); +// {{{ // Test whether or not LSL, LSR, and ASR instructions work, together with their // carry flags -void shift_test(void); asm("\t.text\n\t.global\tshift_test\n" "\t.type\tshift_test,@function\n" "shift_test:\n" @@ -437,8 +461,10 @@ asm("\t.text\n\t.global\tshift_test\n" "\tOR\tR4,R3\n" "\tOR\tR3,R1\n" "\tJMP\tR0"); +// }}} int sw_brev(int v); +// {{{ asm("\t.text\n\t.global\tsw_brev\n" "\t.type\tsw_brev,@function\n" "sw_brev:\n" @@ -460,8 +486,10 @@ asm("\t.text\n\t.global\tsw_brev\n" "\tLW\t4(SP),R3\n" "\tADD\t8,SP\n" "\tJMP\tR0"); +// }}} void pipeline_stack_test(void); +// {{{ asm("\t.text\n\t.global\tpipeline_stack_test\n" "\t.type\tpipeline_stack_test,@function\n" "pipeline_stack_test:\n" @@ -502,8 +530,10 @@ asm("\t.text\n\t.global\tpipeline_stack_test\n" "\tADD\t4,SP\n" "\tJMP\tR0\n" ); +// }}} void pipeline_stack_test_component(void); +// {{{ asm("\t.text\n\t.global\tpipeline_stack_test_component\n" "\t.type\tpipeline_stack_test_component,@function\n" "pipeline_stack_test_component:\n" @@ -549,9 +579,10 @@ asm("\t.text\n\t.global\tpipeline_stack_test_component\n" "\tLW\t48(SP),R12\n" "\tADD\t52,SP\n" "\tJMP\tR0\n"); +// }}} -//mpy_test void mpy_test(void); +// {{{ asm("\t.text\n\t.global\tmpy_test\n" "\t.type\tmpy_test,@function\n" "mpy_test:\n" @@ -583,25 +614,31 @@ asm("\t.text\n\t.global\tmpy_test\n" "end_mpy_137_test_loop:\n" // Second test ... whatever that might be "\tJMP\tR0\n"); +// }}} unsigned soft_mpyuhi(unsigned, unsigned); int soft_mpyshi(int,int); unsigned hard_mpyuhi(unsigned, unsigned); +// {{{ asm("\t.text\n\t.global\thard_mpyuhi\n" "\t.type\thard_mpyuhi,@function\n" "hard_mpyuhi:\n" "\tMPYUHI\tR2,R1\n" "\tRETN\n"); +// }}} int hard_mpyshi(int, int); +// {{{ asm("\t.text\n\t.global\thard_mpyshi\n" "\t.type\thard_mpyshi,@function\n" "hard_mpyshi:\n" "\tMPYSHI\tR2,R1\n" "\tRETN\n"); +// }}} void debugmpy(char *str, int a, int b, int s, int r) { + // {{{ #ifdef HAVE_SCOPE // Trigger the scope, if it hasn't been triggered yet // but ... dont reset it if it has been. @@ -613,8 +650,10 @@ void debugmpy(char *str, int a, int b, int s, int r) { txstr("(Soft) = "); txhex(r); txstr("(Hard)\r\n"); } +// }}} int mpyhi_test(void) { + // {{{ int a = 0xf97e27ab, b = 0; while(b<0x6fffffff) { @@ -646,8 +685,10 @@ int mpyhi_test(void) { return 0; } +// }}} unsigned soft_mpyuhi(unsigned a, unsigned b) { + // {{{ unsigned alo, ahi; unsigned rhi, rlhi, rllo; @@ -701,8 +742,10 @@ unsigned soft_mpyuhi(unsigned a, unsigned b) { return rhi; } +// }}} int soft_mpyshi(int a, int b) { + // {{{ unsigned sgn, r, p; sgn = ((a^b)>>31)&0x01; @@ -722,27 +765,79 @@ int soft_mpyshi(int a, int b) { r += 1; return r; } +// }}} -int div_test(void); -asm("\t.text\n\t.global\tdiv_test\n" - "\t.type\tdiv_test,@function\n" -"div_test:\n" +int divu_test(void); +// {{{ +asm("\t.text\n\t.global\tdivu_test\n" + "\t.type\tdivu_test,@function\n" +"divu_test:\n" "\tLDI\t0x4881a7,R4\n" "\tLDI\t0x2d5108b,R2\n" "\tLDI\t10,R3\n" - "\tDIVU\tR3,R2\n" - "\tCMP\tR4,R2\n" + "\tDIVU\tR3,R2\n" // R3 = 0x2d5108b /= 10 + "\tCMP\tR4,R2\n" // Compare DIVU result to 0x4881a7 "\tLDILO.NZ\t1,R1\n" "\tRETN.NZ\n" "\tLDI\t0x2d5108b,R2\n" - "\tDIVU\t10,R2\n" + "\tDIVU\t10,R2\n" // R2 = 0x2d5108b /= 10 + "\tCMP\tR4,R2\n" // Compare R2 to 0x4881a7 + "\tLDILO.NZ\t1,R1\n" + "\tRETN\n"); +// }}} + +int divs_test(void); +// {{{ +asm("\t.text\n\t.global\tdivs_test\n" + "\t.type\tdivs_test,@function\n" +"divs_test:\n" + "\tLDI\t-26,R4\n" // Expected result + "\tLDI\t131,R2\n" + "\tLDI\t-5,R3\n" + "\tDIVS\tR3,R2\n" // 131 / -5 "\tCMP\tR4,R2\n" "\tLDILO.NZ\t1,R1\n" + "\tRETN.NZ\n" + // 0x2653 * 0x098953d = 0x16d79_f70c7 + // -883 * -999671 = -882709493 + // + "\tLDI\t883,R2\n" // R2 = A + "\tLDI\t999671,R3\n" // R3 = B + "\tMOV\tR3,R4\n" + "\tMPY\tR2,R4\n" // R4 = A*B + // + "\tMOV\tR4,R5\n" // R5 = R4 = A*B + "\tDIVS\tR2,R5\n" // R5 = A * B / A + "\tCMP\tR3,R5\n" + "\tLDILO.NZ\t3,R1\n" + "\tRETN.NZ\n" + // + // -(A*B) / B + "\tMOV\tR4,R5\n" // R5 = R4 = A*B + "\tNEG\tR5\n" // R5 = -R4 = -A*B + "\tMOV\tR3,R6\n" // R6 = B + "\tNEG\tR6\n" // R6 = -B + "\tDIVS\tR2,R5\n" // R5 = A * B / A + "\tCMP\tR6,R5\n" + "\tLDILO.NZ\t5,R1\n" + "\tRETN.NZ\n" + // + // -(A*B) / -B + "\tMOV\tR4,R5\n" // R5 = R4 = A*B + "\tNEG\tR5\n" // R5 = -R4 = -A*B + "\tMOV\tR2,R6\n" // R6 = B + "\tNEG\tR6\n" // R6 = -B + "\tDIVS\tR6,R5\n" // R5 = A * B / A + "\tCMP\tR3,R5\n" + "\tLDILO.NZ\t7,R1\n" + // "\tRETN.NZ\n" + // "\tRETN\n"); +// }}} -//brev_test -//pipeline_test -- used to be called pipeline memory race conditions void pipeline_test(void); +// {{{ +//pipeline_test -- used to be called pipeline memory race conditions asm("\t.text\n\t.global\tpipeline_test\n" "\t.type\tpipeline_test,@function\n" "pipeline_test:\n" @@ -790,9 +885,11 @@ asm("\t.text\n\t.global\tpipeline_test\n" // "\tADD\t12,SP\n" "\tJMP\tR0\n"); +// }}} -//mempipe_test void mempipe_test(void); +// {{{ +//mempipe_test asm("\t.text\n\t.global\tmempipe_test\n" "\t.type\tmempipe_test,@function\n" "mempipe_test:\n" @@ -827,9 +924,11 @@ asm("\t.text\n\t.global\tmempipe_test\n" "\tLW\t(SP),R0\n" "\tADD\t16,SP\n" "\tJMP\tR0\n"); +// }}} -//cexec_test void cexec_test(void); +// {{{ +//cexec_test asm("\t.text\n\t.global\tcexec_test\n" "\t.type\tcexec_test,@function\n" "cexec_test:\n" @@ -845,15 +944,16 @@ asm("\t.text\n\t.global\tcexec_test\n" "\tLW\t(SP),R0\n" "\tADD\t4,SP\n" "\tJMP\tR0\n"); +// }}} +void nowaitpipe_test(void); +// {{{ // Pipeline stalls have been hideous problems for me. The CPU has been modified // with special logic to keep stages from stalling. For the most part, this // means that ALU and memory results may be accessed either before or as they // are written to the register file. This set of code is designed to test // whether this bypass logic works. // -//nowaitpipe_test -void nowaitpipe_test(void); asm("\t.text\n\t.global\tnowaitpipe_test\n" "\t.type\tnowaitpipe_test,@function\n" "nowaitpipe_test:\n" @@ -926,9 +1026,10 @@ asm("\t.text\n\t.global\tnowaitpipe_test\n" // "\tADD\t8,SP\n" "\tJMP\tR0\n"); +// }}} -//bcmem_test void bcmem_test(void); +// {{{ asm("\t.text\n\t.global\tbcmem_test\n" "\t.type\tbcmem_test,@function\n" "bcmem_test:\n" @@ -959,7 +1060,76 @@ asm("\t.text\n\t.global\tbcmem_test\n" // "\tADD\t4,SP\n" "\tJMP\tR0\n"); +// }}} + +void membyte_test(void); +// {{{ +unsigned memword; + +asm("\t.text\n\t.global\tmembyte_test\n" + "\t.type\tmembyte_test,@function\n" +"membyte_test:\n" + "\tCLR\tR1\n" // Return result + "\tLDI\tmemword,R2\n" // Data pointer + "\tMOV\tR2,R3\n" // Byte pointer + "\tCLR\tR4\n" // Zero word + "\tCLR\tR6\n" // Memory return value +"membyte_addr:\n" + "\tCLR\tR5\n" // Clear the Loop index +"membyte_loop:\n" + "\tSW\tR4,(R2)\n" // Clear the full word + "\tSB\tR5,(R3)\n" // Store a byte + "\tLB\t(R3),R6\n" // Read the byte back + "\tCMP\tR5,R6\n" + "\tBNZ\tmembyte_fail\n" + "\tADD\t1,R5\n" + "\tTEST\t0x0100,R5\n" // This will force a signedness test as well + "\tBZ\tmembyte_loop\n" + // + "\tADD\t1,R3\n" + "\tMOV\tR3,R6\n" + "\tTEST\t3,R6\n" + "\tBNZ\tmembyte_addr\n" + "\tRTN\n" +"membyte_fail:\n" + "\tLDI\t1,R1\n" + "\tRTN\n"); +// }}} + +void memhalf_test(void); +// {{{ +asm("\t.text\n\t.global\tmemhalf_test\n" + "\t.type\tmemhalf_test,@function\n" +"memhalf_test:\n" + "\tCLR\tR1\n" // Return result + "\tLDI\tmemword,R2\n" // Data pointer + "\tMOV\tR2,R3\n" // Byte pointer + "\tCLR\tR4\n" // Zero word + "\tCLR\tR6\n" // Memory return value +"memhalf_addr:\n" + "\tCLR\tR5\n" // Loop index +"memhalf_loop:\n" + "\tSW\tR4,(R2)\n" // Clear the full word + "\tSH\tR5,(R3)\n" // Store a half-word + "\tLH\t(R3),R6\n" // Read the half-word back + "\tXOR\tR5,R6\n" + "\tBNZ\tmemhalf_fail\n" + "\tADD\t5,R5\n" + "\tTEST\t0x010000,R5\n" // + "\tBZ\tmemhalf_loop\n" + // + "\tADD\t2,R3\n" + "\tMOV\tR3,R6\n" + "\tTEST\t3,R6\n" + "\tBNZ\tmemhalf_addr\n" + "\tRTN\n" +"memhalf_fail:\n" + "\tLDI\t1,R1\n" + "\tRTN\n"); +// }}} +void ill_test(void); +// {{{ // The illegal instruction test. Specifically, illegal instructions cannot be // allowed to execute. The PC must, upon completion, point to the illegal // instruction that caused the exception. @@ -967,32 +1137,37 @@ asm("\t.text\n\t.global\tbcmem_test\n" // To create our illegal instruction, we assume that the only the three // operations without arguments are NOOP, BREAK, LOCK, and so we envision a // fourth instruction to create. -void ill_test(void); asm("\t.text\n\t.global\till_test\n" "\t.type\till_test,@function\n" "ill_test:\n" // 0.111_1.110_11...... "\t.int\t0x7ec00000\n" "\tJMP\tR0\n"); +// }}} +void sim_test(void); +// {{{ // Are sim instructions considered valid? Just hit the illegal instruction // so we can report the result -void sim_test(void); asm("\t.text\n\t.global\tsim_test\n" "\t.type\tsim_test,@function\n" "sim_test:\n" // 0.111_1.111_10...... "\t.int\t0x7f800000\n" "\tJMP\tR0\n"); +// }}} -// Are CIS instructions considered valid? Try two compare instructions to -// see if they are built into our CPU. void cis_test(void); +// {{{ +// Are CIS (Compound Instruction Set) instructions considered valid? Try two +// compare instructions to see if CIS instruction support is built into our CPU. asm("\t.text\n\t.global\tcis_test\n" "\t.type\tcis_test,@function\n" "cis_test:\n" // 1.000_0.011._1.101_0.000 ... 1.000_1.011._1.110_0.000 "\t.int\t0x83d08be0\n" "\tJMP\tR0\n"); +// }}} void cmpeq_test(void); +// {{{ asm("\t.text\n\t.global\tcmpeq_test\n" "\t.type\tcmpeq_test,@function\n" "cmpeq_test:\n" @@ -1000,8 +1175,10 @@ asm("\t.text\n\t.global\tcmpeq_test\n" "\tCMP.Z\tR3,R4\n" "\tLDILO.NZ\t1,R1\n" "\tJMP\tR0\n"); +// }}} void cmpneq_test(void); +// {{{ asm("\t.text\n\t.global\tcmpneq_test\n" "\t.type\tcmpneq_test,@function\n" "cmpneq_test:\n" @@ -1010,7 +1187,10 @@ asm("\t.text\n\t.global\tcmpneq_test\n" "\tCMP.Z\tR3,R4\n" "\tLDILO.Z\t1,R0\n" "\tJMP\tR0\n"); -// +// }}} + +void ccreg_test(void); +// {{{ // The CC register has some ... unique requirements associated with it. // Particularly, flags are unavailable until after an ALU operation completes, // and they can't really be bypassed for the CC register. After writeback, @@ -1023,7 +1203,6 @@ asm("\t.text\n\t.global\tcmpneq_test\n" // Here, let's see if our pipeline can successfully navigate any of these // issues. // -void ccreg_test(void); asm("\t.text\n\t.global\tccreg_test\n" "\t.type\tccreg_test,@function\n" "ccreg_test:\n" @@ -1057,10 +1236,12 @@ asm("\t.text\n\t.global\tccreg_test\n" // Test #4: Can we load the CC plus a value into a register? // I don't think so ... "\tJMP\tR0\n"); +// }}} -// Multiple argument test __attribute__((noinline)) int multiarg_subroutine(int a, int b, int c, int d, int e, int f, int g) { +// {{{ +// Multiple argument test if (a!=0) return 1; if (b!=1) return 2; if (c!=2) return 4; @@ -1070,24 +1251,181 @@ int multiarg_subroutine(int a, int b, int c, int d, int e, int f, int g) { if (g!=6) return 64; return 0; } +// }}} int multiarg_test(void) { + // {{{ return multiarg_subroutine(0,1,2,3,4,5,6); } +// }}} + +int step_alu_test(void); +// {{{ +asm("\t.text\n\t.global\tstep_mpy_test\n" + "\t.type\tstep_alu_test,@function\n" +"step_alu_test:\n" + "\tXOR R1,R0\n" + "\tBREAK\n" +); +// }}} + +int step_cis_test(void); +// {{{ +asm("\t.text\n\t.global\tstep_cis_test\n" + "\t.type\tstep_cis_test,@function\n" +"step_cis_test:\n" + "\tADD 1,R1\n" + "\tADD 1,R2\n" + "\tBREAK\n" +); +// }}} + +int step_break_test(void); +// {{{ +asm("\t.text\n\t.global\tstep_break_test\n" + "\t.type\tstep_break_test,@function\n" +"step_break_test:\n" + "\tBREAK\n" + "\tSW R1,(R1)\n" // Should generate a bus error, if ever executed +); +// }}} + +int step_mpy_test(void); +// {{{ +asm("\t.text\n\t.global\tstep_mpy_test\n" + "\t.type\tstep_mpy_test,@function\n" +"step_mpy_test:\n" + "\tMPY R6,R7\n" + "\tBREAK\n" +); +// }}} + +int step_div_test(void); +// {{{ +asm("\t.text\n\t.global\tstep_div_test\n" + "\t.type\tstep_div_test,@function\n" +"step_div_test:\n" + "\tDIVU R6,R5\n" + "\tBREAK\n" +); +// }}} + +int step_diverr_test(void); +// {{{ +asm("\t.text\n\t.global\tstep_diverr_test\n" + "\t.type\tstep_diverr_test,@function\n" +"step_diverr_test:\n" + "\tDIVU R0,R5\n" + "\tBREAK\n" +); +// }}} + +int step_lod_test(void); +// {{{ +asm("\t.text\n\t.global\tstep_lod_test\n" + "\t.type\tstep_lod_test,@function\n" +"step_lod_test:\n" + "\tLW (R4),R0\n" + "\tBREAK\n" +); +// }}} + +int step_sto_test(void); +// {{{ +asm("\t.text\n\t.global\tstep_sto_test\n" + "\t.type\tstep_sto_test,@function\n" +"step_sto_test:\n" + "\tSW R0,(R4)\n" + "\tBREAK\n" +); +// }}} + +int step_loderr_test(void); +// {{{ +asm("\t.text\n\t.global\tstep_loderr_test\n" + "\t.type\tstep_loderr_test,@function\n" +"step_loderr_test:\n" + "\tLW (R1),R0\n" + "\tBREAK\n" +); +// }}} + +int step_stoerr_test(void); +// {{{ +asm("\t.text\n\t.global\tstep_stoerr_test\n" + "\t.type\tstep_stoerr_test,@function\n" +"step_stoerr_test:\n" + "\tSW R0,(R1)\n" + "\tBREAK\n" +); +// }}} + +int step_word = 0; + +extern int step_test(void *pc, void *stack); +// {{{ +asm("\t.text\n\t.global\tstep_test\n" + "\t.type\tstep_test,@function\n" +"step_test:\n" + "\tCLR\tR3\n" + "\tMOV\tR3,uR0\n" + "\tMOV\tR3,uR1\n" + "\tMOV\tR3,uR2\n" + "\tMOV\tR3,uR3\n" + "\tMOV\tR3,uR5\n" + "\tMOV\tR3,uR6\n" + "\tMOV\tR3,uR7\n" + "\tMOV\tR3,uR8\n" + "\tMOV\tR3,uR9\n" + "\tMOV\tR3,uR10\n" + "\tMOV\tR3,uR11\n" + "\tMOV\tR3,uR12\n" + // Put a some useful multiply values into uR5, uR6, and uR7 + // (Implied) CLR R3 + "\tMOV\t15+R3,uR5\n" // uR5 = 15 + "\tMOV\t5+R3,uR6\n" // uR6 = 5 + "\tMOV\t3+R3,uR7\n" // uR7 = 3 + // + // Put a valid address into uR4 + "\tLDI\tstep_word,R3\n" + "\tMOV\tR3,uR4\n" + // + "\tMOV\tR2,uSP\n" // uSP = stack + "\tLDI\t0x60,R3\n" + "\tMOV\tR3,uCC\n" // Set the STEP ang GIE bits of uCC + "\tMOV\tR1,uPC\n" // uPC = pc + "\tRTU\n" + // Return from running "a single" instruction + "\tLDI\t0,R1\n" + // If we step more or less than one word, it's an error + "\tMOV\tuPC,R3\n" + "\tSUB\tR1,R3\n" + "\tCMP\t4,R3\n" + "\tLDI.NZ\t1,R3\n" + // If we hit break, it's an error + "\tMOV\tuCC,R2\n" + "\tTEST\t0x80,R2\n" + "\tOR.NZ\t2,R1\n" + // + "\tRTN\n"); +// }}} __attribute__((noinline)) void wait(unsigned int msk) { + // {{{ PIC = 0x7fff0000|msk; asm("MOV\tidle_task(PC),uPC\n"); PIC = 0x80000000|(msk<<16); asm("WAIT\n"); PIC = 0; // Turn interrupts back off, lest they confuse the test } +// }}} asm("\n\t.text\nidle_task:\n\tWAIT\n\tBRA\tidle_task\n"); __attribute__((noinline)) void txchr(char v) { + // {{{ #ifdef _ZIP_HAS_WBUART while((_uart->u_fifo & 0x010000)==0) ; @@ -1100,8 +1438,10 @@ void txchr(char v) { _uart->u_tx = (unsigned)c; #endif } +// }}} void wait_for_uart_idle(void) { + // {{{ #ifdef _ZIP_HAS_WBUART while(_uart->u_tx & 0x100) // While the transmitter is non-idle ; @@ -1109,16 +1449,20 @@ void wait_for_uart_idle(void) { #error "No uart defined" #endif } +// }}} __attribute__((noinline)) void txstr(const char *str) { + // {{{ const char *ptr = str; while(*ptr) txchr(*ptr++); } +// }}} __attribute__((noinline)) void txhex(int num) { + // {{{ for(int ds=28; ds>=0; ds-=4) { int ch; ch = (num >> ds)&0x0f; @@ -1129,9 +1473,11 @@ void txhex(int num) { txchr(ch); } } +// }}} __attribute__((noinline)) void tx4hex(int num) { + // {{{ if (num & 0xffff0000){ txhex(num); return; @@ -1145,22 +1491,55 @@ void tx4hex(int num) { txchr(ch); } } +// }}} __attribute__((noinline)) void txreg(const char *name, int val) { + // {{{ txstr(name); // 4 characters txstr("0x"); // 2 characters txhex(val); // 8 characters txstr(" "); // 4 characters } +// }}} + +void txunsigned(unsigned uv) { + // {{{ + unsigned i, d; + char str[32]; + + if (uv == 0) + txchr('0'); + else { + for(i=0; i<31 && (d = (uv % 10)); i++) { + str[i] = '0'+d; + uv /= 10; + } for(d=0; d 0) txchr(' '); } +// }}} -int testlist[32]; +#define MAXTESTS 40 +int testlist[MAXTESTS]; void entry(void) { - int context[16]; - int user_stack[256], *user_stack_ptr = &user_stack[256]; - int start_time, i; - int cc_fail, cis_insns = 0; + int context[16]; + int user_stack[256], *user_stack_ptr = &user_stack[256]; + int start_time, i; + int cc_fail, cis_insns = 0; + unsigned cc; - for(i=0; i<32; i++) + for(i=0; i for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #ifdef GPIO_ACCESS diff --git a/sw/board/hello.c b/sw/board/hello.c index 5949b9c..61a89c3 100644 --- a/sw/board/hello.c +++ b/sw/board/hello.c @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: helloworld.c -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: The original Helllo World program. If everything works, this @@ -16,7 +16,7 @@ // //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include int main(int argc, char **argv) { diff --git a/sw/board/hellosim.s b/sw/board/hellosim.s index 70a3d8a..4bc3277 100644 --- a/sw/board/hellosim.s +++ b/sw/board/hellosim.s @@ -1,8 +1,8 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; Filename: hellosim.s -;; -;; Project: Zip CPU -- a small, lightweight, RISC CPU soft core +;; {{{ +;; Project: ZBasic, a generic toplevel impl using the full ZipCPU ;; ;; Purpose: Hello World, only using the SIM instructions, as an initial ;; test of whether or not the special SIM instructions even work. @@ -12,11 +12,11 @@ ;; Gisselquist Technology, LLC ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; -;; Copyright (C) 2017-2019, Gisselquist Technology, LLC -;; +;; }}} +;; Copyright (C) 2017-2022, Gisselquist Technology, LLC +;; {{{ ;; This program is free software (firmware): you can redistribute it and/or -;; modify it under the terms of the GNU General Public License as published +;; modify it under the terms of the GNU General Public License as published ;; by the Free Software Foundation, either version 3 of the License, or (at ;; your option) any later version. ;; @@ -25,13 +25,18 @@ ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ;; for more details. ;; +;; You should have received a copy of the GNU General Public License along +;; with this program. (It's in the $(ROOT)/doc directory. Run make with no +;; target there if the PDF file isn't present.) If not, see +;; for a copy. +;; }}} ;; License: GPL, v3, as defined and found on www.gnu.org, +;; {{{ ;; http://www.gnu.org/licenses/gpl.html ;; -;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; +;; }}} .section .start, "ax" .global _start _start: diff --git a/sw/board/hellostep.c b/sw/board/hellostep.c new file mode 100644 index 0000000..3c41c3a --- /dev/null +++ b/sw/board/hellostep.c @@ -0,0 +1,90 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: hellostep.c +// {{{ +// Project: ZBasic, a generic toplevel impl using the full ZipCPU +// +// Purpose: This is a modified version of the original hello world. As +// with the original, if all goes well this program will print +// Hello World to the UART, and then halt the CPU. Behind the scenes, +// what makes this program special is that the supervisor is stepping +// through the Hello World program, one instruction at a time. Thus, this +// program is a test of the ZipCPU's stepping capabilities. +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2021-2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +// }}} +#include +#include "zipcpu.h" +#include "txfns.h" + +void user_main(void) { + printf("Hello, World!\n"); + zip_syscall(); +} + +int main(int argc, char **argv) { + int done = 0, success = 1; + unsigned user_regs[16]; + unsigned user_stack[512]; + + for(unsigned k=0; k<16; k++) + user_regs[k] = 0; + user_regs[15] = (unsigned)user_main; + user_regs[14] = CC_STEP; + user_regs[13] = (unsigned)&user_stack[512]; + zip_restore_context(user_regs); + + while(!done) { + unsigned ucc; + + zip_rtu(); + + ucc = zip_ucc(); + if (ucc & CC_EXCEPTION) { + txstr("\r\nEXCEPTION: CC = ");txhex(ucc); txstr("\r\n"); + txstr("\r\n"); + while((_uart->u_fifo & 0x010000) == 0) + ; + done = 1; + success = 0; + } if (ucc & CC_TRAP) + done = 1; + else if ((ucc & CC_STEP) == 0) { + success = 0; + txstr("\r\nCC & STEP == 0 ?? CC = "); txhex(ucc); + txstr("\r\n"); + while((_uart->u_fifo & 0x010000) == 0) + ; + } + } + + if (success) + txstr("\r\n\r\nSUCCESS!\r\n"); +} diff --git a/sw/board/lockcheck.c b/sw/board/lockcheck.c new file mode 100644 index 0000000..ae949f3 --- /dev/null +++ b/sw/board/lockcheck.c @@ -0,0 +1,162 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Filename: lockcheck.c +// {{{ +// Project: ZBasic, a generic toplevel impl using the full ZipCPU +// +// Purpose: This is a CPU test program designed to test the ZipCPU's LOCK +// instruction. NUM_TASKS (=4) tasks are created, each trying +// to grab and set a shared resource and increment it. If all goes +// well, the resource will be owned by the task and unchanged for the +// duration that the resource is owned by the task. +// +// Creator: Dan Gisselquist, Ph.D. +// Gisselquist Technology, LLC +// +//////////////////////////////////////////////////////////////////////////////// +// }}} +// Copyright (C) 2021-2022, Gisselquist Technology, LLC +// {{{ +// This program is free software (firmware): you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} +// License: GPL, v3, as defined and found on www.gnu.org, +// {{{ +// http://www.gnu.org/licenses/gpl.html +// +//////////////////////////////////////////////////////////////////////////////// +// +// }}} +#include +#include +#include "txfns.h" +#include "zipcpu.h" +#include "board.h" + +const unsigned NUM_TASKS = 4; + +typedef struct TASKT_S { + unsigned volatile *regs; + unsigned *stackp; +} TASKT; + +volatile char atomic; +volatile unsigned shared_resource; + +void user_task(int task_id, int *loops) { + // const unsigned NLOOPS = 1024; + const unsigned NLOOPS = 12; + unsigned task_fail = 0; + *loops = 0; + while(!task_fail && *loops < NLOOPS) { + if (!__atomic_test_and_set(&atomic, __ATOMIC_RELAXED)) { + // printf("Task #%d:\n", task_id); + shared_resource = task_id; + for(int k=0; k<5; k++) { + // NSTR("K-Loop"); + // if (shared_resource != task_id) LED = RED; + if (shared_resource != task_id) + task_fail = 1; + if (atomic != 1) + task_fail = 1; + } + + // NSTR("Increment"); + *loops = *loops + 1; + printf("Task #%d: %d loops\n", task_id, *loops); + // NSTR("Release"); + atomic = 0; + } + } + + // Spin and grab the lock + while (__atomic_test_and_set(&atomic, __ATOMIC_RELAXED)) {} + + printf("Task #%d: COMPLETE%s\n", task_id, (task_fail) ? " -- FAILED!":""); + atomic = 0; + + if (task_fail) + *loops = -1; + zip_syscall(); + (*_gpio) = GPIO_SET(2); + while(1) + txchr('!'); +} + +// And a main task that steps each task +int main(int argc,char **argv) { + TASKT TASK[NUM_TASKS]; + unsigned task_loops[NUM_TASKS]; + unsigned success = 1; + + atomic = 0; + shared_resource = 0; + + // Step up tasks + for(int taskn=0; taskn < NUM_TASKS; taskn++) { + TASK[taskn].regs = malloc(sizeof(int)*16); + TASK[taskn].stackp= malloc(sizeof(int)*512) + + sizeof(int)*512; + for(int r=0; r<13; r++) + TASK[taskn].regs[r] = 0; + TASK[taskn].regs[15] = (unsigned)user_task; + TASK[taskn].regs[14] = CC_STEP; + TASK[taskn].regs[13] = (unsigned)TASK[taskn].stackp; + TASK[taskn].regs[ 1] = taskn; // The task ID + TASK[taskn].regs[ 2] = (unsigned)&task_loops[taskn]; + } + + (*_gpio) = GPIO_SET(1); + + // Then run them + while(1) { + int completed; + + completed = 0; + for(int taskn=0; taskn < NUM_TASKS; taskn++) { + if (TASK[taskn].regs[14] & (CC_TRAP | CC_EXCEPTION)) { + completed++; + } else { + zip_restore_context((void *)TASK[taskn].regs); + // zip_ucc() |= STEP;//What's the syntax here?!? + zip_rtu(); + zip_save_context((void *)TASK[taskn].regs); + if (zip_ucc() & (CC_EXCEPTION)) + success = 0; + + if (TASK[taskn].regs[14] & (CC_TRAP|CC_EXCEPTION)) { + printf("TASK[%2d] has completed\n", taskn); + } + } + } + + if (completed >= NUM_TASKS) + break; + } + + if (success) { + for(int taskn=0; taskn < NUM_TASKS; taskn++) { + printf(" LOOPS[%2d] = 0x%08x\n", taskn, task_loops[taskn]); + if (task_loops[taskn] == 0xffffffff) + success = 0; + } + + if (success) + printf("SUCCESS!\n"); + else + printf("TASK FAILURE\n"); + } else + printf("TEST FAILURE!\n"); +} diff --git a/sw/board/sdtest.c b/sw/board/sdtest.c index cdf20bd..fb82b14 100644 --- a/sw/board/sdtest.c +++ b/sw/board/sdtest.c @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: sdtest.c -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This small program tests the SD card, proving whether it does @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,14 +28,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include "board.h" diff --git a/sw/board/txfns.c b/sw/board/txfns.c index 96fa076..b136336 100644 --- a/sw/board/txfns.c +++ b/sw/board/txfns.c @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: txfns.c -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: These are some *very* simple UART routines, designed to support @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include "board.h" #include "txfns.h" diff --git a/sw/board/txfns.h b/sw/board/txfns.h index 9462d79..5371825 100644 --- a/sw/board/txfns.h +++ b/sw/board/txfns.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: txfns.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: These are some *very* simple UART routines, designed to support @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef TXFNS_H #define TXFNS_H diff --git a/sw/host/Makefile b/sw/host/Makefile index 1dd30f5..6949f53 100644 --- a/sw/host/Makefile +++ b/sw/host/Makefile @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: Makefile -## +## {{{ ## Project: ZBasic, a generic toplevel impl using the full ZipCPU ## ## Purpose: @@ -11,11 +11,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2015-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2015-2021, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -28,14 +28,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} .PHONY: all PROGRAMS := wbregs netuart zipload zipstate zipdbg SCOPES := diff --git a/sw/host/byteswap.cpp b/sw/host/byteswap.cpp index 682bc77..10ffd23 100644 --- a/sw/host/byteswap.cpp +++ b/sw/host/byteswap.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: byteswap.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: To convert between little endian and big endian byte orders, @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include "byteswap.h" diff --git a/sw/host/byteswap.h b/sw/host/byteswap.h index 0bcd651..d3c981b 100644 --- a/sw/host/byteswap.h +++ b/sw/host/byteswap.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: byteswap.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: To convert between little endian and big endian byte orders, @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef BYTESWAP_H #define BYTESWAP_H diff --git a/sw/host/devbus.h b/sw/host/devbus.h index 7c5b178..75d4a7f 100644 --- a/sw/host/devbus.h +++ b/sw/host/devbus.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: devbus.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: The purpose of this file is to document an interface which @@ -21,11 +21,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -38,14 +38,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef DEVBUS_H #define DEVBUS_H diff --git a/sw/host/dumpflash.cpp b/sw/host/dumpflash.cpp index 22c924e..1769c45 100644 --- a/sw/host/dumpflash.cpp +++ b/sw/host/dumpflash.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: dumpflash.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Read/Empty the entire contents of the flash memory to a file. @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,14 +28,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include diff --git a/sw/host/flashdrvr.cpp b/sw/host/flashdrvr.cpp index e7ba362..691b20b 100644 --- a/sw/host/flashdrvr.cpp +++ b/sw/host/flashdrvr.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: flashdrvr.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Flash driver. Encapsulates the erasing and programming (i.e. @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,14 +28,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include diff --git a/sw/host/flashdrvr.h b/sw/host/flashdrvr.h index 346f6b8..280785e 100644 --- a/sw/host/flashdrvr.h +++ b/sw/host/flashdrvr.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: flashdrvr.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Flash driver. Encapsulates writing, both erasing sectors and @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,15 +28,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// -// +// }}} #ifndef FLASHDRVR_H #define FLASHDRVR_H diff --git a/sw/host/llcomms.cpp b/sw/host/llcomms.cpp index 7dbbba4..e6d393c 100644 --- a/sw/host/llcomms.cpp +++ b/sw/host/llcomms.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: llcomms.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This is the C++ program on the command side that will interact @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include diff --git a/sw/host/llcomms.h b/sw/host/llcomms.h index e01590f..8bfb4b0 100644 --- a/sw/host/llcomms.h +++ b/sw/host/llcomms.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: llcomms.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This is the C++ program on the command side that will interact @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef LLCOMMS_H #define LLCOMMS_H diff --git a/sw/host/netuart.cpp b/sw/host/netuart.cpp index b8e0f15..5b6b689 100644 --- a/sw/host/netuart.cpp +++ b/sw/host/netuart.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: netuart.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: @@ -10,11 +10,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -27,15 +27,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// - +// }}} #include #include #include diff --git a/sw/host/port.h b/sw/host/port.h index e5442d6..8db5e58 100644 --- a/sw/host/port.h +++ b/sw/host/port.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: port.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Defines the communication parameters necessary for communicating @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef PORT_H #define PORT_H @@ -47,7 +47,7 @@ // computer, test it, and when/if it doesn't work we can replace the device // with the test-bench. Across the network, no one will know any better that // anything had changed. -#define FPGAHOST "localhost" // A random hostname,back from the grave +#define FPGAHOST "localhost" #define FPGAPORT 8845 #define FPGAOPEN(V) V= new FPGA(new NETCOMMS(FPGAHOST, FPGAPORT)) diff --git a/sw/host/regdefs.cpp b/sw/host/regdefs.cpp index 517b702..0a16ecf 100644 --- a/sw/host/regdefs.cpp +++ b/sw/host/regdefs.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: ./regdefs.cpp -// +// {{{ // Project: ZBasic, a generic toplevel implementation using the full ZipCPU // // DO NOT EDIT THIS FILE! @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include @@ -76,7 +76,55 @@ const REGNAME raw_bregs[] = { { R_RTCDATE , "DATE" }, { R_VERSION , "VERSION" }, { R_BKRAM , "RAM" }, - { R_FLASH , "FLASH" } + { R_FLASH , "FLASH" }, + { R_ZIPCTRL , "CPU" }, + { R_ZIPCTRL , "ZIPCTRL" }, + { R_ZIPREGS , "ZIPREGS" }, + { R_ZIPS0 , "SR0" }, + { R_ZIPS1 , "SR1" }, + { R_ZIPS2 , "SR2" }, + { R_ZIPS3 , "SR3" }, + { R_ZIPS4 , "SR4" }, + { R_ZIPS5 , "SR5" }, + { R_ZIPS6 , "SR6" }, + { R_ZIPS7 , "SR7" }, + { R_ZIPS8 , "SR8" }, + { R_ZIPS9 , "SR9" }, + { R_ZIPS10 , "SR10" }, + { R_ZIPS11 , "SR11" }, + { R_ZIPS12 , "SR12" }, + { R_ZIPSSP , "SSP" }, + { R_ZIPSSP , "SR13" }, + { R_ZIPCC , "ZIPCC" }, + { R_ZIPCC , "CC" }, + { R_ZIPCC , "SCC" }, + { R_ZIPCC , "SR14" }, + { R_ZIPPC , "ZIPPC" }, + { R_ZIPPC , "PC" }, + { R_ZIPPC , "SPC" }, + { R_ZIPPC , "SR15" }, + { R_ZIPUSER , "ZIPUSER" }, + { R_ZIPU0 , "UR0" }, + { R_ZIPU1 , "UR1" }, + { R_ZIPU2 , "UR2" }, + { R_ZIPU3 , "UR3" }, + { R_ZIPU4 , "UR4" }, + { R_ZIPU5 , "UR5" }, + { R_ZIPU6 , "UR6" }, + { R_ZIPU7 , "UR7" }, + { R_ZIPU8 , "UR8" }, + { R_ZIPU9 , "UR9" }, + { R_ZIPU10 , "SR10" }, + { R_ZIPU11 , "SR11" }, + { R_ZIPU12 , "SR12" }, + { R_ZIPUSP , "USP" }, + { R_ZIPUSP , "UR13" }, + { R_ZIPUCC , "ZIPUCC" }, + { R_ZIPUCC , "UCC" }, + { R_ZIPUPC , "ZIPUPC" }, + { R_ZIPUPC , "UPC" }, + { R_ZIPSYSTEM , "ZIPSYSTEM" }, + { R_ZIPSYSTEM , "ZIPSYS" } }; // REGSDEFS.CPP.INSERT for any bus masters @@ -92,14 +140,6 @@ unsigned addrdecode(const char *v) { for(int i=0; i for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef REGDEFS_H #define REGDEFS_H @@ -81,14 +81,49 @@ #define R_VERSION 0x00a00018 // 00a00018, wbregs names: VERSION #define R_BKRAM 0x00c00000 // 00c00000, wbregs names: RAM #define R_FLASH 0x01000000 // 01000000, wbregs names: FLASH +// ZipCPU control/debug registers +#define R_ZIPCTRL 0x02000000 // 02000000, wbregs names: CPU, ZIPCTRL +#define R_ZIPREGS 0x02000080 // 02000000, wbregs names: ZIPREGS +#define R_ZIPS0 0x02000080 // 02000000, wbregs names: SR0 +#define R_ZIPS1 0x02000084 // 02000000, wbregs names: SR1 +#define R_ZIPS2 0x02000088 // 02000000, wbregs names: SR2 +#define R_ZIPS3 0x0200008c // 02000000, wbregs names: SR3 +#define R_ZIPS4 0x02000090 // 02000000, wbregs names: SR4 +#define R_ZIPS5 0x02000094 // 02000000, wbregs names: SR5 +#define R_ZIPS6 0x02000098 // 02000000, wbregs names: SR6 +#define R_ZIPS7 0x0200009c // 02000000, wbregs names: SR7 +#define R_ZIPS8 0x020000a0 // 02000000, wbregs names: SR8 +#define R_ZIPS9 0x020000a4 // 02000000, wbregs names: SR9 +#define R_ZIPS10 0x020000a8 // 02000000, wbregs names: SR10 +#define R_ZIPS11 0x020000ac // 02000000, wbregs names: SR11 +#define R_ZIPS12 0x020000b0 // 02000000, wbregs names: SR12 +#define R_ZIPSSP 0x020000b4 // 02000000, wbregs names: SSP, SR13 +#define R_ZIPCC 0x020000b8 // 02000000, wbregs names: ZIPCC, CC, SCC, SR14 +#define R_ZIPPC 0x020000bc // 02000000, wbregs names: ZIPPC, PC, SPC, SR15 +#define R_ZIPUSER 0x020000c0 // 02000000, wbregs names: ZIPUSER +#define R_ZIPU0 0x020000c0 // 02000000, wbregs names: UR0 +#define R_ZIPU1 0x020000c4 // 02000000, wbregs names: UR1 +#define R_ZIPU2 0x020000c8 // 02000000, wbregs names: UR2 +#define R_ZIPU3 0x020000cc // 02000000, wbregs names: UR3 +#define R_ZIPU4 0x020000d0 // 02000000, wbregs names: UR4 +#define R_ZIPU5 0x020000d4 // 02000000, wbregs names: UR5 +#define R_ZIPU6 0x020000d8 // 02000000, wbregs names: UR6 +#define R_ZIPU7 0x020000dc // 02000000, wbregs names: UR7 +#define R_ZIPU8 0x020000e0 // 02000000, wbregs names: UR8 +#define R_ZIPU9 0x020000e4 // 02000000, wbregs names: UR9 +#define R_ZIPU10 0x020000e8 // 02000000, wbregs names: SR10 +#define R_ZIPU11 0x020000ec // 02000000, wbregs names: SR11 +#define R_ZIPU12 0x020000f0 // 02000000, wbregs names: SR12 +#define R_ZIPUSP 0x020000f4 // 02000000, wbregs names: USP, UR13 +#define R_ZIPUCC 0x020000f8 // 02000000, wbregs names: ZIPUCC, UCC +#define R_ZIPUPC 0x020000fc // 02000000, wbregs names: ZIPUPC, UPC +#define R_ZIPSYSTEM 0x02000100 // 02000000, wbregs names: ZIPSYSTEM, ZIPSYS // // The @REGDEFS.H.DEFNS tag // // @REGDEFS.H.DEFNS for masters -#define R_ZIPCTRL 0x02000000 -#define R_ZIPDATA 0x02000004 #define BAUDRATE 1000000 #define CLKFREQHZ 100000000 // @REGDEFS.H.DEFNS for peripherals @@ -108,6 +143,35 @@ // // @REGDEFS.H.INSERT for masters // @REGDEFS.H.INSERT for peripherals +//////////////////////////////////////////////////////////////////////////////// +// +// ZipCPU register definitions +// {{{ + +#define CPU_GO 0x0000 +#define CPU_HALT 0x0001 +#define CPU_STALL 0x0002 +#define CPU_STEP 0x0004 +#define CPU_RESET 0x0008 +#define CPU_CLRCACHE 0x0010 +// (Reserved) 0x00e0 +#define CPU_SLEEPING 0x0100 +#define CPU_GIE 0x0200 +#define CPU_INT 0x0400 +#define CPU_BREAK 0x0800 +#define CPU_EXINT 0xfffff000 +// +#define CPU_sR0 0x0000 +#define CPU_sSP 0x000d +#define CPU_sCC 0x000e +#define CPU_sPC 0x000f +#define CPU_uR0 0x0010 +#define CPU_uSP 0x001d +#define CPU_uCC 0x001e +#define CPU_uPC 0x001f + +#define RESET_ADDRESS 0x01400000 +// }}} // Flash control constants #define QSPI_FLASH // This core and hardware support a Quad SPI flash #define SZPAGEB 256 @@ -122,26 +186,6 @@ #define SUBSECTOROF(A) ((A) & (-1<<12)) #define PAGEOF(A) ((A) & (-1<<8)) - -#define CPU_GO 0x0000 -#define CPU_RESET 0x0040 -#define CPU_INT 0x0080 -#define CPU_STEP 0x0100 -#define CPU_STALL 0x0200 -#define CPU_HALT 0x0400 -#define CPU_CLRCACHE 0x0800 -#define CPU_sR0 0x0000 -#define CPU_sSP 0x000d -#define CPU_sCC 0x000e -#define CPU_sPC 0x000f -#define CPU_uR0 0x0010 -#define CPU_uSP 0x001d -#define CPU_uCC 0x001e -#define CPU_uPC 0x001f - -#define RESET_ADDRESS 0x01400000 - - // @REGDEFS.H.INSERT from the top level typedef struct { unsigned m_addr; diff --git a/sw/host/ttybus.cpp b/sw/host/ttybus.cpp index e1fcb3d..efd5808 100644 --- a/sw/host/ttybus.cpp +++ b/sw/host/ttybus.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: ttybus.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This is the C++ program on the command side that will interact @@ -15,11 +15,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -32,14 +32,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include diff --git a/sw/host/ttybus.h b/sw/host/ttybus.h index 5e7b349..ae09619 100644 --- a/sw/host/ttybus.h +++ b/sw/host/ttybus.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: ttybus.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This is the C++ program on the command side that will interact @@ -21,11 +21,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -38,15 +38,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// -// +// }}} #ifndef TTYBUS_H #define TTYBUS_H diff --git a/sw/host/twoc.cpp b/sw/host/twoc.cpp index 0676f1d..4c406a6 100644 --- a/sw/host/twoc.cpp +++ b/sw/host/twoc.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: twoc.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Some various two's complement related C++ helper routines. @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include "twoc.h" long sbits(const long val, const int bits) { diff --git a/sw/host/twoc.h b/sw/host/twoc.h index a04953c..7c835f1 100644 --- a/sw/host/twoc.h +++ b/sw/host/twoc.h @@ -1,23 +1,23 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: twoc.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Some various two's complement related C++ helper routines. // Specifically, these help extract signed numbers from -// packed bitfields, while guaranteeing that the upper bits -// are properly sign extended (or not) as desired. +// packed bitfields, while guaranteeing that the upper bits are properly +// sign extended (or not) as desired. // // Creator: Dan Gisselquist, Ph.D. // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef TWOC_H #define TWOC_H diff --git a/sw/host/wbregs.cpp b/sw/host/wbregs.cpp index 9d925ee..cfe02f0 100644 --- a/sw/host/wbregs.cpp +++ b/sw/host/wbregs.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: wbregs.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: To give a user access, via a command line program, to read @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include diff --git a/sw/host/zipdbg.cpp b/sw/host/zipdbg.cpp index be04376..c0ee356 100644 --- a/sw/host/zipdbg.cpp +++ b/sw/host/zipdbg.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zipdbg.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Provide a debugger to step through the ZipCPU assembler, @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,17 +30,17 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} // BUGS: // - No ability to verify CPU functionality (3rd party simulator) -// - No ability to set/clear breakpoints +// - No ability to set/clear breakpoints (yet) // // #include @@ -101,24 +101,30 @@ class ZIPPY : public DEVBUS { DEVBUS *m_fpga; int m_cursor; ZIPSTATE m_state; - bool m_user_break, m_show_users_timers, m_show_cc; + bool m_show_users_timers, m_show_cc, m_stopped_at_break; public: - ZIPPY(DEVBUS *fpga) : m_fpga(fpga), m_cursor(0), m_user_break(false), - m_show_users_timers(false), m_show_cc(false) {} + ZIPPY(DEVBUS *fpga) : m_fpga(fpga), m_cursor(0), + m_show_users_timers(false), + m_show_cc(false), + m_stopped_at_break(false) {} void read_raw_state(void) { + // {{{ m_state.m_valid = false; - for(int i=0; i<16; i++) - m_state.m_sR[i] = cmd_read(i); - for(int i=0; i<16; i++) - m_state.m_uR[i] = cmd_read(i+16); - for(int i=0; i<20; i++) - m_state.m_p[i] = cmd_read(i+32); + + // Read registers and peripherals + // {{{ + readi(R_ZIPREGS, 16, m_state.m_sR); + readi(R_ZIPUSER, 16, m_state.m_uR); + readi(R_ZIPSYSTEM, 20, m_state.m_p); m_state.m_gie = (m_state.m_sR[14] & 0x020); m_state.m_pc = (m_state.m_gie) ? (m_state.m_uR[15]):(m_state.m_sR[15]); m_state.m_sp = (m_state.m_gie) ? (m_state.m_uR[13]):(m_state.m_sR[13]); + // }}} + // Read the instructions in memory + // {{{ if (m_state.m_last_pc_valid) m_state.m_imem[0].m_a = m_state.m_last_pc; else @@ -126,8 +132,11 @@ class ZIPPY : public DEVBUS { try { m_state.m_imem[0].m_d = readio(m_state.m_imem[0].m_a); m_state.m_imem[0].m_valid = true; + + m_stopped_at_break = ((m_state.m_imem[0].m_d & 0xf7ffffff) == 0x77000000); } catch(BUSERR be) { m_state.m_imem[0].m_valid = false; + m_stopped_at_break = false; } m_state.m_imem[1].m_a = m_state.m_pc; try { @@ -153,22 +162,43 @@ class ZIPPY : public DEVBUS { m_state.m_imem[i+1].m_valid = false; } } + // }}} + // Read the stack + // {{{ m_state.m_smem[0].m_a = m_state.m_sp; - for(int i=1; i<5; i++) - m_state.m_smem[i].m_a = m_state.m_smem[i-1].m_a+4; - for(int i=0; i<5; i++) { - m_state.m_smem[i].m_valid = true; - if (m_state.m_smem[i].m_valid) - try { - m_state.m_smem[i].m_d = readio(m_state.m_smem[i].m_a); + + try { // Try a fast read. Works if no bus errors + // {{{ + unsigned int buffer[5]; + readi(m_state.m_sp, 5, buffer); + for(int i=0; i<5; i++) { m_state.m_smem[i].m_valid = true; - } catch(BUSERR be) { - m_state.m_smem[i].m_valid = false; + m_state.m_smem[i].m_a = m_state.m_sp + 4*i; + m_state.m_smem[i].m_d = buffer[i]; + } + // }}} + } catch(BUSERR be) { + // {{{ + // Got a bus error. Try reading each value + // from the stack individually. + for(int i=0; i<5; i++) { + m_state.m_smem[i].m_valid = true; + if (m_state.m_smem[i].m_valid) + try { + m_state.m_smem[i].m_d = readio(m_state.m_smem[i].m_a); + m_state.m_smem[i].m_valid = true; + } catch(BUSERR be) { + m_state.m_smem[i].m_valid = false; + } } + // }}} } + // }}} + m_state.m_valid = true; } + // }}} void kill(void) { m_fpga->kill(); } void close(void) { m_fpga->close(); } @@ -190,35 +220,62 @@ class ZIPPY : public DEVBUS { void clear(void) { m_fpga->clear(); } void reset(void) { writeio(R_ZIPCTRL, CPU_RESET|CPU_HALT); } - void step(void) { writeio(R_ZIPCTRL, CPU_STEP); m_state.step(); } + void step(void) { + // {{{ + if (m_stopped_at_break) { + unsigned break_insn, insn_addr; + const unsigned noop_insn = 0x77c00000; + + insn_addr = m_state.m_imem[0].m_a; + break_insn = m_state.m_imem[0].m_d; + + writeio(insn_addr, noop_insn); + writeio(R_ZIPCTRL, CPU_CLRCACHE); + writeio(R_ZIPCTRL, CPU_STEP); m_state.step(); + writeio(insn_addr, break_insn); + writeio(R_ZIPCTRL, CPU_CLRCACHE); + } else { + writeio(R_ZIPCTRL, CPU_STEP); m_state.step(); + } + // }}} + } void go(void) { writeio(R_ZIPCTRL, CPU_GO); } void halt(void) { writeio(R_ZIPCTRL, CPU_HALT); } bool stalled(void) { return ((readio(R_ZIPCTRL)&CPU_STALL)==0); } void show_user_timers(bool v) { + // {{{ m_show_users_timers = v; } + // }}} void toggle_cc(void) { + // {{{ m_show_cc = !m_show_cc; } + // }}} void showval(int y, int x, const char *lbl, unsigned int v, bool c) { + // {{{ if (c) mvprintw(y,x, ">%s> 0x%08x<", lbl, v); else mvprintw(y,x, " %s: 0x%08x ", lbl, v); } + // }}} void dispreg(int y, int x, const char *n, unsigned int v, bool c) { + // {{{ // 4,4,8,1 = 17 of 20, +2 = 18 if (c) mvprintw(y, x, ">%s> 0x%08x<", n, v); else mvprintw(y, x, " %s: 0x%08x ", n, v); } + // }}} int showins(int y, const char *lbl, const unsigned int pcidx, bool ignore_a) { + // {{{ char la[80], lb[80]; int r = y-1, bln = r; @@ -249,8 +306,10 @@ class ZIPPY : public DEVBUS { return r; } + // }}} void showstack(int y, const char *lbl, const unsigned int idx) { + // {{{ mvprintw(y, 27+26, "%s%08x ", lbl, m_state.m_smem[idx].m_a); if (m_state.m_gie) attroff(A_BOLD); @@ -262,65 +321,26 @@ class ZIPPY : public DEVBUS { printw("(Bus Err)"); attroff(A_BOLD); } + // }}} + // Routine is not used + /* unsigned int cmd_read(unsigned int a) { - int errcount = 0; - unsigned int s; - - writeio(R_ZIPCTRL, CMD_HALT|(a&0x3f)); - while((((s=readio(R_ZIPCTRL))&CPU_STALL)== 0)&&(errcount= MAXERR) { - endwin(); - printf("ERR: errcount(%d) >= MAXERR on cmd_read(a=%2x)\n", errcount, a); - printf("ZIPCTRL = 0x%08x", s); - if ((s & 0x0200)==0) printf(" STALL"); - if (s & 0x0400) printf(" HALTED"); - if ((s & 0x03000)==0x01000) - printf(" SW-HALT"); - else { - if (s & 0x01000) printf(" SLEEPING"); - if (s & 0x02000) printf(" GIE(UsrMode)"); - } printf("\n"); - exit(EXIT_FAILURE); - } - return readio(R_ZIPDATA); + // {{{ + return readio(R_ZIPREGS + (4*a)); } + // }}} + */ + // Writes to the CPU are simpler with our newer cmd_write interface void cmd_write(unsigned int a, int v) { - int errcount = 0; - unsigned int s; - - writeio(R_ZIPCTRL, CMD_HALT|(a&0x3f)); - while((((s=readio(R_ZIPCTRL))&CPU_STALL)== 0)&&(errcount= MAXERR) { - endwin(); - printf("ERR: errcount(%d) >= MAXERR on cmd_read(a=%2x)\n", errcount, a); - printf("ZIPCTRL = 0x%08x", s); - if ((s & 0x0200)==0) printf(" STALL"); - if (s & 0x0400) printf(" HALTED"); - if ((s & 0x03000)==0x01000) - printf(" SW-HALT"); - else { - if (s & 0x01000) printf(" SLEEPING"); - if (s & 0x02000) printf(" GIE(UsrMode)"); - } printf("\n"); - exit(EXIT_FAILURE); - } - - writeio(R_ZIPDATA, (unsigned int)v); + // {{{ + writeio(R_ZIPREGS+(4*a), (unsigned int)v); } + // }}} void read_state(void) { + // {{{ int ln= 0; bool gie; @@ -331,6 +351,8 @@ class ZIPPY : public DEVBUS { else if (m_cursor >= 44) m_cursor = 43; + // Peripheral state + // {{{ mvprintw(ln,0, "Peripherals"); mvprintw(ln,30,"%-50s", "CPU State: "); { @@ -377,7 +399,10 @@ class ZIPPY : public DEVBUS { showval(ln,40, "UPST", m_state.m_p[14], (m_cursor==10)); showval(ln,60, "UICT", m_state.m_p[15], (m_cursor==11)); } + // }}} + // Draw supervisor registers + // {{{ ln++; ln++; unsigned int cc = m_state.m_sR[14]; @@ -429,7 +454,10 @@ class ZIPPY : public DEVBUS { } dispreg(ln,60, "sPC ", m_state.m_sR[15], (m_cursor==27)); ln++; + // }}} + // Draw user registers + // {{{ if (gie) attron(A_BOLD); else @@ -474,10 +502,13 @@ class ZIPPY : public DEVBUS { (cc&1)?"Z":" "); } dispreg(ln,60, "uPC ", m_state.m_uR[15], (m_cursor==43)); + // }}} attroff(A_BOLD); ln+=3; + // Show instruction pipe and top of stack + // {{{ showins(ln+4, " ", 0, true); { int lclln = ln+3; @@ -486,7 +517,9 @@ class ZIPPY : public DEVBUS { for(int i=0; i<5; i++) showstack(ln+i, (i==0)?">":" ", i); } + // }}} } + // }}} void cursor_up(void) { if (m_cursor > 3) @@ -512,21 +545,27 @@ const int ZIPPY::MAXERR = 100000; FPGA *m_fpga; void get_value(ZIPPY *zip) { + // {{{ int wy, wx, ra; int c = zip->cursor(); - wx = (c & 0x03) * 20 + 9 + 1; + wx = (c & 0x03) * 20 + 9; wy = (c >> 2); if (wy >= 3+4) wy++; - if (wy > 3) + if (wy >= 3) wy += 2; wy++; + // Get the register address from the cursor position + // {{{ if (c >= 12) + // ZipCPU registers have an offset of 12 from the cursor + // address ra = c - 12; else ra = c + 32; + // }}} bool done = false; char str[16]; @@ -534,8 +573,11 @@ void get_value(ZIPPY *zip) { attron(A_NORMAL | A_UNDERLINE); mvprintw(wy, wx, "%-8s", ""); while(!done) { + // Get a keypress int chv = getch(); - switch(chv) { + + switch(chv) { // Process the keypress + // {{{ case KEY_ESCAPE: pos = 0; str[pos] = '\0'; done = true; break; @@ -564,39 +606,53 @@ void get_value(ZIPPY *zip) { case 'D': case 'd': str[pos++] = 'D'; break; case 'E': case 'e': str[pos++] = 'E'; break; case 'F': case 'f': str[pos++] = 'F'; break; + // }}} } + // Update the value on the screen + // {{{ if (pos > 8) pos = 8; str[pos] = '\0'; attron(A_NORMAL | A_UNDERLINE); - mvprintw(wy, wx, "%-8s", str); + mvprintw(wy, wx, "%8s", str); + /* if (pos > 0) { attron(A_NORMAL | A_UNDERLINE | A_BLINK); - mvprintw(wy, wx+pos-1, "%c", str[pos-1]); + mvprintw(wy, wx, "%s", str); } + */ attrset(A_NORMAL); + // }}} } + // Update a register by writing a new value to it + // {{{ if (pos > 0) { int v; v = strtoul(str, NULL, 16); zip->cmd_write(ra, v); } + // }}} } +// }}} void on_sigint(int v) { + // {{{ endwin(); fprintf(stderr, "Interrupted!\n"); exit(-2); } +// }}} void stall_screen(void) { + // {{{ erase(); mvprintw(0,0, "CPU is stalled. (Q to quit)\n"); } +// }}} char gbl_errstr[8192]; void eprintf(const char *fmt, ...) { @@ -634,9 +690,11 @@ int main(int argc, char **argv) { zip->read_state(); else stall_screen(); + while((!done)&&(!gbl_err)) { chv = getch(); - switch(chv) { + switch(chv) { // Process a received character + // {{{ case 'c': case 'C': zip->toggle_cc(); break; @@ -686,6 +744,7 @@ int main(int argc, char **argv) { default: ; } + // }}} if ((done)||(gbl_err)) break; diff --git a/sw/host/zipelf.cpp b/sw/host/zipelf.cpp index e693b74..a899d36 100644 --- a/sw/host/zipelf.cpp +++ b/sw/host/zipelf.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zipelf.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,14 +28,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include diff --git a/sw/host/zipelf.h b/sw/host/zipelf.h index c4f15ba..af18012 100644 --- a/sw/host/zipelf.h +++ b/sw/host/zipelf.h @@ -1,21 +1,23 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zipelf.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // -// Purpose: +// Purpose: The ZIPELF class is designed to simplify the representation +// of the fields in an ELF file, for the purpose of loading a +// computer from the sections within it. // // // Creator: Dan Gisselquist, Ph.D. // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef ZIPELF_H #define ZIPELF_H diff --git a/sw/host/zipload.cpp b/sw/host/zipload.cpp index 071c91e..f019771 100644 --- a/sw/host/zipload.cpp +++ b/sw/host/zipload.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zipload.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: To load a program for the ZipCPU into memory, whether flash @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,14 +30,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include @@ -336,23 +336,25 @@ int main(int argc, char **argv) { // Now ... how shall we start this CPU? printf("Clearing the CPUs registers\n"); - for(int i=0; i<32; i++) { - m_fpga->writeio(R_ZIPCTRL, CPU_HALT|i); - m_fpga->writeio(R_ZIPDATA, 0); + { + unsigned r[32]; + m_fpga->writeio(R_ZIPCTRL, CPU_HALT); + for(int i=0; i<32; i++) + r[i] = 0; + m_fpga->writei(R_ZIPREGS, 32, r); } m_fpga->writeio(R_ZIPCTRL, CPU_HALT|CPU_CLRCACHE); printf("Setting PC to %08x\n", entry); - m_fpga->writeio(R_ZIPCTRL, CPU_HALT|CPU_sPC); - m_fpga->writeio(R_ZIPDATA, entry); + m_fpga->writeio(R_ZIPPC, entry); if (start_when_finished) { printf("Starting the CPU\n"); - m_fpga->writeio(R_ZIPCTRL, CPU_GO|CPU_sPC); + m_fpga->writeio(R_ZIPCTRL, CPU_GO); } else { printf("The CPU should be fully loaded, you may now\n"); printf("start it (from reset/reboot) with:\n"); - printf("> wbregs cpu 0x0f\n"); + printf("> wbregs cpu 0\n"); printf("\n"); } } catch(BUSERR a) { diff --git a/sw/host/zipstate.cpp b/sw/host/zipstate.cpp index 555acff..e8d9b6e 100644 --- a/sw/host/zipstate.cpp +++ b/sw/host/zipstate.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zipstate.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: To get a quick (understandable) peek at what the ZipCPU @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include @@ -59,28 +59,18 @@ void closeup(int v) { exit(0); } +/* unsigned int cmd_read(FPGA *fpga, int r) { const unsigned int MAXERR = 1000; unsigned int errcount = 0; unsigned int s; - fpga->writeio(R_ZIPCTRL, CPU_HALT|(r&0x03f)); - while((((s = fpga->readio(R_ZIPCTRL))&CPU_STALL)== 0)&&(errcount= MAXERR) { - printf("ERR: errcount(%d) >= MAXERR on cmd_read(a=%02x)\n", - errcount, r); - printf("ZIPCTRL = 0x%08x", s); - if ((s & 0x0200)==0) printf(" BUSY"); - if (s & 0x0400) printf(" HALTED"); - if ((s & 0x03000)==0x01000) - printf(" SW-HALT"); - else { - if (s & 0x01000) printf(" SLEEPING"); - if (s & 0x02000) printf(" GIE(UsrMode)"); - } printf("\n"); - exit(EXIT_FAILURE); - } return fpga->readio(R_ZIPDATA); + return fpga->readio(R_ZIPCTRL, CPU_REGBASE + (r*4)); +} +*/ + +void read_regs(FPGA *fpga, unsigned *r) { + fpga->readi(R_ZIPREGS, 32, r); } void usage(void) { @@ -108,34 +98,48 @@ int main(int argc, char **argv) { v = m_fpga->readio(R_ZIPCTRL); printf("0x%08x: ", v); + if (v & 0x0040) printf("RESET "); if (v & 0x0080) printf("PINT "); // if (v & 0x0100) printf("STEP "); // self resetting - if((v & 0x00200)==0) printf("BUSY "); - if (v & 0x00400) printf("HALTED "); + // if (v & 0x00200) printf("DBG-STALL "); + if (v & 0x00400) { + if (v & 0x0200) + printf("DBG-HALTED "); + else + printf("HALT_REQ "); + } + // if (v & 0x0800) printf("CLR-CACHE "); if((v & 0x03000)==0x01000) { - printf("SW-HALT"); + printf("SW-HALTED"); } else { if (v & 0x01000) printf("SLEEPING "); if (v & 0x02000) printf("GIE(UsrMode) "); } - // if (v & 0x0800) printf("CLR-CACHE "); + if (v & 0x04000) printf("sBusErr "); + if (v & 0x08000) printf("BREAK-HALT"); + + if ((v & 0x0fc00) == 0) + printf("Running"); printf("\n"); } else { - printf("Reading the long-state ...\n"); + unsigned r[32]; + + printf("Reading the long-state ...\n"); fflush(stdout); + read_regs(m_fpga, r); for(int i=0; i<14; i++) { - printf("sR%-2d: 0x%08x ", i, cmd_read(m_fpga, i)); + printf("sR%-2d: 0x%08x ", i, r[i]); if ((i&3)==3) printf("\n"); - } printf("sCC : 0x%08x ", cmd_read(m_fpga, 14)); - printf("sPC : 0x%08x ", cmd_read(m_fpga, 15)); + } printf("sCC : 0x%08x ", r[14]); + printf("sPC : 0x%08x ", r[15]); printf("\n\n"); for(int i=0; i<14; i++) { - printf("uR%-2d: 0x%08x ", i, cmd_read(m_fpga, i+16)); + printf("uR%-2d: 0x%08x ", i, r[i+16]); if ((i&3)==3) printf("\n"); - } printf("uCC : 0x%08x ", cmd_read(m_fpga, 14+16)); - printf("uPC : 0x%08x ", cmd_read(m_fpga, 15+16)); + } printf("uCC : 0x%08x ", r[14]); + printf("uPC : 0x%08x ", r[15]); printf("\n\n"); } diff --git a/sw/host/zopcodes.cpp b/sw/host/zopcodes.cpp index 9364d43..c0552a6 100644 --- a/sw/host/zopcodes.cpp +++ b/sw/host/zopcodes.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zopcodes.cpp -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: A simple program to handle the disassembly and definition @@ -15,9 +15,9 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or // modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at @@ -32,14 +32,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include @@ -296,9 +296,9 @@ static const ZOPCODE zip_oplist_raw[] = { // 1.rrrr.100.1.rrrrsss { "LW", 0x87800000, 0x84000000, ZIP_REGFIELD(27), ZIP_OPUNUSED, ZIP_SP, ZIP_IMMFIELD(7,16), ZIP_OPUNUSED }, { "LW", 0x87800000, 0x84800000, ZIP_REGFIELD(27), ZIP_OPUNUSED, ZIP_REGFIELD(19), ZIP_IMMFIELD(3,16), ZIP_OPUNUSED }, - // 1.rrrr.101.ssssssss - { "SW", 0x87000000, 0x85000000, ZIP_OPUNUSED, ZIP_REGFIELD(27), ZIP_SP, ZIP_IMMFIELD(7,16), ZIP_OPUNUSED }, - // 1.rrrr.110.0.sssssss + // 1.rrrr.101.0sssssss + { "SW", 0x87800000, 0x85000000, ZIP_OPUNUSED, ZIP_REGFIELD(27), ZIP_SP, ZIP_IMMFIELD(7,16), ZIP_OPUNUSED }, + // 1.rrrr.110.1.sssssss { "SW", 0x87800000, 0x85800000, ZIP_OPUNUSED, ZIP_REGFIELD(27), ZIP_REGFIELD(19), ZIP_IMMFIELD(3,16), ZIP_OPUNUSED }, // 1.rrrr.110.iiiiiiii { "LDI", 0x87000000, 0x86000000, ZIP_REGFIELD(27), ZIP_OPUNUSED, ZIP_OPUNUSED, ZIP_IMMFIELD(8,16), ZIP_OPUNUSED }, @@ -532,13 +532,14 @@ zipi_to_halfstring(const uint32_t addr, const ZIPI ins, char *line, const ZOPCOD int dv = zip_getbits(ins, ZIP_REGFIELD(27)); int iv = zip_sbits(ins, 13); uint32_t ref; + char opcode[128]; ref = (iv<<2) + addr + 4; - sprintf(line, "%s%s", "MOV", zip_ccstr[cv]); - sprintf(line, "%-11s", line); - sprintf(line, "%s0x%08x", line, ref); - sprintf(line, "%s,%s", line, zip_regstr[dv]); + sprintf(opcode, "%s%s", "MOV", zip_ccstr[cv]); + sprintf(line, "%-11s", opcode); + sprintf(opcode, "%s0x%08x", line, ref); + sprintf(line, "%s,%s", opcode, zip_regstr[dv]); return; } else if (TWOWORD_CIS_JSR(ins)) { @@ -569,11 +570,12 @@ zipi_to_halfstring(const uint32_t addr, const ZIPI ins, char *line, const ZOPCOD // zip_oplist[i].s_mask, zip_oplist[i].s_val); if ((ins & listp[i].s_mask) == listp[i].s_val) { // Write the opcode onto our line - sprintf(line, "%s", listp[i].s_opstr); + char opcode[128]; + sprintf(opcode, "%s", listp[i].s_opstr); if (listp[i].s_cf != ZIP_OPUNUSED) { int bv = zip_getbits(ins, listp[i].s_cf); - strcat(line, zip_ccstr[bv]); - } sprintf(line, "%-11s", line); // Pad it to 11 chars + strcat(opcode, zip_ccstr[bv]); + } sprintf(line, "%-11s", opcode); // Pad it to 11 chars int ra = -1, rb = -1, rr = -1, imv = 0; diff --git a/sw/host/zopcodes.h b/sw/host/zopcodes.h index 386ddb0..aef5ae9 100644 --- a/sw/host/zopcodes.h +++ b/sw/host/zopcodes.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zopcodes.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: A structure defined to keep track of our various opcodes and @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,14 +28,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef ZOPCODES_H #define ZOPCODES_H diff --git a/sw/zlib/Makefile b/sw/zlib/Makefile index c30d10c..babea1a 100644 --- a/sw/zlib/Makefile +++ b/sw/zlib/Makefile @@ -1,7 +1,7 @@ ################################################################################ ## ## Filename: Makefile -## +## {{{ ## Project: ZBasic, a generic toplevel impl using the full ZipCPU ## ## Purpose: To guide/direct the build of the board-specific portion of @@ -13,11 +13,11 @@ ## Gisselquist Technology, LLC ## ################################################################################ -## -## Copyright (C) 2017-2020, Gisselquist Technology, LLC -## +## }}} +## Copyright (C) 2017-2022, Gisselquist Technology, LLC +## {{{ ## This program is free software (firmware): you can redistribute it and/or -## modify it under the terms of the GNU General Public License as published +## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## @@ -30,14 +30,14 @@ ## with this program. (It's in the $(ROOT)/doc directory. Run make with no ## target there if the PDF file isn't present.) If not, see ## for a copy. -## +## }}} ## License: GPL, v3, as defined and found on www.gnu.org, +## {{{ ## http://www.gnu.org/licenses/gpl.html ## -## ################################################################################ ## -## +## }}} CC := zip-gcc AR := zip-ar OBJDIR := obj-zip diff --git a/sw/zlib/board.h b/sw/zlib/board.h index d243a35..7ab6c8f 100644 --- a/sw/zlib/board.h +++ b/sw/zlib/board.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: ./board.h -// +// {{{ // Project: ZBasic, a generic toplevel implementation using the full ZipCPU // // DO NOT EDIT THIS FILE! @@ -14,11 +14,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -31,14 +31,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef BOARD_H #define BOARD_H @@ -46,8 +46,11 @@ // from within our main.v file, let's include: #include +//////////////////////////////////////////////////////////////////////////////// +// +// ZipCPU defines and macros +// {{{ #include -#include #define _HAVE_ZIPSYS #define PIC _zip->z_pic @@ -60,47 +63,20 @@ #define _HAVE_ZIPSYS_PERFORMANCE_COUNTERS #endif // INCLUDE_ACCOUNTING_COUNTERS #endif // INCLUDE_ZIPCPU +// }}} -typedef struct CONSOLE_S { - unsigned u_setup; - unsigned u_fifo; - unsigned u_rx, u_tx; -} CONSOLE; - -#define _uart_txbusy ((_uart->u_fifo & 0x10000)==0) +#define SYSPIC(A) (1<<(A)) +//////////////////////////////////////////////////////////////////////////////// // -// GPIO input wires -// -#define GPIO_IN(WIRE) (((WIRE)>>16)&1) -// -// GPIO output wires -// -#define GPIO_SET(WIRE) (((WIRE)<<16)|(WIRE)) -#define GPIO_CLR(WIRE) ((WIRE)<<16) +// SD SPI constants +// {{{ +//////////////////////////////////////////////////////////////////////////////// // // - -#define BUSPIC(X) (1<u_fifo & 0x10000)==0) +#define _uart_txidle ((_uart->u_tx & 0x100) ==0) + + +#define BUSPIC(X) (1<>16)&1) +// +// GPIO output wires +// +#define GPIO_SET(WIRE) (((WIRE)<<16)|(WIRE)) +#define GPIO_CLR(WIRE) ((WIRE)<<16) +// +// + + +#define _BOARD_HAS_BUILDTIME +static volatile unsigned *const _buildtime = ((unsigned *)0x00a00000); +#ifdef BKRAM_ACCESS +#define _BOARD_HAS_BKRAM +extern char _bkram[0x00100000]; +#endif // BKRAM_ACCESS #ifdef VERSION_ACCESS #define _BOARD_HAS_VERSION static volatile unsigned *const _version = ((unsigned *)0x00a00018); #endif // VERSION_ACCESS -#ifdef FLASHCFG_ACCESS -#define _BOARD_HAS_FLASHCFG -static volatile unsigned * const _flashcfg = ((unsigned *)(0x00200000)); -#endif // FLASHCFG_ACCESS -#ifdef BUSPIC_ACCESS -#define _BOARD_HAS_BUSPIC -static volatile unsigned *const _buspic = ((unsigned *)0x00a00008); -#endif // BUSPIC_ACCESS +#ifdef SDSPI_ACCESS +#define _BOARD_HAS_SDSPI +static volatile SDSPI *const _sdcard = ((SDSPI *)0x00400000); +#endif // SDSPI_ACCESS #ifdef RTC_ACCESS #define _BOARD_HAS_RTC static volatile RTCLIGHT *const _rtc = ((RTCLIGHT *)0x00800000); #endif // RTC_ACCESS -#ifdef BKRAM_ACCESS -#define _BOARD_HAS_BKRAM -extern char _bkram[0x00100000]; -#endif // BKRAM_ACCESS -#ifdef FLASH_ACCESS -#define _BOARD_HAS_FLASH -extern int _flash[1]; -#endif // FLASH_ACCESS -#define _BOARD_HAS_BUILDTIME -static volatile unsigned *const _buildtime = ((unsigned *)0x00a00000); +#ifdef BUSCONSOLE_ACCESS +#define _BOARD_HAS_BUSCONSOLE +static volatile CONSOLE *const _uart = ((CONSOLE *)0x00600000); +#endif // BUSCONSOLE_ACCESS +#ifdef BUSPIC_ACCESS +#define _BOARD_HAS_BUSPIC +static volatile unsigned *const _buspic = ((unsigned *)0x00a00008); +#endif // BUSPIC_ACCESS #define _BOARD_HAS_BUSERR static volatile unsigned *const _buserr = ((unsigned *)0x00a00004); #ifdef PWRCOUNT_ACCESS @@ -169,16 +175,21 @@ static volatile unsigned *const _pwrcount = ((unsigned *)0x00a00010); #define _BOARD_HAS_RTCDATE static volatile unsigned *const _rtcdate = ((unsigned *)10485780); #endif // RTCDATE_ACCESS -#ifdef SDSPI_ACCESS -#define _BOARD_HAS_SDSPI -static volatile SDSPI *const _sdcard = ((SDSPI *)0x00400000); -#endif // SDSPI_ACCESS +#ifdef GPIO_ACCESS +#define _BOARD_HAS_GPIO +static volatile unsigned *const _gpio = ((unsigned *)0x00a0000c); +#endif // GPIO_ACCESS +#ifdef FLASHCFG_ACCESS +#define _BOARD_HAS_FLASHCFG +static volatile unsigned * const _flashcfg = ((unsigned *)(0x00200000)); +#endif // FLASHCFG_ACCESS +#ifdef FLASH_ACCESS +#define _BOARD_HAS_FLASH +extern int _flash[1]; +#endif // FLASH_ACCESS // // Interrupt assignments (3 PICs) // -// PIC: buspic -#define BUSPIC_GPIO BUSPIC(0) -#define BUSPIC_SDCARD BUSPIC(1) // PIC: syspic #define SYSPIC_DMAC SYSPIC(0) #define SYSPIC_JIFFIES SYSPIC(1) @@ -187,9 +198,9 @@ static volatile SDSPI *const _sdcard = ((SDSPI *)0x00400000); #define SYSPIC_TMA SYSPIC(4) #define SYSPIC_ALT SYSPIC(5) #define SYSPIC_BUS SYSPIC(6) -#define SYSPIC_UARTTXF SYSPIC(7) +#define SYSPIC_SDCARD SYSPIC(7) #define SYSPIC_UARTRXF SYSPIC(8) -#define SYSPIC_SDCARD SYSPIC(9) +#define SYSPIC_UARTTXF SYSPIC(9) // PIC: altpic #define ALTPIC_UIC ALTPIC(0) #define ALTPIC_UOC ALTPIC(1) @@ -199,7 +210,10 @@ static volatile SDSPI *const _sdcard = ((SDSPI *)0x00400000); #define ALTPIC_MOC ALTPIC(5) #define ALTPIC_MPC ALTPIC(6) #define ALTPIC_MTC ALTPIC(7) -#define ALTPIC_UARTTX ALTPIC(8) -#define ALTPIC_UARTRX ALTPIC(9) -#define ALTPIC_RTC ALTPIC(10) +#define ALTPIC_RTC ALTPIC(8) +#define ALTPIC_UARTTX ALTPIC(9) +#define ALTPIC_UARTRX ALTPIC(10) +// PIC: buspic +#define BUSPIC_SDCARD BUSPIC(0) +#define BUSPIC_GPIO BUSPIC(1) #endif // BOARD_H diff --git a/sw/zlib/bootloader.h b/sw/zlib/bootloader.h index 5224423..8f49294 100644 --- a/sw/zlib/bootloader.h +++ b/sw/zlib/bootloader.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: bootloader.h -// +// {{{ // Project: Zip CPU -- a small, lightweight, RISC CPU soft core // // Purpose: @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,14 +28,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef BOOTLOADER_H #define BOOTLOADER_H diff --git a/sw/zlib/crt0.c b/sw/zlib/crt0.c index a482f75..7e41767 100644 --- a/sw/zlib/crt0.c +++ b/sw/zlib/crt0.c @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: crt0.c -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: To start a program from flash, loading its various components @@ -90,9 +90,9 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or // modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at @@ -103,18 +103,25 @@ // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // for more details. // +// You should have received a copy of the GNU General Public License along +// with this program. (It's in the $(ROOT)/doc directory. Run make with no +// target there if the PDF file isn't present.) If not, see +// for a copy. +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include "zipcpu.h" #include "board.h" // Our current board support file #include "bootloader.h" #include "zipsys.h" +// USE_DMA +// {{{ // A bootloader is about nothing more than copying memory from a couple // particular locations (Flash/ROM) to other locations in memory (BLKRAM // and SDRAM). Our DMA is a hardware accelerator that does nothing but @@ -127,13 +134,16 @@ // however: 1) It obscures for any readers of this code what is actually // happening, and 2) it makes the code dependent upon yet another piece of the // hardware design working. For these reasons, we allow you to turn it off. +// }}} #ifdef _HAVE_ZIPSYS_DMA #define USE_DMA #endif // -// _start: -// +// _start: (Assembly routine) +// {{{ +// Notes: +// {{{ // Every computer needs to start processing from somewhere on reboot, and every // program needs some entry point. For the ZipCPU, that starting place is the // routine with the _start symbol. It is important that this start symbol be @@ -151,7 +161,7 @@ // the kernel entry function. It also sets up a return address for the kernel // entry function so that, should the kernel ever exit, it wouldn't exit on // any error but rather it would exit by halting the CPU. -// +// }}} asm("\t.section\t.start,\"ax\",@progbits\n" "\t.global\t_start\n" "_start:" "\t; Here's the global ZipCPU entry point upon reset/reboot\n" @@ -187,19 +197,21 @@ asm("\t.section\t.start,\"ax\",@progbits\n" "_argv:\n" "\t.WORD\t0,0\n" "\t.section\t.text"); +// }}} -// +// extern _bootloader +// {{{ // We need to insist that the bootloader be kept in Flash, else it would depend // upon running a routine from memory that ... wasn't in memory yet. For this // purpose, we place the bootloader in a special .boot section. We'll also tell // the linker, via the linker script, that this .boot section needs to be placed // into flash. -// +// }}} extern void _bootloader(void) __attribute__ ((section (".boot"))); // // bootloader() -// +// {{{ // Here's the actual boot loader itself. It copies three areas from flash: // 1. An area from flash to block RAM // 2. A second area from flash to SDRAM @@ -207,12 +219,31 @@ extern void _bootloader(void) __attribute__ ((section (".boot"))); // zero. This is sometimes called the BSS segment. // #ifndef SKIP_BOOTLOADER +// NOTNULL(A) +// {{{ +// Several comparisons below look like: if (_ram != NULL) or even +// if (_ram == NULL). The problem is that, in order to optimize linking, we've +// defined _ram as something like: unsigned _ram[1]; This means that GCC +// believes space is actually allocated for _ram somewhere (by the linker), but +// just doesn't know where. Hence, GCC will optimize these comparisons away, +// since it knows they cannot be NULL. The macro below helps to return the +// comparison back to what it should be--getting GCC out of the way in the +// process. It's an ugly hack, but it works. #define NOTNULL(A) (4 != (unsigned)&A[1]) +// }}} void _bootloader(void) { // NSTR("BOOTLOADER"); int *ramend = _ram_image_end, *bsend = _bss_image_end; if (!NOTNULL(_rom)) { + // No ROM present + // {{{ + // In this case, we have the least to do: an external loader + // has already been responsible for loading us into memory. + // All that's then required is simply to zero out the BSS + // segment and start. This can be done either with (or without) + // the DMA. If you want to understand how this routine + // works, check out the non-DMA section #ifdef USE_DMA // NSTR("No-ROM"); // @@ -234,22 +265,31 @@ void _bootloader(void) { while((_zip->z_pic & SYSINT_DMAC)==0) ; } CLEAR_CACHE; -#else +#else // USE_DMA int *wrp = _ram_image_end; while(wrp < _bss_image_end) *wrp++ = 0; +#endif // USE_DMA return; -#endif - return; + // }}} } int *kramdev = (_kram) ? _kram : _ram; + // Okay, we have ROM present. We need to copy our instructions and + // data from the ROM to a designated RAM area. Indeed, we allow two + // designated RAM areas: kernel (fast) and normal RAM. Once done, + // we zero out the BSS segment. This can be done with or without the + // DMA #ifdef USE_DMA + // {{{ // Disable and clear all interrupts _zip->z_pic = CLEARPIC; // NSTR("DMA"); _zip->z_dma.d_ctrl= DMACLEAR; + + // Copy to Kernel RAM + // {{{ if (NOTNULL(_kram)) { _zip->z_dma.d_rd = _kram_start; // Flash memory ptr _zip->z_dma.d_wr = (_kram) ? _kram : _ram; @@ -263,18 +303,17 @@ void _bootloader(void) { while((_zip->z_pic & SYSINT_DMAC)==0) ; } - - // Writing to kram, need to switch to RAM - _zip->z_dma.d_wr = _ram; - _zip->z_dma.d_len = ramend - _ram; } else { // NSTR("No-KRAM"); // Continue writing to the RAM device from where we left off - _zip->z_dma.d_len = ramend - _ram; _zip->z_dma.d_rd = _ram_image_start; // ROM (flash) memory - _zip->z_dma.d_wr = _ram; } + // }}} + // Copy to external or regular RAM + // {{{ + _zip->z_dma.d_wr = _ram; + _zip->z_dma.d_len = ramend - _ram; if (_zip->z_dma.d_len>0) { // NSTR("RAM"); _zip->z_pic = SYSINT_DMAC; @@ -283,7 +322,10 @@ void _bootloader(void) { while((_zip->z_pic & SYSINT_DMAC)==0) ; } + // }}} + // Zero out the BSS segment + // {{{ if (bsend != ramend) { volatile int zero = 0; @@ -297,31 +339,44 @@ void _bootloader(void) { while((_zip->z_pic & SYSINT_DMAC)==0) ; } + // }}} CLEAR_CACHE; // Disable and clear all interrupts _zip->z_pic = CLEARPIC; + // }}} #else - int *rdp = _kram_start, *wrp = (_kram) ? _kram : _ram; + // {{{ + int *rdp = _kram_start, *wrp; // NSTR("Not using DMA"); + // Copy to Kernel RAM + // {{{ + if (NOTNULL(_kram)) + wrp = _kram; + else + wrp = _ram; + // Load any part of the image into kernel RAM (i.e. fast or block RAM), + // but *only* if there's a block RAM section in the image. Based upon + // our LD script, the block RAM should be filled from _blkram to + // _kernel_image_end. It starts at _kram_start --- our last valid + // address within the flash address region. // - // Load any part of the image into block RAM, but *only* if there's a - // block RAM section in the image. Based upon our LD script, the - // block RAM should be filled from _blkram to _kernel_image_end. - // It starts at _kram_start --- our last valid address within - // the flash address region. - // - if (_kram_end != _kram_start) { + if (_kram_end != _kram_start) { // The memory copy // NSTR("KRAM"); while(wrp < _kram_end) *wrp++ = *rdp++; } + // }}} + // If there's a separate RAM section, then we'll switch to writing into + // it next. If not, we'll continue writing from where we left off in + // kernel RAM. if (NOTNULL(_ram)) wrp = _ram; - // + // Copy to external or regular RAM + // {{{ // Now, we move on to the SDRAM image. We'll here load into SDRAM // memory up to the end of the SDRAM image, _sdram_image_end. // As with the last pointer, this one is also created for us by the @@ -331,8 +386,10 @@ void _bootloader(void) { // NSTR("RAM"); for(int i=0; i< ramend - _ram; i++) *wrp++ = *rdp++; + // }}} - // + // Zero out the BSS segment + // {{{ // Finally, we load BSS. This is the segment that only needs to be // cleared to zero. It is available for global variables, but some // initialization is expected within it. We start writing where @@ -341,9 +398,11 @@ void _bootloader(void) { // NSTR("BSS"); for(int i=0; i for a copy. +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include #include #include @@ -51,35 +56,85 @@ #define UARTTX _uart->u_tx #endif +#ifdef _ZIP_HAS_WBUART +#define TXBUSY ((_uart->u_fifo & 0x010000)==0) +#define TXCLEAR (((UARTTX & 0x100)==0) \ + && (((_uart->u_fifo >> 18) & 0x3ff) \ + >= (1 << ((_uart->u_fifo>>28)&&15))-1)) +#elif defined(_ZIP_HAS_UARTTX) +#define TXBUSY ((UARTTX & 0x0100)!=0) +#define TXCLEAR TXBUSY +#endif + void _outbyte(char v) { -#ifdef _ZIP_HAS_WBUART if (v == '\n') { // Depend upon the WBUART, not the PIC - while((_uart->u_fifo & 0x010000)==0) + while(TXBUSY) ; UARTTX = (unsigned)'\r'; } // Depend upon the WBUART, not the PIC - while((_uart->u_fifo & 0x010000)==0) + while(TXBUSY) ; uint8_t c = v; UARTTX = (unsigned)c; +} + +void +_outbytes(int nbytes, const char *buf) { +#ifdef FIFO_ENABLED + // {{{ + const uint8_t *ptr = buf; + int fifosz; + + // Get the size of the FIFO + fifosz = _uart->u_fifo >> 28; + fifosz = 1<u_fifo) & 0x010000)==0) + ; + available = ufifo >> 18; + available &= (fifosz-1); + + // Don't check availability again: for each available + // position, simply output the desired character while + // doing LF -> CR/LF translation + for(int k=0; (ku_fifo & 0x010000)==0) + ; + } UARTTX = '\r'; + } + } + } + // }}} #else -#ifdef _ZIP_HAS_UARTTX - // Depend upon the WBUART, not the PIC - while(UARTTX & 0x100) - ; - uint8_t c = v; - UARTTX = (unsigned)c; -#endif + const uint8_t *ptr = buf; + + for(int i=0; iu_tx & 0x0100) + while(!TXCLEAR) ; -#elif defined(_BOARD_HAS_BUSCONSOLE) - while(_uart->u_tx & 0x0100) - ; -#elif defined(_ZIP_HAS_UARTTX) - // Depend upon the WBUART, not the PIC - while(UARTTX & 0x100) - ; - uint8_t c = v; - UARTTX = (unsigned)c; -#else -// #error "No console" -#endif _hw_shutdown(rcode); } diff --git a/sw/zlib/udiv.c b/sw/zlib/udiv.c index f158358..fca4a4b 100644 --- a/sw/zlib/udiv.c +++ b/sw/zlib/udiv.c @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: udiv.c -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This is a temporary file--a crutch if you will--until a similar @@ -13,11 +13,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -30,21 +30,24 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include - +// Bit reversal +// {{{ #ifdef __ZIPCPU__ #include "zipcpu.h" #else +// This is a compatibility function, in case we are ever running ZipCPU +// programs on a non-ZipCPU platform uint32_t zip_bitrev(const uint32_t a) { uint32_t r, b = a; r = 0; @@ -54,7 +57,10 @@ uint32_t zip_bitrev(const uint32_t a) { } return r; } #endif +// }}} +// cltz: Count leading zeros +// {{{ extern int cltz(unsigned long); #ifdef __ZIPCPU__ @@ -120,7 +126,10 @@ cltz(unsigned long v) { return cnt; } #endif +// }}} +// __udivdi3 +// {{{ unsigned long __udivdi3(unsigned long a, unsigned long b) { unsigned long r; @@ -168,8 +177,10 @@ __udivdi3(unsigned long a, unsigned long b) { } return r; } } +// }}} -// +// __divdi3 +// {{{ // A possible assembly version of __divdi3 // // SUB 8,SP @@ -225,3 +236,4 @@ long __divdi3(long a, long b) { r = -r; return r; } +// }}} diff --git a/sw/zlib/umod.c b/sw/zlib/umod.c index 0f20790..aeb3ce1 100644 --- a/sw/zlib/umod.c +++ b/sw/zlib/umod.c @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: umod.c -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: This is a temporary file--a crutch if you will--until a similar @@ -19,11 +19,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2017-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2017-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -36,14 +36,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #include diff --git a/sw/zlib/zipcpu.h b/sw/zlib/zipcpu.h index 592c066..17798c3 100644 --- a/sw/zlib/zipcpu.h +++ b/sw/zlib/zipcpu.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // -// Filename: zipsys.h -// +// Filename: zipcpu.h +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Declare the capabilities and memory structure of the ZipSystem @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,14 +28,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef ZIPCPU_H #define ZIPCPU_H @@ -68,12 +68,13 @@ #define SETUREG(A,ID) asm("MOV %0," ID : : "r"(A)) #define NSTR(A) asm("NSTR \"" A "\\n\"") #define NVAL(V) do { unsigned tmp = (unsigned)(V); asm volatile("NDUMP %0":"=r"(tmp):"0"(tmp)); } while(0) +#define NEXIT(V) do { unsigned tmp = (unsigned)(V); asm volatile("NEXIT %0":"=r"(tmp)); } while(0) extern void zip_rtu(void); extern void zip_halt(void); extern void zip_idle(void); extern void zip_syscall(void); -extern void zip_restore_context(int *); -extern void zip_save_context(int *); +extern void zip_restore_context(void *); +extern void zip_save_context(void *); extern int zip_bitrev(int v); extern unsigned zip_cc(void); extern unsigned zip_ucc(void); diff --git a/sw/zlib/zipsys.h b/sw/zlib/zipsys.h index 6bb2d8b..9fcdcde 100644 --- a/sw/zlib/zipsys.h +++ b/sw/zlib/zipsys.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Filename: zipsys.h -// +// {{{ // Project: ZBasic, a generic toplevel impl using the full ZipCPU // // Purpose: Declare the capabilities and memory structure of the ZipSystem @@ -11,11 +11,11 @@ // Gisselquist Technology, LLC // //////////////////////////////////////////////////////////////////////////////// -// -// Copyright (C) 2015-2020, Gisselquist Technology, LLC -// +// }}} +// Copyright (C) 2015-2022, Gisselquist Technology, LLC +// {{{ // This program is free software (firmware): you can redistribute it and/or -// modify it under the terms of the GNU General Public License as published +// modify it under the terms of the GNU General Public License as published // by the Free Software Foundation, either version 3 of the License, or (at // your option) any later version. // @@ -28,14 +28,14 @@ // with this program. (It's in the $(ROOT)/doc directory. Run make with no // target there if the PDF file isn't present.) If not, see // for a copy. -// +// }}} // License: GPL, v3, as defined and found on www.gnu.org, +// {{{ // http://www.gnu.org/licenses/gpl.html // -// //////////////////////////////////////////////////////////////////////////////// // -// +// }}} #ifndef ZIPSYS_H #define ZIPSYS_H