From a7a959a677efe79259d0311809b8d1c6ce82aa94 Mon Sep 17 00:00:00 2001 From: Bertold Van den Bergh Date: Thu, 26 Aug 2021 16:27:41 +0200 Subject: [PATCH] Access TVD regs from main loop to avoid concurrent access --- mshal/hal_patch_tvdsafe.go | 65 ++++++++++++++++++++++++++++++++++++++ mshal/hal_region.go | 8 +++++ 2 files changed, 73 insertions(+) create mode 100644 mshal/hal_patch_tvdsafe.go diff --git a/mshal/hal_patch_tvdsafe.go b/mshal/hal_patch_tvdsafe.go new file mode 100644 index 0000000..49e0ed8 --- /dev/null +++ b/mshal/hal_patch_tvdsafe.go @@ -0,0 +1,65 @@ +package mshal + +type halPatchTVDMemoryRegion struct { + hal *HAL +} + +func (h halPatchTVDMemoryRegion) GetName() MemoryRegionNameType { + return MemoryRegionRegisters2106TVD +} + +func (h halPatchTVDMemoryRegion) GetLength() int { + return 256 +} + +func (h halPatchTVDMemoryRegion) GetParent() (MemoryRegion, int) { + return nil, 0 +} + +func (h halPatchTVDMemoryRegion) read(addr int, buf []byte) (int, error) { + req := PatchExecFuncRequest{ + R7_A: uint8(addr), + } + + resp, err := h.hal.PatchExecFunc(false, 0x3a33, req) + if err != nil { + return 0, err + } + + buf[0] = resp.R7 + return 1, nil +} + +func (h halPatchTVDMemoryRegion) write(addr int, buf []byte) (int, error) { + req := PatchExecFuncRequest{ + R5: uint8(buf[0]), + R7_A: uint8(addr), + } + + _, err := h.hal.PatchExecFunc(false, 0x3a17, req) + if err != nil { + return 0, err + } + + return 1, nil +} + +func (h halPatchTVDMemoryRegion) Access(write bool, addr int, buf []byte) (int, error) { + if len(buf) == 0 { + return 0, nil + } + + if write { + return h.write(addr, buf) + } + + return h.read(addr, buf) +} + +func (h *HAL) patchMakeTVDRegion() MemoryRegion { + if !h.patchInstalled { + return nil + } + + return regionWrapCompleteIO(halPatchTVDMemoryRegion{hal: h}) +} diff --git a/mshal/hal_region.go b/mshal/hal_region.go index fb76860..df455b8 100644 --- a/mshal/hal_region.go +++ b/mshal/hal_region.go @@ -80,6 +80,14 @@ func (h *HAL) MemoryRegionGet(name MemoryRegionNameType) MemoryRegion { return regionWrapPartial(MemoryRegionUserConfig, h.MemoryRegionGet(MemoryRegionUserRAM), 0x3F0, 0x10) case MemoryRegionRegisters2106TVD: + if h.patchInstalled { + if h.config.LogFunc != nil { + h.config.LogFunc(1, "Using patched TVD access") + } + + return h.patchMakeTVDRegion() + } + return h.memoryRegionRegisters2106TVD() } }