system: add dip switch label configurations

This commit is contained in:
2025-03-02 00:23:53 +01:00
parent b8af67377c
commit 4d0ef54279
12 changed files with 117 additions and 67 deletions

View File

@ -1,40 +1,37 @@
#include <windows.h>
#include "platform/system.h"
#include <assert.h>
#include <stdlib.h>
#include <ntstatus.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "platform/system.h"
#include "platform/vfs.h"
#include "util/crc.h"
#include "util/dprintf.h"
#include "util/str.h"
#include "util/crc.h"
#define DATA_SIZE 503
#define BLOCK_SIZE (sizeof(uint32_t) + 4 + 1 + DATA_SIZE)
#pragma pack(push, 1)
typedef struct
{
typedef struct {
uint32_t checksum;
char padding[6];
uint8_t freeplay;
char data[DATA_SIZE - 2];
} CreditBlock;
typedef struct
{
typedef struct {
uint32_t checksum;
char padding[4];
uint8_t dip_switches;
char data[DATA_SIZE];
} DipSwBlock;
typedef struct
{
typedef struct {
CreditBlock credit_block;
DipSwBlock dip_switch_block;
char *data;
@ -50,16 +47,15 @@ static struct vfs_config vfs_config;
static void system_read_sysfile(const wchar_t *sys_file);
static void system_save_sysfile(const wchar_t *sys_file);
HRESULT system_init(const struct system_config *cfg, const struct vfs_config *vfs_cfg)
{
HRESULT system_init(const struct system_config *cfg,
const struct vfs_config *vfs_cfg) {
HRESULT hr;
wchar_t sys_file_path[MAX_PATH];
assert(cfg != NULL);
assert(vfs_cfg != NULL);
if (!cfg->enable)
{
if (!cfg->enable) {
return S_FALSE;
}
@ -78,13 +74,13 @@ HRESULT system_init(const struct system_config *cfg, const struct vfs_config *vf
return S_OK;
}
static void system_read_sysfile(const wchar_t *sys_file)
{
static void system_read_sysfile(const wchar_t *sys_file) {
FILE *f = _wfopen(sys_file, L"r");
if (f == NULL)
{
dprintf("System: First run detected, system settings can only be applied AFTER the first run\n");
if (f == NULL) {
dprintf(
"System: First run detected, system settings can only be applied "
"AFTER the first run\n");
return;
}
@ -92,8 +88,7 @@ static void system_read_sysfile(const wchar_t *sys_file)
long file_size = ftell(f);
fseek(f, 0, SEEK_SET);
if (file_size != 0x6000)
{
if (file_size != 0x6000) {
dprintf("System: Invalid sysfile.dat file size\n");
fclose(f);
@ -106,11 +101,11 @@ static void system_read_sysfile(const wchar_t *sys_file)
// copy the credit_block and dip_switch_block from the sysfile.dat
memcpy(&system_info.credit_block, system_info.data, BLOCK_SIZE);
memcpy(&system_info.dip_switch_block, system_info.data + 0x2800, BLOCK_SIZE);
memcpy(&system_info.dip_switch_block, system_info.data + 0x2800,
BLOCK_SIZE);
}
static void system_save_sysfile(const wchar_t *sys_file)
{
static void system_save_sysfile(const wchar_t *sys_file) {
char block[BLOCK_SIZE];
uint8_t system = 0;
uint8_t freeplay = 0;
@ -118,24 +113,28 @@ static void system_save_sysfile(const wchar_t *sys_file)
// open the sysfile.dat for writing in bytes mode
FILE *f = _wfopen(sys_file, L"rb+");
if (f == NULL)
{
if (f == NULL) {
return;
}
// write the system_config.system to the dip_switch_block
for (int i = 0; i < 8; i++)
{
if (system_config.dipsw[i])
{
// print which system is enabled
for (int i = 0; i < 8; i++) {
// print the dip switch configuration with labels if present
if (system_config.dipsw_config[i].label[0] != L'\0') {
dprintf("System: %ls: %ls\n", system_config.dipsw_config[i].label,
system_config.dipsw[i] ? system_config.dipsw_config[i].on
: system_config.dipsw_config[i].off);
} else if (system_config.dipsw[i]) {
dprintf("System: DipSw%d=1 set\n", i + 1);
}
// set the system variable to the dip switch configuration
if (system_config.dipsw[i]) {
system |= (1 << i);
}
}
if (system_config.freeplay)
{
if (system_config.freeplay) {
// print that freeplay is enabled
dprintf("System: Freeplay enabled\n");
freeplay = 1;
@ -149,10 +148,10 @@ static void system_save_sysfile(const wchar_t *sys_file)
// calculate the new checksum, skip the old crc32 value
// which is at the beginning of the block, thats's why the +4
// conver the struct to chars in order for the crc32 calculation to work
system_info.credit_block.checksum = crc32(
(char *)&system_info.credit_block + 4, BLOCK_SIZE - 4, 0);
system_info.dip_switch_block.checksum = crc32(
(char *)&system_info.dip_switch_block + 4, BLOCK_SIZE - 4, 0);
system_info.credit_block.checksum =
crc32((char *)&system_info.credit_block + 4, BLOCK_SIZE - 4, 0);
system_info.dip_switch_block.checksum =
crc32((char *)&system_info.dip_switch_block + 4, BLOCK_SIZE - 4, 0);
// build the new credit block
memcpy(block, (char *)&system_info.credit_block, BLOCK_SIZE);

View File

@ -7,10 +7,20 @@
#include "platform/vfs.h"
struct dipsw_config
{
wchar_t label[MAX_PATH];
wchar_t on[MAX_PATH];
wchar_t off[MAX_PATH];
};
struct system_config {
bool enable;
bool freeplay;
bool dipsw[8];
struct dipsw_config dipsw_config[8];
};
HRESULT system_init(const struct system_config *cfg, const struct vfs_config *vfs_cfg);