Skip to content

Commit

Permalink
[SC64][SW] Added API versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Polprzewodnikowy committed Feb 14, 2023
1 parent 46f7b8b commit 740114c
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/02_usb_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

| id | name | arg0 | arg1 | data | response | description |
| --- | ---------------------- | ------------ | ------------ | ---- | ---------------- | --------------------------------------------------- |
| `v` | **VERSION_GET** | --- | --- | --- | api_version | Get command API version |
| `v` | **HW_VERSION_GET** | --- | --- | --- | hw_version | Get HW version |
| `V` | **API_VERSION_GET** | --- | --- | --- | api_version | Get USB command API version |
| `R` | **STATE_RESET** | --- | --- | --- | --- | Reset entire flashcart state |
| `B` | **CIC_PARAMS_SET** | cic_params_0 | cic_params_1 | --- | --- | Set CIC disable/mode/seed/checksum |
| `c` | **CONFIG_GET** | config_id | --- | --- | current_value | Get config option |
Expand Down
3 changes: 2 additions & 1 deletion docs/03_n64_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

| id | name | arg0 | arg1 | rsp0 | rsp1 | description |
| --- | --------------------- | ---------- | ------------ | ---------------- | -------------- | -------------------------------------------------- |
| `v` | **VERSION_GET** | --- | --- | api_version | --- | Get command API version |
| `v` | **HW_VERSION_GET** | --- | --- | hw_version | --- | Get HW version |
| `V` | **API_VERSION_GET** | --- | --- | api_version | --- | Get N64 command API version |
| `c` | **CONFIG_GET** | config_id | --- | --- | current_value | Get config option |
| `C` | **CONFIG_SET** | config_id | new_value | --- | previous_value | Set config option and get previous value |
| `c` | **SETTING_GET** | setting_id | --- | --- | current_value | Get persistent setting option |
Expand Down
3 changes: 2 additions & 1 deletion sw/bootloader/src/sc64.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ typedef struct {
#define SC64_KEY_LOCK (0xFFFFFFFFUL)

typedef enum {
SC64_CMD_VERSION_GET = 'v',
SC64_CMD_HW_VERSION_GET = 'v',
SC64_CMD_API_VERSION_GET = 'V',
SC64_CMD_CONFIG_GET = 'c',
SC64_CMD_CONFIG_SET = 'C',
SC64_CMD_SETTING_GET = 'a',
Expand Down
1 change: 1 addition & 0 deletions sw/controller/app.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SRC_FILES = \
timer.c \
update.c \
usb.c \
version.c \
writeback.c

include common.mk
Expand Down
5 changes: 5 additions & 0 deletions sw/controller/src/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "rtc.h"
#include "sd.h"
#include "usb.h"
#include "version.h"
#include "writeback.h"


Expand Down Expand Up @@ -464,6 +465,10 @@ void cfg_process (void) {
args[0] = cfg_get_version();
break;

case 'V':
args[0] = version_api(API_N64);
break;

case 'c':
if (cfg_query(args)) {
cfg_set_error(CFG_ERROR_BAD_CONFIG_ID);
Expand Down
8 changes: 8 additions & 0 deletions sw/controller/src/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "rtc.h"
#include "update.h"
#include "usb.h"
#include "version.h"


enum rx_state {
Expand Down Expand Up @@ -162,6 +163,13 @@ static void usb_rx_process (void) {
p.response_info.data[0] = cfg_get_version();
break;

case 'V':
p.rx_state = RX_STATE_IDLE;
p.response_pending = true;
p.response_info.data_length = 4;
p.response_info.data[0] = version_api(API_USB);
break;

case 'R':
cfg_reset_state();
cic_reset_parameters();
Expand Down
17 changes: 17 additions & 0 deletions sw/controller/src/version.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "version.h"


#define VERSION_API_USB (1)
#define VERSION_API_N64 (1)


uint32_t version_api (version_api_type_t type) {
switch (type) {
case API_USB:
return VERSION_API_USB;
case API_N64:
return VERSION_API_N64;
default:
return 0;
}
}
17 changes: 17 additions & 0 deletions sw/controller/src/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef VERSION_H__
#define VERSION_H__


#include <stdint.h>


typedef enum {
API_USB,
API_N64,
} version_api_type_t;


uint32_t version_api (version_api_type_t type);


#endif
17 changes: 15 additions & 2 deletions sw/pc/sc64.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ class __DebugDatatype(IntEnum):
SCREENSHOT = 4
GDB = 0xDB

__MIN_SUPPORTED_API_VERSION = 1

__isv_line_buffer: bytes = b''
__debug_header: Optional[bytes] = None
__gdb_client: Optional[socket.socket] = None
Expand All @@ -327,11 +329,20 @@ def __init__(self) -> None:
self.__link = SC64Serial()
version = self.__link.execute_cmd(cmd=b'v')
if (version != b'SCv2'):
raise ConnectionException('Unknown SC64 API version')
raise ConnectionException('Unknown SC64 HW version')

def __get_int(self, data: bytes) -> int:
return int.from_bytes(data[:4], byteorder='big')

def check_api_version(self) -> None:
try:
data = self.__link.execute_cmd(cmd=b'V')
except ConnectionException:
raise ConnectionException('Outdated SC64 API, please update firmware')
version = self.__get_int(data)
if (version < self.__MIN_SUPPORTED_API_VERSION):
raise ConnectionException('Unsupported SC64 API version, please update firmware')

def __set_config(self, config: __CfgId, value: int) -> None:
try:
self.__link.execute_cmd(cmd=b'C', args=[config, value])
Expand Down Expand Up @@ -876,7 +887,9 @@ def cic_params_type(argument: str):
status_callback = lambda status: print(f'{status} ', end='', flush=True)
sc64.update_firmware(f.read(), status_callback)
print('done')


sc64.check_api_version()

if (args.update_bootloader):
with open(args.update_bootloader, 'rb') as f:
print('Uploading Bootloader... ', end='', flush=True)
Expand Down

0 comments on commit 740114c

Please sign in to comment.