From 6de29251b559ea1195c123459536a23b83ac1b5a Mon Sep 17 00:00:00 2001 From: Polprzewodnikowy Date: Sat, 20 Aug 2022 21:30:55 +0200 Subject: [PATCH] button and naming --- build.sh | 2 +- fw/rtl/n64/n64_cfg.sv | 2 +- fw/rtl/n64/n64_scb.sv | 2 + hw/pcb/sc64v2.kicad_pcb | 2 +- hw/pcb/sc64v2.kicad_sch | 2 +- sw/controller/app.mk | 1 + sw/controller/src/button.c | 78 ++++++++++++++++++++++++++++++++++++++ sw/controller/src/button.h | 23 +++++++++++ sw/controller/src/cfg.c | 15 ++++++++ sw/controller/src/gvr.c | 3 ++ sw/controller/src/usb.h | 1 + 11 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 sw/controller/src/button.c create mode 100644 sw/controller/src/button.h diff --git a/build.sh b/build.sh index 65306fb..05ce52b 100755 --- a/build.sh +++ b/build.sh @@ -105,7 +105,7 @@ build_release () { } print_usage () { - echo "builder script for SummerCart64" + echo "builder script for SC64" echo "usage: ./build.sh [bootloader] [controller] [fpga] [update] [release] [-c] [--help]" echo "parameters:" echo " bootloader - compile N64 bootloader software" diff --git a/fw/rtl/n64/n64_cfg.sv b/fw/rtl/n64/n64_cfg.sv index 95c10a2..a89f6a1 100644 --- a/fw/rtl/n64/n64_cfg.sv +++ b/fw/rtl/n64/n64_cfg.sv @@ -38,7 +38,7 @@ module n64_cfg ( end always_ff @(posedge clk) begin - if (reset) begin + if (reset || n64_scb.n64_reset) begin n64_scb.cfg_pending <= 1'b0; n64_scb.cfg_cmd <= 8'h00; irq <= 1'b0; diff --git a/fw/rtl/n64/n64_scb.sv b/fw/rtl/n64/n64_scb.sv index 2901f07..b961435 100644 --- a/fw/rtl/n64/n64_scb.sv +++ b/fw/rtl/n64/n64_scb.sv @@ -159,6 +159,8 @@ interface n64_scb (); ); modport cfg ( + input n64_reset, + output cfg_pending, input cfg_done, input cfg_error, diff --git a/hw/pcb/sc64v2.kicad_pcb b/hw/pcb/sc64v2.kicad_pcb index 467d6d9..bd42713 100644 --- a/hw/pcb/sc64v2.kicad_pcb +++ b/hw/pcb/sc64v2.kicad_pcb @@ -6,7 +6,7 @@ (paper "A4") (title_block - (title "SummerCart64") + (title "SC64") (date "2022-05-15") (rev "2.0") (company "Polprzewodnikowy") diff --git a/hw/pcb/sc64v2.kicad_sch b/hw/pcb/sc64v2.kicad_sch index f1ca86e..c0f2c54 100644 --- a/hw/pcb/sc64v2.kicad_sch +++ b/hw/pcb/sc64v2.kicad_sch @@ -5,7 +5,7 @@ (paper "A2") (title_block - (title "SummerCart64") + (title "SC64") (date "2022-05-15") (rev "2.0") (company "Polprzewodnikowy") diff --git a/sw/controller/app.mk b/sw/controller/app.mk index d9534f3..5194466 100644 --- a/sw/controller/app.mk +++ b/sw/controller/app.mk @@ -4,6 +4,7 @@ BUILD_DIR = build/app SRC_FILES = \ app.S \ app.c \ + button.c \ cfg.c \ cic.c \ dd.c \ diff --git a/sw/controller/src/button.c b/sw/controller/src/button.c new file mode 100644 index 0000000..319eed2 --- /dev/null +++ b/sw/controller/src/button.c @@ -0,0 +1,78 @@ +#include +#include "button.h" +#include "fpga.h" +#include "usb.h" + + +struct process { + uint32_t shift; + bool state; + button_mode_t mode; + bool trigger; +}; + + +static struct process p; + + +bool button_get_state (void) { + return p.state; +} + +button_mode_t button_get_mode (void) { + return p.mode; +} + +void button_set_mode (button_mode_t mode) { + p.mode = mode; + if (p.mode == BUTTON_MODE_NONE) { + p.trigger = false; + } +} + +void button_init (void) { + p.shift = 0x00000000UL; + p.state = false; + p.mode = BUTTON_MODE_NONE; + p.trigger = false; +} + +void button_process (void) { + usb_tx_info_t packet_info; + uint32_t status = fpga_reg_get(REG_STATUS); + p.shift <<= 1; + if (!(status & STATUS_BUTTON)) { + p.shift |= (1 << 0); + } + if (!p.state && p.shift == 0xFFFFFFFFUL) { + p.state = true; + p.trigger = true; + } + if (p.state && p.shift == 0x00000000UL) { + p.state = false; + } + if (p.trigger) { + switch (p.mode) { + case BUTTON_MODE_N64_IRQ: + fpga_reg_set(REG_CFG_CMD, fpga_reg_get(REG_CFG_CMD) | CFG_CMD_IRQ); + p.trigger = false; + break; + + case BUTTON_MODE_USB_PACKET: + usb_create_packet(&packet_info, PACKET_CMD_BUTTON_TRIGGER); + if (usb_enqueue_packet(&packet_info)) { + p.trigger = false; + } + break; + + case BUTTON_MODE_DD_DISK_SWAP: + // TODO: implement 64DD disk swapping + p.trigger = false; + break; + + default: + p.trigger = false; + break; + } + } +} diff --git a/sw/controller/src/button.h b/sw/controller/src/button.h new file mode 100644 index 0000000..5f14e4a --- /dev/null +++ b/sw/controller/src/button.h @@ -0,0 +1,23 @@ +#ifndef BUTTON_H__ +#define BUTTON_H__ + + +#include + + +typedef enum { + BUTTON_MODE_NONE, + BUTTON_MODE_N64_IRQ, + BUTTON_MODE_USB_PACKET, + BUTTON_MODE_DD_DISK_SWAP, +} button_mode_t; + + +bool button_get_state (void); +button_mode_t button_get_mode (void); +void button_set_mode (button_mode_t mode); +void button_init (void); +void button_process (void); + + +#endif diff --git a/sw/controller/src/cfg.c b/sw/controller/src/cfg.c index c1c8337..487e68b 100644 --- a/sw/controller/src/cfg.c +++ b/sw/controller/src/cfg.c @@ -1,3 +1,4 @@ +#include "button.h" #include "cfg.h" #include "dd.h" #include "flash.h" @@ -20,6 +21,8 @@ typedef enum { CFG_ID_FLASH_ERASE_BLOCK, CFG_ID_DD_DRIVE_TYPE, CFG_ID_DD_DISK_STATE, + CFG_ID_BUTTON_STATE, + CFG_ID_BUTTON_MODE, } cfg_id_t; typedef enum { @@ -202,6 +205,12 @@ bool cfg_query (uint32_t *args) { case CFG_ID_DD_DISK_STATE: args[1] = dd_get_disk_state(); break; + case CFG_ID_BUTTON_STATE: + args[1] = button_get_state(); + break; + case CFG_ID_BUTTON_MODE: + args[1] = button_get_mode(); + break; default: return true; } @@ -258,6 +267,12 @@ bool cfg_update (uint32_t *args) { case CFG_ID_DD_DISK_STATE: dd_set_disk_state(args[1]); break; + case CFG_ID_BUTTON_STATE: + return true; + break; + case CFG_ID_BUTTON_MODE: + button_set_mode(args[1]); + break; default: return true; } diff --git a/sw/controller/src/gvr.c b/sw/controller/src/gvr.c index d935284..e14d99c 100644 --- a/sw/controller/src/gvr.c +++ b/sw/controller/src/gvr.c @@ -1,3 +1,4 @@ +#include "button.h" #include "cfg.h" #include "dd.h" #include "flashram.h" @@ -10,6 +11,7 @@ void gvr_task (void) { while (fpga_id_get() != FPGA_ID); + button_init(); cfg_init(); dd_init(); flashram_init(); @@ -17,6 +19,7 @@ void gvr_task (void) { usb_init(); while (1) { + button_process(); cfg_process(); dd_process(); flashram_process(); diff --git a/sw/controller/src/usb.h b/sw/controller/src/usb.h index b098071..2e5273b 100644 --- a/sw/controller/src/usb.h +++ b/sw/controller/src/usb.h @@ -7,6 +7,7 @@ typedef enum packet_cmd { + PACKET_CMD_BUTTON_TRIGGER = 'B', PACKET_CMD_DD_REQUEST = 'D', PACKET_CMD_DEBUG_OUTPUT = 'U', PACKET_CMD_ISV_OUTPUT = 'I',