From aa55b972e019396f7ead05d1b9047d178f500dbc Mon Sep 17 00:00:00 2001 From: edmccard Date: Fri, 27 Apr 2012 13:09:32 -0400 Subject: [PATCH] opindex interface to memory --- src/cpu/ctfe_d6502.d | 8 ++++---- src/cpu/d6502.d | 6 +++--- test/d6502/benchmark.d | 5 +++-- test/d6502/cpu.d | 16 +++++++++++++--- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/cpu/ctfe_d6502.d b/src/cpu/ctfe_d6502.d index 0523ca1..4b1f29a 100644 --- a/src/cpu/ctfe_d6502.d +++ b/src/cpu/ctfe_d6502.d @@ -1053,7 +1053,7 @@ string ReadOp(string var) string ReadRaw(string addr) { - return Attr("memory") ~ ".read(" ~ addr ~")"; + return Attr("memory") ~ "[" ~ addr ~"]"; } string ReadWordBasic(string type, string var, string addr1, string addr2) @@ -1106,20 +1106,20 @@ string Tick() string Peek(string addr) { return Tick() ~ - If!(strict)(Attr("memory") ~ ".read(" ~ addr ~");\n"); + If!(strict)(Attr("memory") ~ "[" ~ addr ~ "];\n"); } string Poke(string addr, string val) { return Tick() ~ If!(strict)( - Attr("memory") ~ ".write(" ~ addr ~ ", " ~ val ~ ");\n"); + Attr("memory") ~ "[" ~ addr ~ "] = " ~ val ~ ";\n"); } string Write(string addr, string val) { return Tick() ~ - Attr("memory") ~ ".write(" ~ addr ~ ", " ~ val ~ ");\n"; + Attr("memory") ~ "[" ~ addr ~ "] = " ~ val ~ ";\n"; } string IncPC() diff --git a/src/cpu/d6502.d b/src/cpu/d6502.d index 50b3044..42dbc74 100644 --- a/src/cpu/d6502.d +++ b/src/cpu/d6502.d @@ -81,8 +81,8 @@ public: final class Cpu(string chip, MEM, CLK) if (__traits(compiles, { MEM m; ubyte val; ushort addr; - val = m.read(addr); - m.write(addr, val); + val = m[addr]; + m[addr] = val; CLK c; int cycles; version(Cumulative) c.tick(cycles); else c.tick(); @@ -166,7 +166,7 @@ if (__traits(compiles, { static if (_chip == "6502") { idelay = ndelay = false; } version(Cumulative) { cycles = 1; } else { clock.tick(); } - opcode = memory.read(PC++); + opcode = memory[PC++]; mixin(OpExecute(_chip)); } while (keepRunning); } diff --git a/test/d6502/benchmark.d b/test/d6502/benchmark.d index 10451df..8daf415 100644 --- a/test/d6502/benchmark.d +++ b/test/d6502/benchmark.d @@ -13,7 +13,7 @@ final class BreakRunner this.mem = &mem; } - final ubyte read(ushort addr) + final ubyte opIndex(ushort addr) { if (addr == 0xfffe) { @@ -27,9 +27,10 @@ final class BreakRunner else return mem.read(addr); } - final void write(ushort addr, ubyte val) + final ubyte opIndexAssign(ubyte val, ushort addr) { mem.write(addr, val); + return val; } static if (cumulative) { final void tick(int) {} } diff --git a/test/d6502/cpu.d b/test/d6502/cpu.d index 2aec369..b979847 100644 --- a/test/d6502/cpu.d +++ b/test/d6502/cpu.d @@ -35,10 +35,7 @@ template isCMOS(T) class TestIO { ubyte delegate(ushort) dread; - ubyte read(ushort addr) { return dread(addr); } - void delegate(ushort, ubyte) dwrite; - void write(ushort addr, ubyte val) { dwrite(addr, val); } static if (cumulative) { @@ -50,6 +47,19 @@ class TestIO void delegate() dtick; void tick() { dtick(); } } + + ubyte opIndex(ushort i1) const + { + auto addr = cast(ushort)i1; + return dread(addr); + } + + ubyte opIndexAssign(ubyte val, ushort i1) + { + auto addr = cast(ushort)i1; + dwrite(addr, val); + return val; + } }