From f16d9f9f48fb5e74ab10fe79a8206223342d5010 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Tue, 28 Apr 2020 23:42:20 +0300 Subject: fix(timer): incorrect high counter byte behavior According to the datasheet, the value of the high byte of the counter for 16-bit timers (such as timer 1) is only updated when the low byte is being read/written. close #37 --- src/cpu/cpu.ts | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/cpu') diff --git a/src/cpu/cpu.ts b/src/cpu/cpu.ts index 0c60c9e..5663a1c 100644 --- a/src/cpu/cpu.ts +++ b/src/cpu/cpu.ts @@ -40,11 +40,16 @@ export interface CPUMemoryHooks { [key: number]: CPUMemoryHook; } +export type CPUMemoryReadHook = (addr: u16) => u8; +export interface CPUMemoryReadHooks { + [key: number]: CPUMemoryReadHook; +} export class CPU implements ICPU { readonly data: Uint8Array = new Uint8Array(this.sramBytes + registerSpace); readonly data16 = new Uint16Array(this.data.buffer); readonly dataView = new DataView(this.data.buffer); readonly progBytes = new Uint8Array(this.progMem.buffer); + readonly readHooks: CPUMemoryReadHooks = []; readonly writeHooks: CPUMemoryHooks = []; readonly pc22Bits = this.progBytes.length > 0x20000; @@ -61,6 +66,9 @@ export class CPU implements ICPU { } readData(addr: number) { + if (addr >= 32 && this.readHooks[addr]) { + return this.readHooks[addr](addr); + } return this.data[addr]; } -- cgit v1.2.3