From 90e248e7453bb3ce94dd8a260bca6d2135916622 Mon Sep 17 00:00:00 2001 From: Ariejan de Vroom Date: Sun, 17 Aug 2014 16:49:22 +0200 Subject: [PATCH 1/7] Add basics for ACIA 6551:w --- acia6551.go | 28 ++++++++++++++++++++++++++++ acia6551_test.go | 15 +++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 acia6551.go create mode 100644 acia6551_test.go diff --git a/acia6551.go b/acia6551.go new file mode 100644 index 0000000..ffcbc80 --- /dev/null +++ b/acia6551.go @@ -0,0 +1,28 @@ +package i6502 + +/* +ACIA 6551 Serial IO +*/ +type Acia6551 struct { + rx chan byte // Reading (Acia Input) line + tx chan byte // Transmitting (Acia Output) line +} + +func NewAcia6551(rx chan byte, tx chan byte) (*Acia6551, error) { + return &Acia6551{tx: tx, rx: rx}, nil +} + +func (r *Acia6551) Size() uint16 { + // We have a only 4 addresses, RX, TX, Command and Control + return 0x04 +} + +/* +func (r *Rom) Read(address uint16) byte { + return r.data[address] +} + +func (r *Rom) Write(address uint16, data byte) { + panic(fmt.Errorf("Trying to write to ROM at 0x%04X", address)) +} +*/ diff --git a/acia6551_test.go b/acia6551_test.go new file mode 100644 index 0000000..9e19e15 --- /dev/null +++ b/acia6551_test.go @@ -0,0 +1,15 @@ +package i6502 + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNewAcia6551(t *testing.T) { + tx := make(chan byte) + rx := make(chan byte) + acia, err := NewAcia6551(rx, tx) + + assert.Nil(t, err) + assert.Equal(t, 0x4, acia.Size()) +} From 856ff0057316987f85fcae545ee5836334835342 Mon Sep 17 00:00:00 2001 From: Ariejan de Vroom Date: Sun, 17 Aug 2014 17:18:14 +0200 Subject: [PATCH 2/7] Add ACIA Reset() --- acia6551.go | 50 +++++++++++++++++++++++++++++++++++++++++++----- acia6551_test.go | 25 ++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/acia6551.go b/acia6551.go index ffcbc80..3bdbe2f 100644 --- a/acia6551.go +++ b/acia6551.go @@ -1,22 +1,62 @@ package i6502 +const ( + aciaData = iota + aciaStatus + aciaCommand + aciaControl +) + /* ACIA 6551 Serial IO */ type Acia6551 struct { - rx chan byte // Reading (Acia Input) line - tx chan byte // Transmitting (Acia Output) line + Rx chan byte // Reading (Acia Input) line + Tx chan byte // Transmitting (Acia Output) line + + rxData byte + txData byte + commandData byte + controlData byte + + rxFull bool + txEmpty bool + + rxIrqEnabled bool + txIrqEnabled bool } func NewAcia6551(rx chan byte, tx chan byte) (*Acia6551, error) { - return &Acia6551{tx: tx, rx: rx}, nil + acia := &Acia6551{Tx: tx, Rx: rx} + acia.Reset() + + go func() { + // Handle rx data channel + }() + + go func() { + // Handle tx data channel + }() + + return acia, nil } -func (r *Acia6551) Size() uint16 { - // We have a only 4 addresses, RX, TX, Command and Control +func (a *Acia6551) Size() uint16 { + // We have a only 4 addresses, Data, Status, Command and Control return 0x04 } +func (a *Acia6551) Reset() { + a.rxData = 0 + a.rxFull = false + + a.txData = 0 + a.txEmpty = true + + a.rxIrqEnabled = false + a.txIrqEnabled = false +} + /* func (r *Rom) Read(address uint16) byte { return r.data[address] diff --git a/acia6551_test.go b/acia6551_test.go index 9e19e15..8763d79 100644 --- a/acia6551_test.go +++ b/acia6551_test.go @@ -5,6 +5,14 @@ import ( "testing" ) +func AciaSubject() *Acia6551 { + tx := make(chan byte) + rx := make(chan byte) + acia, _ := NewAcia6551(rx, tx) + + return acia +} + func TestNewAcia6551(t *testing.T) { tx := make(chan byte) rx := make(chan byte) @@ -13,3 +21,20 @@ func TestNewAcia6551(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 0x4, acia.Size()) } + +func TestAciaReset(t *testing.T) { + a := AciaSubject() + a.Reset() + + assert.Equal(t, a.txData, 0) + assert.True(t, a.txEmpty) + + assert.Equal(t, a.rxData, 0) + assert.False(t, a.rxFull) + + assert.False(t, a.txIrqEnabled) + assert.False(t, a.rxIrqEnabled) +} + +func TestAciaCommand(t *testing.T) { +} From 44738e191f4fa00e00d21fb8a07507751a0200e9 Mon Sep 17 00:00:00 2001 From: Ariejan de Vroom Date: Sun, 17 Aug 2014 22:36:01 +0200 Subject: [PATCH 3/7] WIP --- acia6551.go | 49 ++++++++++++++++++++++++++++++++++++++++-------- acia6551_test.go | 3 +++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/acia6551.go b/acia6551.go index 3bdbe2f..6f04540 100644 --- a/acia6551.go +++ b/acia6551.go @@ -14,8 +14,9 @@ type Acia6551 struct { Rx chan byte // Reading (Acia Input) line Tx chan byte // Transmitting (Acia Output) line - rxData byte - txData byte + rxData byte + txData byte + commandData byte controlData byte @@ -24,6 +25,8 @@ type Acia6551 struct { rxIrqEnabled bool txIrqEnabled bool + + overrun bool } func NewAcia6551(rx chan byte, tx chan byte) (*Acia6551, error) { @@ -46,6 +49,7 @@ func (a *Acia6551) Size() uint16 { return 0x04 } +// Emulates a hardware reset func (a *Acia6551) Reset() { a.rxData = 0 a.rxFull = false @@ -55,14 +59,43 @@ func (a *Acia6551) Reset() { a.rxIrqEnabled = false a.txIrqEnabled = false + + a.overrun = false + + a.setControl(0) + a.setCommand(0) } -/* -func (r *Rom) Read(address uint16) byte { - return r.data[address] +func (a *Acia6551) setControl(data byte) { } -func (r *Rom) Write(address uint16, data byte) { - panic(fmt.Errorf("Trying to write to ROM at 0x%04X", address)) +func (a *Acia6551) setCommand(data byte) { +} + +func (a *Acia6551) Read(address uint16) byte { + switch address { + case aciaData: + // Read Rx + case aciaStatus: + // Read Status reg. + case aciaCommand: + // Read command + case aciaControl: + // Read control + } + + return 0x00 +} + +func (a *Acia6551) Write(address uint16, data byte) { + switch address { + case aciaData: + // Write Tx + case aciaStatus: + // Reset + case aciaCommand: + // Write command + case aciaControl: + // Write control + } } -*/ diff --git a/acia6551_test.go b/acia6551_test.go index 8763d79..d62f86d 100644 --- a/acia6551_test.go +++ b/acia6551_test.go @@ -34,6 +34,9 @@ func TestAciaReset(t *testing.T) { assert.False(t, a.txIrqEnabled) assert.False(t, a.rxIrqEnabled) + + assert.False(t, a.overrun) + assert.Equal(t, 0, a.controlData) } func TestAciaCommand(t *testing.T) { From 432119f5077931264c452216ab16ed860b7d9112 Mon Sep 17 00:00:00 2001 From: Ariejan de Vroom Date: Mon, 18 Aug 2014 16:37:51 +0200 Subject: [PATCH 4/7] Add failing spec with chanels. --- acia6551.go | 20 +++++++++++++++++++- acia6551_test.go | 20 ++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/acia6551.go b/acia6551.go index 6f04540..3cd4a64 100644 --- a/acia6551.go +++ b/acia6551.go @@ -1,5 +1,7 @@ package i6502 +import "fmt" + const ( aciaData = iota aciaStatus @@ -9,6 +11,14 @@ const ( /* ACIA 6551 Serial IO + +This Asynchronous Communications Interface Adapater can be +directly attached to the 6502's address and data busses. + +It provides serial IO. + +The supplied Rx and Tx channels can be used to read and wirte +data to the ACIA 6551. */ type Acia6551 struct { Rx chan byte // Reading (Acia Input) line @@ -34,7 +44,14 @@ func NewAcia6551(rx chan byte, tx chan byte) (*Acia6551, error) { acia.Reset() go func() { - // Handle rx data channel + for { + select { + case data := <-acia.Rx: + acia.rxData = data + acia.rxFull = true + fmt.Printf("Rx: 0x%02X\n", data) + } + } }() go func() { @@ -72,6 +89,7 @@ func (a *Acia6551) setControl(data byte) { func (a *Acia6551) setCommand(data byte) { } +// Used by the AddressBus to read data from the ACIA 6551 func (a *Acia6551) Read(address uint16) byte { switch address { case aciaData: diff --git a/acia6551_test.go b/acia6551_test.go index d62f86d..c0f0226 100644 --- a/acia6551_test.go +++ b/acia6551_test.go @@ -1,16 +1,17 @@ package i6502 import ( + "fmt" "github.com/stretchr/testify/assert" "testing" ) -func AciaSubject() *Acia6551 { +func AciaSubject() (*Acia6551, chan byte, chan byte) { tx := make(chan byte) rx := make(chan byte) acia, _ := NewAcia6551(rx, tx) - return acia + return acia, rx, tx } func TestNewAcia6551(t *testing.T) { @@ -23,7 +24,8 @@ func TestNewAcia6551(t *testing.T) { } func TestAciaReset(t *testing.T) { - a := AciaSubject() + a, _, _ := AciaSubject() + a.Reset() assert.Equal(t, a.txData, 0) @@ -39,5 +41,15 @@ func TestAciaReset(t *testing.T) { assert.Equal(t, 0, a.controlData) } -func TestAciaCommand(t *testing.T) { +func TestAciaReadData(t *testing.T) { + a, _, _ := AciaSubject() + + a.Rx <- 0x42 + + assert.True(t, a.rxFull) + + fmt.Printf("Reading...\n") + value := a.Read(aciaData) + assert.Equal(t, 0x42, value) + assert.False(t, a.rxFull) } From 099d6c11bdca1dd0dac4b76d9b9cdf6d9bff57fa Mon Sep 17 00:00:00 2001 From: Ariejan de Vroom Date: Tue, 19 Aug 2014 16:40:31 +0200 Subject: [PATCH 5/7] Implement acia 6551 --- acia6551.go | 118 ++++++++++++++++++++++++++++++++--------------- acia6551_test.go | 110 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 170 insertions(+), 58 deletions(-) diff --git a/acia6551.go b/acia6551.go index 3cd4a64..a264a9a 100644 --- a/acia6551.go +++ b/acia6551.go @@ -1,7 +1,5 @@ package i6502 -import "fmt" - const ( aciaData = iota aciaStatus @@ -21,11 +19,8 @@ The supplied Rx and Tx channels can be used to read and wirte data to the ACIA 6551. */ type Acia6551 struct { - Rx chan byte // Reading (Acia Input) line - Tx chan byte // Transmitting (Acia Output) line - - rxData byte - txData byte + rx byte + tx byte commandData byte controlData byte @@ -39,25 +34,10 @@ type Acia6551 struct { overrun bool } -func NewAcia6551(rx chan byte, tx chan byte) (*Acia6551, error) { - acia := &Acia6551{Tx: tx, Rx: rx} +func NewAcia6551() (*Acia6551, error) { + acia := &Acia6551{} acia.Reset() - go func() { - for { - select { - case data := <-acia.Rx: - acia.rxData = data - acia.rxFull = true - fmt.Printf("Rx: 0x%02X\n", data) - } - } - }() - - go func() { - // Handle tx data channel - }() - return acia, nil } @@ -68,10 +48,10 @@ func (a *Acia6551) Size() uint16 { // Emulates a hardware reset func (a *Acia6551) Reset() { - a.rxData = 0 + a.rx = 0 a.rxFull = false - a.txData = 0 + a.tx = 0 a.txEmpty = true a.rxIrqEnabled = false @@ -84,36 +64,102 @@ func (a *Acia6551) Reset() { } func (a *Acia6551) setControl(data byte) { + a.controlData = data } func (a *Acia6551) setCommand(data byte) { + a.commandData = data + + a.rxIrqEnabled = (data & 0x02) != 0 + a.txIrqEnabled = ((data & 0x04) != 0) && ((data & 0x08) != 1) +} + +func (a *Acia6551) statusRegister() byte { + status := byte(0) + + if a.rxFull { + status |= 0x08 + } + + if a.txEmpty { + status |= 0x10 + } + + if a.overrun { + status |= 0x04 + } + + return status +} + +// Implements io.Reader, for external programs to read TX'ed data from +// the serial output. +func (a *Acia6551) Read(p []byte) (n int, err error) { + a.txEmpty = true + copy(p, []byte{a.tx}) + // TODO: Handle txInterrupt + return 1, nil +} + +// Implements io.Writer, for external programs to write to the +// ACIA's RX +func (a *Acia6551) Write(p []byte) (n int, err error) { + for _, b := range p { + a.rxWrite(b) + } + + return len(p), nil } // Used by the AddressBus to read data from the ACIA 6551 -func (a *Acia6551) Read(address uint16) byte { +func (a *Acia6551) ReadByte(address uint16) byte { switch address { case aciaData: - // Read Rx + return a.rxRead() case aciaStatus: - // Read Status reg. + return a.statusRegister() case aciaCommand: - // Read command + return a.commandData case aciaControl: - // Read control + return a.controlData } return 0x00 } -func (a *Acia6551) Write(address uint16, data byte) { +// Used by the AddressBus to write data to the ACIA 6551 +func (a *Acia6551) WriteByte(address uint16, data byte) { switch address { case aciaData: - // Write Tx + a.txWrite(data) case aciaStatus: - // Reset + a.Reset() case aciaCommand: - // Write command + a.setCommand(data) case aciaControl: - // Write control + a.setControl(data) } } + +func (a *Acia6551) rxRead() byte { + a.overrun = false + a.rxFull = false + return a.rx +} + +func (a *Acia6551) rxWrite(data byte) { + // Oh no, overrun. Set the appropriate status + if a.rxFull { + a.overrun = true + } + + a.rx = data + a.rxFull = true + + // TODO: Interrupts +} + +func (a *Acia6551) txWrite(data byte) { + a.tx = data + a.txEmpty = false +} diff --git a/acia6551_test.go b/acia6551_test.go index c0f0226..f540e8e 100644 --- a/acia6551_test.go +++ b/acia6551_test.go @@ -1,37 +1,32 @@ package i6502 import ( - "fmt" - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) -func AciaSubject() (*Acia6551, chan byte, chan byte) { - tx := make(chan byte) - rx := make(chan byte) - acia, _ := NewAcia6551(rx, tx) - - return acia, rx, tx +func AciaSubject() *Acia6551 { + acia, _ := NewAcia6551() + return acia } func TestNewAcia6551(t *testing.T) { - tx := make(chan byte) - rx := make(chan byte) - acia, err := NewAcia6551(rx, tx) + acia, err := NewAcia6551() assert.Nil(t, err) assert.Equal(t, 0x4, acia.Size()) } func TestAciaReset(t *testing.T) { - a, _, _ := AciaSubject() + a := AciaSubject() a.Reset() - assert.Equal(t, a.txData, 0) + assert.Equal(t, a.tx, 0) assert.True(t, a.txEmpty) - assert.Equal(t, a.rxData, 0) + assert.Equal(t, a.rx, 0) assert.False(t, a.rxFull) assert.False(t, a.txIrqEnabled) @@ -41,15 +36,86 @@ func TestAciaReset(t *testing.T) { assert.Equal(t, 0, a.controlData) } -func TestAciaReadData(t *testing.T) { - a, _, _ := AciaSubject() +func TestAciaWriteByteAndReader(t *testing.T) { + a := AciaSubject() - a.Rx <- 0x42 + // CPU writes data + a.WriteByte(aciaData, 0x42) - assert.True(t, a.rxFull) + // System reads from Tx + value := make([]byte, 1) + bytesRead, _ := a.Read(value) - fmt.Printf("Reading...\n") - value := a.Read(aciaData) - assert.Equal(t, 0x42, value) - assert.False(t, a.rxFull) + if assert.Equal(t, 1, bytesRead) { + assert.Equal(t, 0x42, value[0]) + } +} + +func TestAciaWriterAndReadByte(t *testing.T) { + a := AciaSubject() + + // System writes a single byte + bytesWritten, _ := a.Write([]byte{0x42}) + + if assert.Equal(t, 1, bytesWritten) { + assert.Equal(t, 0x42, a.ReadByte(aciaData)) + } + + // System writes multiple bytes + bytesWritten, _ = a.Write([]byte{0x42, 0x32, 0xAB}) + + if assert.Equal(t, 3, bytesWritten) { + assert.Equal(t, 0xAB, a.ReadByte(aciaData)) + } +} + +func TestAciaCommandRegister(t *testing.T) { + a := AciaSubject() + assert.False(t, a.rxIrqEnabled) + assert.False(t, a.txIrqEnabled) + + a.WriteByte(aciaCommand, 0x02) // b0000 0010 RX Irq enabled + assert.True(t, a.rxIrqEnabled) + assert.False(t, a.txIrqEnabled) + + a.WriteByte(aciaCommand, 0x04) // b0000 0100 TX Irq enabled + assert.False(t, a.rxIrqEnabled) + assert.True(t, a.txIrqEnabled) + + a.WriteByte(aciaCommand, 0x06) // b0000 0110 RX + TX Irq enabled + assert.True(t, a.rxIrqEnabled) + assert.True(t, a.txIrqEnabled) + + assert.Equal(t, 0x06, a.ReadByte(aciaCommand)) +} + +func TestAciaControlRegister(t *testing.T) { + a := AciaSubject() + + a.WriteByte(aciaControl, 0xB8) + assert.Equal(t, 0xB8, a.ReadByte(aciaControl)) +} + +func TestAciaStatusRegister(t *testing.T) { + a := AciaSubject() + + a.rxFull = false + a.txEmpty = false + a.overrun = false + assert.Equal(t, 0x00, a.ReadByte(aciaStatus)) + + a.rxFull = true + a.txEmpty = false + a.overrun = false + assert.Equal(t, 0x08, a.ReadByte(aciaStatus)) + + a.rxFull = false + a.txEmpty = true + a.overrun = false + assert.Equal(t, 0x10, a.ReadByte(aciaStatus)) + + a.rxFull = false + a.txEmpty = false + a.overrun = true + assert.Equal(t, 0x04, a.ReadByte(aciaStatus)) } From da43c2bca1068a47dc93a87f357a5659014ea5f5 Mon Sep 17 00:00:00 2001 From: Ariejan de Vroom Date: Tue, 19 Aug 2014 16:49:46 +0200 Subject: [PATCH 6/7] Update Memory interface Because io.Reader and io.Writer already claim the functions Read and Write it was necessary to rename the Memory interface methods Read and Write to ReadByte and WriteByte. --- address_bus.go | 16 +-- address_bus_test.go | 27 ++-- cpu.go | 44 +++--- cpu_test.go | 341 ++++++++++++++++++++++---------------------- memory.go | 4 +- ram.go | 4 +- ram_test.go | 7 +- 7 files changed, 223 insertions(+), 220 deletions(-) diff --git a/address_bus.go b/address_bus.go index dec8917..01b632f 100644 --- a/address_bus.go +++ b/address_bus.go @@ -62,13 +62,13 @@ Read an 8-bit value from Memory attached at the 16-bit address. This will panic if you try to read from an address that has no Memory attached. */ -func (a *AddressBus) Read(address uint16) byte { +func (a *AddressBus) ReadByte(address uint16) byte { addressable, err := a.addressableForAddress(address) if err != nil { panic(err) } - return addressable.memory.Read(address - addressable.start) + return addressable.memory.ReadByte(address - addressable.start) } /* @@ -77,8 +77,8 @@ Convenience method to quickly read a 16-bit value from address and address + 1. Note that we first read the LOW byte from address and then the HIGH byte from address + 1. */ func (a *AddressBus) Read16(address uint16) uint16 { - lo := uint16(a.Read(address)) - hi := uint16(a.Read(address + 1)) + lo := uint16(a.ReadByte(address)) + hi := uint16(a.ReadByte(address + 1)) return (hi << 8) | lo } @@ -89,13 +89,13 @@ Write an 8-bit value to the Memory at the 16-bit address. This will panic if you try to write to an address that has no Memory attached or Memory that is read-only, like Rom. */ -func (a *AddressBus) Write(address uint16, data byte) { +func (a *AddressBus) WriteByte(address uint16, data byte) { addressable, err := a.addressableForAddress(address) if err != nil { panic(err) } - addressable.memory.Write(address-addressable.start, data) + addressable.memory.WriteByte(address-addressable.start, data) } /* @@ -104,8 +104,8 @@ Convenience method to quickly write a 16-bit value to address and address + 1. Note that the LOW byte will be stored in address and the high byte in address + 1. */ func (a *AddressBus) Write16(address uint16, data uint16) { - a.Write(address, byte(data)) - a.Write(address+1, byte(data>>8)) + a.WriteByte(address, byte(data)) + a.WriteByte(address+1, byte(data>>8)) } // Returns the addressable for the specified address, or an error if no addressable exists. diff --git a/address_bus_test.go b/address_bus_test.go index 5158435..bfcd8fc 100644 --- a/address_bus_test.go +++ b/address_bus_test.go @@ -1,8 +1,9 @@ package i6502 import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestEmptyAddressBus(t *testing.T) { @@ -37,29 +38,29 @@ func TestBusReadWrite(t *testing.T) { bus.Attach(ram2, 0x8000) // 8-bit Writing - bus.Write(0x1234, 0xFA) - assert.Equal(0xFA, ram.Read(0x1234)) + bus.WriteByte(0x1234, 0xFA) + assert.Equal(0xFA, ram.ReadByte(0x1234)) // 16-bit Writing bus.Write16(0x1000, 0xAB42) - assert.Equal(0x42, ram.Read(0x1000)) - assert.Equal(0xAB, ram.Read(0x1001)) + assert.Equal(0x42, ram.ReadByte(0x1000)) + assert.Equal(0xAB, ram.ReadByte(0x1001)) // 8-bit Reading - ram.Write(0x5522, 0xDA) - assert.Equal(0xDA, bus.Read(0x5522)) + ram.WriteByte(0x5522, 0xDA) + assert.Equal(0xDA, bus.ReadByte(0x5522)) // 16-bit Reading - ram.Write(0x4440, 0x7F) - ram.Write(0x4441, 0x56) + ram.WriteByte(0x4440, 0x7F) + ram.WriteByte(0x4441, 0x56) assert.Equal(0x567F, bus.Read16(0x4440)) //// Test addressing memory not mounted at 0x0000 // Read from relative addressable Ram2: $C123 - ram2.Write(0x4123, 0xEF) - assert.Equal(0xEF, bus.Read(0xC123)) + ram2.WriteByte(0x4123, 0xEF) + assert.Equal(0xEF, bus.ReadByte(0xC123)) - bus.Write(0x8001, 0x12) - assert.Equal(0x12, ram2.Read(0x0001)) + bus.WriteByte(0x8001, 0x12) + assert.Equal(0x12, ram2.ReadByte(0x0001)) } diff --git a/cpu.go b/cpu.go index a53d5cf..c7f7bec 100644 --- a/cpu.go +++ b/cpu.go @@ -90,7 +90,7 @@ func (c *Cpu) handleIrq(PC uint16) { // and point the Program Counter to the beginning of the program. func (c *Cpu) LoadProgram(data []byte, location uint16) { for i, b := range data { - c.Bus.Write(location+uint16(i), b) + c.Bus.WriteByte(location+uint16(i), b) } c.PC = location @@ -158,13 +158,13 @@ func (c *Cpu) execute(instruction Instruction) { c.setA(c.A ^ value) case sta: address := c.memoryAddress(instruction) - c.Bus.Write(address, c.A) + c.Bus.WriteByte(address, c.A) case stx: address := c.memoryAddress(instruction) - c.Bus.Write(address, c.X) + c.Bus.WriteByte(address, c.X) case sty: address := c.memoryAddress(instruction) - c.Bus.Write(address, c.Y) + c.Bus.WriteByte(address, c.Y) case tax: c.setX(c.A) case tay: @@ -264,7 +264,7 @@ func (c *Cpu) execute(instruction Instruction) { func (c *Cpu) readNextInstruction() Instruction { // Read the opcode - opcode := c.Bus.Read(c.PC) + opcode := c.Bus.ReadByte(c.PC) optype, ok := opTypes[opcode] if !ok { @@ -275,7 +275,7 @@ func (c *Cpu) readNextInstruction() Instruction { switch instruction.Size { case 1: // Zero operand instruction case 2: // 8-bit operand - instruction.Op8 = c.Bus.Read(c.PC + 1) + instruction.Op8 = c.Bus.ReadByte(c.PC + 1) case 3: // 16-bit operand instruction.Op16 = c.Bus.Read16(c.PC + 1) } @@ -297,7 +297,7 @@ func (c *Cpu) resolveOperand(in Instruction) uint8 { case immediate: return in.Op8 default: - return c.Bus.Read(c.memoryAddress(in)) + return c.Bus.ReadByte(c.memoryAddress(in)) } } @@ -356,17 +356,17 @@ func (c *Cpu) sbc(in Instruction) { func (c *Cpu) inc(in Instruction) { address := c.memoryAddress(in) - value := c.Bus.Read(address) + 1 + value := c.Bus.ReadByte(address) + 1 - c.Bus.Write(address, value) + c.Bus.WriteByte(address, value) c.setArithmeticFlags(value) } func (c *Cpu) dec(in Instruction) { address := c.memoryAddress(in) - value := c.Bus.Read(address) - 1 + value := c.Bus.ReadByte(address) - 1 - c.Bus.Write(address, value) + c.Bus.WriteByte(address, value) c.setArithmeticFlags(value) } @@ -378,10 +378,10 @@ func (c *Cpu) asl(in Instruction) { c.setArithmeticFlags(c.A) default: address := c.memoryAddress(in) - value := c.Bus.Read(address) + value := c.Bus.ReadByte(address) c.setCarry((value >> 7) == 1) value <<= 1 - c.Bus.Write(address, value) + c.Bus.WriteByte(address, value) c.setArithmeticFlags(value) } } @@ -394,10 +394,10 @@ func (c *Cpu) lsr(in Instruction) { c.setArithmeticFlags(c.A) default: address := c.memoryAddress(in) - value := c.Bus.Read(address) + value := c.Bus.ReadByte(address) c.setCarry((value & 0x01) == 1) value >>= 1 - c.Bus.Write(address, value) + c.Bus.WriteByte(address, value) c.setArithmeticFlags(value) } } @@ -412,10 +412,10 @@ func (c *Cpu) rol(in Instruction) { c.setArithmeticFlags(c.A) default: address := c.memoryAddress(in) - value := c.Bus.Read(address) + value := c.Bus.ReadByte(address) c.setCarry((value & 0x80) != 0) value = value<<1 | carry - c.Bus.Write(address, value) + c.Bus.WriteByte(address, value) c.setArithmeticFlags(value) } } @@ -430,10 +430,10 @@ func (c *Cpu) ror(in Instruction) { c.setArithmeticFlags(c.A) default: address := c.memoryAddress(in) - value := c.Bus.Read(address) + value := c.Bus.ReadByte(address) c.setCarry(value&0x01 == 1) value = value>>1 | carry<<7 - c.Bus.Write(address, value) + c.Bus.WriteByte(address, value) c.setArithmeticFlags(value) } } @@ -513,15 +513,15 @@ func (c *Cpu) sbcDecimal(a uint8, b uint8, carryIn uint8) { } func (c *Cpu) stackPush(data byte) { - c.Bus.Write(StackBase+uint16(c.SP), data) + c.Bus.WriteByte(StackBase+uint16(c.SP), data) c.SP -= 1 } func (c *Cpu) stackPeek() byte { - return c.Bus.Read(StackBase + uint16(c.SP+1)) + return c.Bus.ReadByte(StackBase + uint16(c.SP+1)) } func (c *Cpu) stackPop() byte { c.SP += 1 - return c.Bus.Read(StackBase + uint16(c.SP)) + return c.Bus.ReadByte(StackBase + uint16(c.SP)) } diff --git a/cpu_test.go b/cpu_test.go index f4bfccc..9e2ac0e 100644 --- a/cpu_test.go +++ b/cpu_test.go @@ -2,9 +2,10 @@ package i6502 import ( "fmt" - "github.com/stretchr/testify/assert" "io/ioutil" "testing" + + "github.com/stretchr/testify/assert" ) // Creates a new machine, returning the different parts @@ -45,8 +46,8 @@ func TestStackPushPopPeek(t *testing.T) { cpu.stackPush(0xA0) assert.Equal(0xFD, cpu.SP) - assert.Equal(0x42, cpu.Bus.Read(0x1FF)) - assert.Equal(0xA0, cpu.Bus.Read(0x1FE)) + assert.Equal(0x42, cpu.Bus.ReadByte(0x1FF)) + assert.Equal(0xA0, cpu.Bus.ReadByte(0x1FE)) peekValue := cpu.stackPeek() assert.Equal(0xFD, cpu.SP) @@ -100,9 +101,9 @@ func TestCpuInterrupt(t *testing.T) { cpu.Interrupt() assert.Equal(t, 0x1234, cpu.PC) - assert.Equal(t, 0x03, cpu.Bus.Read(0x01FF)) - assert.Equal(t, 0x80, cpu.Bus.Read(0x01FE)) - assert.Equal(t, status, cpu.Bus.Read(0x01FD)) + assert.Equal(t, 0x03, cpu.Bus.ReadByte(0x01FF)) + assert.Equal(t, 0x80, cpu.Bus.ReadByte(0x01FE)) + assert.Equal(t, status, cpu.Bus.ReadByte(0x01FD)) assert.True(t, cpu.getIrqDisable()) } @@ -114,9 +115,9 @@ func TestProgramLoading(t *testing.T) { cpu, bus, _ := NewRamMachine() cpu.LoadProgram(program, 0x0300) - assert.Equal(0xEA, bus.Read(0x0300)) - assert.Equal(0xEB, bus.Read(0x0301)) - assert.Equal(0xEC, bus.Read(0x0302)) + assert.Equal(0xEA, bus.ReadByte(0x0300)) + assert.Equal(0xEB, bus.ReadByte(0x0301)) + assert.Equal(0xEC, bus.ReadByte(0x0302)) assert.Equal(0x0300, cpu.PC) } @@ -295,7 +296,7 @@ func TestADCZeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x65, 0x53}, 0x0300) cpu.A = 0x42 - cpu.Bus.Write(0x53, 0x12) + cpu.Bus.WriteByte(0x53, 0x12) cpu.Step() @@ -308,7 +309,7 @@ func TestADCZeropageX(t *testing.T) { cpu.LoadProgram([]byte{0x75, 0x53}, 0x0300) cpu.A = 0x42 cpu.X = 0x01 - cpu.Bus.Write(0x54, 0x12) + cpu.Bus.WriteByte(0x54, 0x12) cpu.Step() @@ -320,7 +321,7 @@ func TestADCAbsolute(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x6D, 0x00, 0x80}, 0x0300) cpu.A = 0x42 - cpu.Bus.Write(0x8000, 0x12) + cpu.Bus.WriteByte(0x8000, 0x12) cpu.Step() @@ -333,7 +334,7 @@ func TestADCAbsoluteX(t *testing.T) { cpu.LoadProgram([]byte{0x7D, 0x00, 0x80}, 0x0300) cpu.A = 0x42 cpu.X = 0x02 - cpu.Bus.Write(0x8002, 0x12) + cpu.Bus.WriteByte(0x8002, 0x12) cpu.Step() @@ -346,7 +347,7 @@ func TestADCAbsoluteY(t *testing.T) { cpu.LoadProgram([]byte{0x79, 0x00, 0x80}, 0x0300) cpu.A = 0x42 cpu.Y = 0x02 - cpu.Bus.Write(0x8002, 0x12) + cpu.Bus.WriteByte(0x8002, 0x12) cpu.Step() @@ -360,7 +361,7 @@ func TestADCIndirectX(t *testing.T) { cpu.A = 0x42 cpu.X = 0x02 cpu.Bus.Write16(0x82, 0xC000) - cpu.Bus.Write(0xC000, 0x12) + cpu.Bus.WriteByte(0xC000, 0x12) cpu.Step() @@ -374,7 +375,7 @@ func TestADCIndirectY(t *testing.T) { cpu.A = 0x42 cpu.Y = 0x02 cpu.Bus.Write16(0x80, 0xC000) - cpu.Bus.Write(0xC002, 0x12) + cpu.Bus.WriteByte(0xC002, 0x12) cpu.Step() @@ -454,7 +455,7 @@ func TestSBCZeropage(t *testing.T) { cpu.LoadProgram([]byte{0xE5, 0x53}, 0x0300) cpu.setCarry(true) cpu.A = 0x42 - cpu.Bus.Write(0x53, 0x12) + cpu.Bus.WriteByte(0x53, 0x12) cpu.Step() @@ -468,7 +469,7 @@ func TestZeropageX(t *testing.T) { cpu.setCarry(true) cpu.A = 0x42 cpu.X = 0x01 - cpu.Bus.Write(0x54, 0x12) + cpu.Bus.WriteByte(0x54, 0x12) cpu.Step() @@ -481,7 +482,7 @@ func TestSBCAbsolute(t *testing.T) { cpu.LoadProgram([]byte{0xED, 0x00, 0x80}, 0x0300) cpu.setCarry(true) cpu.A = 0x42 - cpu.Bus.Write(0x8000, 0x12) + cpu.Bus.WriteByte(0x8000, 0x12) cpu.Step() @@ -495,7 +496,7 @@ func TestSBCAbsoluteX(t *testing.T) { cpu.setCarry(true) cpu.A = 0x42 cpu.X = 0x02 - cpu.Bus.Write(0x8002, 0x12) + cpu.Bus.WriteByte(0x8002, 0x12) cpu.Step() @@ -509,7 +510,7 @@ func TestSBCAbsoluteY(t *testing.T) { cpu.setCarry(true) cpu.A = 0x42 cpu.Y = 0x02 - cpu.Bus.Write(0x8002, 0x12) + cpu.Bus.WriteByte(0x8002, 0x12) cpu.Step() @@ -524,7 +525,7 @@ func TestSBCIndirectX(t *testing.T) { cpu.A = 0x42 cpu.X = 0x02 cpu.Bus.Write16(0x82, 0xC000) - cpu.Bus.Write(0xC000, 0x12) + cpu.Bus.WriteByte(0xC000, 0x12) cpu.Step() @@ -539,7 +540,7 @@ func TestSBCIndirectY(t *testing.T) { cpu.A = 0x42 cpu.Y = 0x02 cpu.Bus.Write16(0x80, 0xC000) - cpu.Bus.Write(0xC002, 0x12) + cpu.Bus.WriteByte(0xC002, 0x12) cpu.Step() @@ -600,47 +601,47 @@ func TestINYRollover(t *testing.T) { func TestINCZeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xE6, 0x42}, 0x0300) - cpu.Bus.Write(0x42, 0x01) + cpu.Bus.WriteByte(0x42, 0x01) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x42)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x42)) } func TestINCZeropageX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xF6, 0x42}, 0x0300) cpu.X = 0x01 - cpu.Bus.Write(0x43, 0x01) + cpu.Bus.WriteByte(0x43, 0x01) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x43)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x43)) } func TestINCAbsolute(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xEE, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x01) + cpu.Bus.WriteByte(0x8000, 0x01) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x8000)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x8000)) } func TestINCAbsoluteX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xFE, 0x00, 0x80}, 0x0300) cpu.X = 0x02 - cpu.Bus.Write(0x8002, 0x01) + cpu.Bus.WriteByte(0x8002, 0x01) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x8002)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x8002)) } //// DEX @@ -696,47 +697,47 @@ func TestDEYRollover(t *testing.T) { func TestDECZeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xC6, 0x42}, 0x0300) - cpu.Bus.Write(0x42, 0x01) + cpu.Bus.WriteByte(0x42, 0x01) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x00, cpu.Bus.Read(0x42)) + assert.Equal(t, 0x00, cpu.Bus.ReadByte(0x42)) } func TestDECZeropageX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xD6, 0x42}, 0x0300) cpu.X = 0x01 - cpu.Bus.Write(0x43, 0x01) + cpu.Bus.WriteByte(0x43, 0x01) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x00, cpu.Bus.Read(0x43)) + assert.Equal(t, 0x00, cpu.Bus.ReadByte(0x43)) } func TestDECAbsolute(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xCE, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x01) + cpu.Bus.WriteByte(0x8000, 0x01) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x00, cpu.Bus.Read(0x8000)) + assert.Equal(t, 0x00, cpu.Bus.ReadByte(0x8000)) } func TestDECAbsoluteX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xDE, 0x00, 0x80}, 0x0300) cpu.X = 0x02 - cpu.Bus.Write(0x8002, 0x01) + cpu.Bus.WriteByte(0x8002, 0x01) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x00, cpu.Bus.Read(0x8002)) + assert.Equal(t, 0x00, cpu.Bus.ReadByte(0x8002)) } //// LDA @@ -776,7 +777,7 @@ func TestLDAZero(t *testing.T) { func TestLDAZeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xA5, 0x42}, 0x0300) - cpu.Bus.Write(0x42, 0xF8) + cpu.Bus.WriteByte(0x42, 0xF8) cpu.Step() @@ -788,7 +789,7 @@ func TestLDAZeropageX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xB5, 0x41}, 0x0300) cpu.X = 0x01 - cpu.Bus.Write(0x42, 0xF8) + cpu.Bus.WriteByte(0x42, 0xF8) cpu.Step() @@ -836,7 +837,7 @@ func TestLDAIndirectX(t *testing.T) { cpu.LoadProgram([]byte{0xA1, 0x80}, 0x0300) cpu.X = 0x02 cpu.Bus.Write16(0x82, 0xC000) - cpu.Bus.Write(0xC000, 0xF8) + cpu.Bus.WriteByte(0xC000, 0xF8) cpu.Step() @@ -849,7 +850,7 @@ func TestLDAIndirectY(t *testing.T) { cpu.LoadProgram([]byte{0xB1, 0x80}, 0x0300) cpu.Y = 0x02 cpu.Bus.Write16(0x80, 0xC000) - cpu.Bus.Write(0xC002, 0xF8) + cpu.Bus.WriteByte(0xC002, 0xF8) cpu.Step() @@ -894,7 +895,7 @@ func TestLDXZero(t *testing.T) { func TestLDXZeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xA6, 0x42}, 0x0300) - cpu.Bus.Write(0x42, 0xF8) + cpu.Bus.WriteByte(0x42, 0xF8) cpu.Step() @@ -906,7 +907,7 @@ func TestLDXZeropageY(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xB6, 0x41}, 0x0300) cpu.Y = 0x01 - cpu.Bus.Write(0x42, 0xF8) + cpu.Bus.WriteByte(0x42, 0xF8) cpu.Step() @@ -974,7 +975,7 @@ func TestLDYZero(t *testing.T) { func TestLDYZeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xA4, 0x42}, 0x0300) - cpu.Bus.Write(0x42, 0xF8) + cpu.Bus.WriteByte(0x42, 0xF8) cpu.Step() @@ -986,7 +987,7 @@ func TestLDYZeropageX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0xB4, 0x41}, 0x0300) cpu.X = 0x01 - cpu.Bus.Write(0x42, 0xF8) + cpu.Bus.WriteByte(0x42, 0xF8) cpu.Step() @@ -1058,7 +1059,7 @@ func TestORAZeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x05, 0x42}, 0x0300) cpu.A = 0xF0 - cpu.Bus.Write(0x0042, 0x02) + cpu.Bus.WriteByte(0x0042, 0x02) cpu.Step() @@ -1071,7 +1072,7 @@ func TestORAZeropageX(t *testing.T) { cpu.LoadProgram([]byte{0x15, 0x40}, 0x0300) cpu.A = 0xF0 cpu.X = 0x02 - cpu.Bus.Write(0x0042, 0x02) + cpu.Bus.WriteByte(0x0042, 0x02) cpu.Step() @@ -1083,7 +1084,7 @@ func TestORAAbsolute(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x0D, 0x00, 0x80}, 0x0300) cpu.A = 0xF0 - cpu.Bus.Write(0x8000, 0x02) + cpu.Bus.WriteByte(0x8000, 0x02) cpu.Step() @@ -1096,7 +1097,7 @@ func TestORAAbsoluteX(t *testing.T) { cpu.LoadProgram([]byte{0x1D, 0x00, 0x80}, 0x0300) cpu.A = 0xF0 cpu.X = 0x02 - cpu.Bus.Write(0x8002, 0x02) + cpu.Bus.WriteByte(0x8002, 0x02) cpu.Step() @@ -1109,7 +1110,7 @@ func TestORAAbsoluteY(t *testing.T) { cpu.LoadProgram([]byte{0x19, 0x00, 0x80}, 0x0300) cpu.A = 0xF0 cpu.Y = 0x02 - cpu.Bus.Write(0x8002, 0x02) + cpu.Bus.WriteByte(0x8002, 0x02) cpu.Step() @@ -1123,7 +1124,7 @@ func TestORAIndirectX(t *testing.T) { cpu.A = 0xF0 cpu.X = 0x02 cpu.Bus.Write16(0x42, 0xC000) - cpu.Bus.Write(0xC000, 0x02) + cpu.Bus.WriteByte(0xC000, 0x02) cpu.Step() @@ -1137,7 +1138,7 @@ func TestORAIndirectY(t *testing.T) { cpu.A = 0xF0 cpu.Y = 0x02 cpu.Bus.Write16(0x40, 0xC000) - cpu.Bus.Write(0xC002, 0x02) + cpu.Bus.WriteByte(0xC002, 0x02) cpu.Step() @@ -1186,7 +1187,7 @@ func TestANDZeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x25, 0x42}, 0x0300) cpu.A = 0xE9 - cpu.Bus.Write(0x0042, 0x0f) + cpu.Bus.WriteByte(0x0042, 0x0f) cpu.Step() @@ -1199,7 +1200,7 @@ func TestANDZeropageX(t *testing.T) { cpu.LoadProgram([]byte{0x35, 0x40}, 0x0300) cpu.A = 0x42 cpu.X = 0x02 - cpu.Bus.Write(0x0042, 0x0F) + cpu.Bus.WriteByte(0x0042, 0x0F) cpu.Step() @@ -1211,7 +1212,7 @@ func TestANDAbsolute(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x2D, 0x00, 0x80}, 0x0300) cpu.A = 0x42 - cpu.Bus.Write(0x8000, 0x0F) + cpu.Bus.WriteByte(0x8000, 0x0F) cpu.Step() @@ -1224,7 +1225,7 @@ func TestANDAbsoluteX(t *testing.T) { cpu.LoadProgram([]byte{0x3D, 0x00, 0x80}, 0x0300) cpu.A = 0x42 cpu.X = 0x02 - cpu.Bus.Write(0x8002, 0x0F) + cpu.Bus.WriteByte(0x8002, 0x0F) cpu.Step() @@ -1237,7 +1238,7 @@ func TestANDAbsoluteY(t *testing.T) { cpu.LoadProgram([]byte{0x39, 0x00, 0x80}, 0x0300) cpu.A = 0x42 cpu.Y = 0x02 - cpu.Bus.Write(0x8002, 0x0F) + cpu.Bus.WriteByte(0x8002, 0x0F) cpu.Step() @@ -1251,7 +1252,7 @@ func TestANDIndirectX(t *testing.T) { cpu.A = 0x42 cpu.X = 0x02 cpu.Bus.Write16(0x82, 0xC000) - cpu.Bus.Write(0xC000, 0x0F) + cpu.Bus.WriteByte(0xC000, 0x0F) cpu.Step() @@ -1265,7 +1266,7 @@ func TestANDIndirectY(t *testing.T) { cpu.A = 0x42 cpu.Y = 0x02 cpu.Bus.Write16(0x80, 0xC000) - cpu.Bus.Write(0xC002, 0x0F) + cpu.Bus.WriteByte(0xC002, 0x0F) cpu.Step() @@ -1314,7 +1315,7 @@ func TestEORZeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x45, 0x80}, 0x0300) cpu.A = 0x42 - cpu.Bus.Write(0x0080, 0x7f) + cpu.Bus.WriteByte(0x0080, 0x7f) cpu.Step() @@ -1327,7 +1328,7 @@ func TestEORZeropageX(t *testing.T) { cpu.LoadProgram([]byte{0x55, 0x80}, 0x0300) cpu.A = 0x42 cpu.X = 0x02 - cpu.Bus.Write(0x0082, 0x7F) + cpu.Bus.WriteByte(0x0082, 0x7F) cpu.Step() @@ -1339,7 +1340,7 @@ func TestEORAbsolute(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x4D, 0x00, 0x80}, 0x0300) cpu.A = 0x42 - cpu.Bus.Write(0x8000, 0x7F) + cpu.Bus.WriteByte(0x8000, 0x7F) cpu.Step() @@ -1352,7 +1353,7 @@ func TestEORAbsoluteX(t *testing.T) { cpu.LoadProgram([]byte{0x5D, 0x00, 0x80}, 0x0300) cpu.A = 0x42 cpu.X = 0x02 - cpu.Bus.Write(0x8002, 0x7F) + cpu.Bus.WriteByte(0x8002, 0x7F) cpu.Step() @@ -1365,7 +1366,7 @@ func TestEORAbsoluteY(t *testing.T) { cpu.LoadProgram([]byte{0x59, 0x00, 0x80}, 0x0300) cpu.A = 0x42 cpu.Y = 0x02 - cpu.Bus.Write(0x8002, 0x7F) + cpu.Bus.WriteByte(0x8002, 0x7F) cpu.Step() @@ -1379,7 +1380,7 @@ func TestEORIndirectX(t *testing.T) { cpu.A = 0x42 cpu.X = 0x02 cpu.Bus.Write16(0x82, 0xC000) - cpu.Bus.Write(0xC000, 0x7F) + cpu.Bus.WriteByte(0xC000, 0x7F) cpu.Step() @@ -1393,7 +1394,7 @@ func TestEORIndirectY(t *testing.T) { cpu.A = 0x42 cpu.Y = 0x02 cpu.Bus.Write16(0x80, 0xC000) - cpu.Bus.Write(0xC002, 0x7F) + cpu.Bus.WriteByte(0xC002, 0x7F) cpu.Step() @@ -1411,7 +1412,7 @@ func TestSTAZeropage(t *testing.T) { cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0x0080)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0x0080)) } func TestSTAZeropageX(t *testing.T) { @@ -1423,7 +1424,7 @@ func TestSTAZeropageX(t *testing.T) { cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0x0082)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0x0082)) } func TestSTAAbsolute(t *testing.T) { @@ -1434,7 +1435,7 @@ func TestSTAAbsolute(t *testing.T) { cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0x8000)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0x8000)) } func TestSTAAbsoluteX(t *testing.T) { @@ -1446,7 +1447,7 @@ func TestSTAAbsoluteX(t *testing.T) { cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0x8002)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0x8002)) } func TestSTAAbsoluteY(t *testing.T) { @@ -1454,12 +1455,12 @@ func TestSTAAbsoluteY(t *testing.T) { cpu.LoadProgram([]byte{0x99, 0x00, 0x80}, 0x0300) cpu.A = 0x42 cpu.Y = 0x02 - cpu.Bus.Write(0x8002, 0x7F) + cpu.Bus.WriteByte(0x8002, 0x7F) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0x8002)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0x8002)) } func TestSTAIndirectX(t *testing.T) { @@ -1472,7 +1473,7 @@ func TestSTAIndirectX(t *testing.T) { cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0xC000)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0xC000)) } func TestSTAIndirectY(t *testing.T) { @@ -1485,7 +1486,7 @@ func TestSTAIndirectY(t *testing.T) { cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0xC002)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0xC002)) } //// STX @@ -1498,7 +1499,7 @@ func TestSTXZeropage(t *testing.T) { cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0x0080)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0x0080)) } func TestSTXZeropageY(t *testing.T) { @@ -1510,7 +1511,7 @@ func TestSTXZeropageY(t *testing.T) { cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0x0082)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0x0082)) } func TestSTXAbsolute(t *testing.T) { @@ -1521,7 +1522,7 @@ func TestSTXAbsolute(t *testing.T) { cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0x8000)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0x8000)) } //// STY @@ -1534,7 +1535,7 @@ func TestSTYZeropage(t *testing.T) { cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0x0080)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0x0080)) } func TestSTYZeropageX(t *testing.T) { @@ -1546,7 +1547,7 @@ func TestSTYZeropageX(t *testing.T) { cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0x0082)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0x0082)) } func TestSTYAbsolute(t *testing.T) { @@ -1557,7 +1558,7 @@ func TestSTYAbsolute(t *testing.T) { cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x42, cpu.Bus.Read(0x8000)) + assert.Equal(t, 0x42, cpu.Bus.ReadByte(0x8000)) } //// TAX @@ -1810,47 +1811,47 @@ func TestASLAccumulatorCarry(t *testing.T) { func TestASLzeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x06, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x01) + cpu.Bus.WriteByte(0x0080, 0x01) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x0080)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x0080)) } func TestASLzeropageNegative(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x06, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x40) + cpu.Bus.WriteByte(0x0080, 0x40) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x80, cpu.Bus.Read(0x0080)) + assert.Equal(t, 0x80, cpu.Bus.ReadByte(0x0080)) assert.True(t, cpu.getNegative()) } func TestASLzeropageZero(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x06, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x80) + cpu.Bus.WriteByte(0x0080, 0x80) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x00, cpu.Bus.Read(0x0080)) + assert.Equal(t, 0x00, cpu.Bus.ReadByte(0x0080)) assert.True(t, cpu.getZero()) } func TestASLzeropageCarry(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x06, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0xAA) + cpu.Bus.WriteByte(0x0080, 0xAA) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x54, cpu.Bus.Read(0x0080)) + assert.Equal(t, 0x54, cpu.Bus.ReadByte(0x0080)) assert.True(t, cpu.getCarry()) } @@ -1858,35 +1859,35 @@ func TestASLzeropageX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x16, 0x80}, 0x0300) cpu.X = 0x02 - cpu.Bus.Write(0x0082, 0x01) + cpu.Bus.WriteByte(0x0082, 0x01) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x0082)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x0082)) } func TestASLabsolute(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x0E, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x01) + cpu.Bus.WriteByte(0x8000, 0x01) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x8000)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x8000)) } func TestASLabsoluteX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x1E, 0x00, 0x80}, 0x0300) cpu.X = 0x02 - cpu.Bus.Write(0x8002, 0x01) + cpu.Bus.WriteByte(0x8002, 0x01) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x8002)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x8002)) } //// LSR @@ -1929,35 +1930,35 @@ func TestLSRAccumulatorCarry(t *testing.T) { func TestLSRzeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x46, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x02) + cpu.Bus.WriteByte(0x0080, 0x02) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x01, cpu.Bus.Read(0x0080)) + assert.Equal(t, 0x01, cpu.Bus.ReadByte(0x0080)) } func TestLSRzeropageZero(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x46, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x01) + cpu.Bus.WriteByte(0x0080, 0x01) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x00, cpu.Bus.Read(0x0080)) + assert.Equal(t, 0x00, cpu.Bus.ReadByte(0x0080)) assert.True(t, cpu.getZero()) } func TestLSRzeropageCarry(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x46, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x01) + cpu.Bus.WriteByte(0x0080, 0x01) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x00, cpu.Bus.Read(0x0080)) + assert.Equal(t, 0x00, cpu.Bus.ReadByte(0x0080)) assert.True(t, cpu.getCarry()) } @@ -1965,35 +1966,35 @@ func TestLSRzeropageX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x56, 0x80}, 0x0300) cpu.X = 0x02 - cpu.Bus.Write(0x0082, 0x04) + cpu.Bus.WriteByte(0x0082, 0x04) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x0082)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x0082)) } func TestLSRabsolute(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x4E, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x04) + cpu.Bus.WriteByte(0x8000, 0x04) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x8000)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x8000)) } func TestLSRabsoluteX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x5E, 0x00, 0x80}, 0x0300) cpu.X = 0x02 - cpu.Bus.Write(0x8002, 0x04) + cpu.Bus.WriteByte(0x8002, 0x04) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x8002)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x8002)) } //// ROL @@ -2041,47 +2042,47 @@ func TestROLAccumulatorNegative(t *testing.T) { func TestROLZeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x26, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x01) + cpu.Bus.WriteByte(0x0080, 0x01) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x0080)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x0080)) } func TestROLZeropageX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x36, 0x80}, 0x0300) cpu.X = 0x02 - cpu.Bus.Write(0x0082, 0x01) + cpu.Bus.WriteByte(0x0082, 0x01) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x0082)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x0082)) } func TestROLAbsolute(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x2E, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x01) + cpu.Bus.WriteByte(0x8000, 0x01) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x8000)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x8000)) } func TestROLAbsoluteX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x3E, 0x00, 0x80}, 0x0300) cpu.X = 0x02 - cpu.Bus.Write(0x8002, 0x01) + cpu.Bus.WriteByte(0x8002, 0x01) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x02, cpu.Bus.Read(0x8002)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x8002)) } //// ROR @@ -2129,47 +2130,47 @@ func TestRORAccumulatorNegative(t *testing.T) { func TestRORZeropage(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x66, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x02) + cpu.Bus.WriteByte(0x0080, 0x02) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x01, cpu.Bus.Read(0x0080)) + assert.Equal(t, 0x01, cpu.Bus.ReadByte(0x0080)) } func TestRORZeropageX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x76, 0x80}, 0x0300) cpu.X = 0x02 - cpu.Bus.Write(0x0082, 0x02) + cpu.Bus.WriteByte(0x0082, 0x02) cpu.Step() assert.Equal(t, 0x0302, cpu.PC) - assert.Equal(t, 0x01, cpu.Bus.Read(0x0082)) + assert.Equal(t, 0x01, cpu.Bus.ReadByte(0x0082)) } func TestRORAbsolute(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x6E, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x02) + cpu.Bus.WriteByte(0x8000, 0x02) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x01, cpu.Bus.Read(0x8000)) + assert.Equal(t, 0x01, cpu.Bus.ReadByte(0x8000)) } func TestRORAbsoluteX(t *testing.T) { cpu, _, _ := NewRamMachine() cpu.LoadProgram([]byte{0x7E, 0x00, 0x80}, 0x0300) cpu.X = 0x02 - cpu.Bus.Write(0x8002, 0x02) + cpu.Bus.WriteByte(0x8002, 0x02) cpu.Step() assert.Equal(t, 0x0303, cpu.PC) - assert.Equal(t, 0x01, cpu.Bus.Read(0x8002)) + assert.Equal(t, 0x01, cpu.Bus.ReadByte(0x8002)) } /// CMP @@ -2213,7 +2214,7 @@ func TestCMPZeropage(t *testing.T) { // Equality cpu.LoadProgram([]byte{0xC5, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x42) + cpu.Bus.WriteByte(0x0080, 0x42) cpu.A = 0x42 cpu.Step() @@ -2224,7 +2225,7 @@ func TestCMPZeropage(t *testing.T) { // Greater Than cpu.LoadProgram([]byte{0xC5, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x42) + cpu.Bus.WriteByte(0x0080, 0x42) cpu.A = 0x43 cpu.Step() @@ -2235,7 +2236,7 @@ func TestCMPZeropage(t *testing.T) { // Less Than cpu.LoadProgram([]byte{0xC5, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x0A) + cpu.Bus.WriteByte(0x0080, 0x0A) cpu.A = 0x08 cpu.Step() @@ -2250,7 +2251,7 @@ func TestCMPZeropageX(t *testing.T) { // Equality cpu.LoadProgram([]byte{0xD5, 0x80}, 0x0300) - cpu.Bus.Write(0x0082, 0x42) + cpu.Bus.WriteByte(0x0082, 0x42) cpu.X = 0x02 cpu.A = 0x42 cpu.Step() @@ -2262,7 +2263,7 @@ func TestCMPZeropageX(t *testing.T) { // Greater Than cpu.LoadProgram([]byte{0xD5, 0x80}, 0x0300) - cpu.Bus.Write(0x0082, 0x42) + cpu.Bus.WriteByte(0x0082, 0x42) cpu.X = 0x02 cpu.A = 0x43 cpu.Step() @@ -2274,7 +2275,7 @@ func TestCMPZeropageX(t *testing.T) { // Less Than cpu.LoadProgram([]byte{0xD5, 0x80}, 0x0300) - cpu.Bus.Write(0x0082, 0x0A) + cpu.Bus.WriteByte(0x0082, 0x0A) cpu.X = 0x02 cpu.A = 0x08 cpu.Step() @@ -2290,7 +2291,7 @@ func TestCMPAbsolute(t *testing.T) { // Equality cpu.LoadProgram([]byte{0xCD, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x42) + cpu.Bus.WriteByte(0x8000, 0x42) cpu.A = 0x42 cpu.Step() @@ -2301,7 +2302,7 @@ func TestCMPAbsolute(t *testing.T) { // Greater Than cpu.LoadProgram([]byte{0xCD, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x42) + cpu.Bus.WriteByte(0x8000, 0x42) cpu.A = 0x43 cpu.Step() @@ -2312,7 +2313,7 @@ func TestCMPAbsolute(t *testing.T) { // Less Than cpu.LoadProgram([]byte{0xCD, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x0A) + cpu.Bus.WriteByte(0x8000, 0x0A) cpu.A = 0x08 cpu.Step() @@ -2327,7 +2328,7 @@ func TestCMPAbsoluteX(t *testing.T) { // Equality cpu.LoadProgram([]byte{0xDD, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8002, 0x42) + cpu.Bus.WriteByte(0x8002, 0x42) cpu.X = 0x02 cpu.A = 0x42 cpu.Step() @@ -2339,7 +2340,7 @@ func TestCMPAbsoluteX(t *testing.T) { // Greater Than cpu.LoadProgram([]byte{0xDD, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8002, 0x42) + cpu.Bus.WriteByte(0x8002, 0x42) cpu.X = 0x02 cpu.A = 0x43 cpu.Step() @@ -2351,7 +2352,7 @@ func TestCMPAbsoluteX(t *testing.T) { // Less Than cpu.LoadProgram([]byte{0xDD, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8002, 0x0A) + cpu.Bus.WriteByte(0x8002, 0x0A) cpu.X = 0x02 cpu.A = 0x08 cpu.Step() @@ -2367,7 +2368,7 @@ func TestCMPAbsoluteY(t *testing.T) { // Equality cpu.LoadProgram([]byte{0xD9, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8002, 0x42) + cpu.Bus.WriteByte(0x8002, 0x42) cpu.Y = 0x02 cpu.A = 0x42 cpu.Step() @@ -2379,7 +2380,7 @@ func TestCMPAbsoluteY(t *testing.T) { // Greater Than cpu.LoadProgram([]byte{0xD9, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8002, 0x42) + cpu.Bus.WriteByte(0x8002, 0x42) cpu.Y = 0x02 cpu.A = 0x43 cpu.Step() @@ -2391,7 +2392,7 @@ func TestCMPAbsoluteY(t *testing.T) { // Less Than cpu.LoadProgram([]byte{0xD9, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8002, 0x0A) + cpu.Bus.WriteByte(0x8002, 0x0A) cpu.Y = 0x02 cpu.A = 0x08 cpu.Step() @@ -2408,7 +2409,7 @@ func TestCMPIndirectX(t *testing.T) { // Equality cpu.LoadProgram([]byte{0xC1, 0x80}, 0x0300) cpu.Bus.Write16(0x0082, 0xC000) - cpu.Bus.Write(0xC000, 0x42) + cpu.Bus.WriteByte(0xC000, 0x42) cpu.X = 0x02 cpu.A = 0x42 cpu.Step() @@ -2421,7 +2422,7 @@ func TestCMPIndirectX(t *testing.T) { // Greater Than cpu.LoadProgram([]byte{0xC1, 0x80}, 0x0300) cpu.Bus.Write16(0x0082, 0xC000) - cpu.Bus.Write(0xC000, 0x42) + cpu.Bus.WriteByte(0xC000, 0x42) cpu.X = 0x02 cpu.A = 0x43 cpu.Step() @@ -2434,7 +2435,7 @@ func TestCMPIndirectX(t *testing.T) { // Less Than cpu.LoadProgram([]byte{0xC1, 0x80}, 0x0300) cpu.Bus.Write16(0x0082, 0xC000) - cpu.Bus.Write(0xC000, 0x0A) + cpu.Bus.WriteByte(0xC000, 0x0A) cpu.X = 0x02 cpu.A = 0x08 cpu.Step() @@ -2451,7 +2452,7 @@ func TestCMPIndirectY(t *testing.T) { // Equality cpu.LoadProgram([]byte{0xD1, 0x80}, 0x0300) cpu.Bus.Write16(0x0080, 0xC000) - cpu.Bus.Write(0xC002, 0x42) + cpu.Bus.WriteByte(0xC002, 0x42) cpu.Y = 0x02 cpu.A = 0x42 cpu.Step() @@ -2464,7 +2465,7 @@ func TestCMPIndirectY(t *testing.T) { // Greater Than cpu.LoadProgram([]byte{0xD1, 0x80}, 0x0300) cpu.Bus.Write16(0x0080, 0xC000) - cpu.Bus.Write(0xC002, 0x42) + cpu.Bus.WriteByte(0xC002, 0x42) cpu.X = 0x02 cpu.A = 0x43 cpu.Step() @@ -2477,7 +2478,7 @@ func TestCMPIndirectY(t *testing.T) { // Less Than cpu.LoadProgram([]byte{0xD1, 0x80}, 0x0300) cpu.Bus.Write16(0x0080, 0xC000) - cpu.Bus.Write(0xC002, 0x0A) + cpu.Bus.WriteByte(0xC002, 0x0A) cpu.X = 0x02 cpu.A = 0x08 cpu.Step() @@ -2529,7 +2530,7 @@ func TestCPXZeropage(t *testing.T) { // Equality cpu.LoadProgram([]byte{0xE4, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x42) + cpu.Bus.WriteByte(0x0080, 0x42) cpu.X = 0x42 cpu.Step() @@ -2540,7 +2541,7 @@ func TestCPXZeropage(t *testing.T) { // Greater Than cpu.LoadProgram([]byte{0xE4, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x42) + cpu.Bus.WriteByte(0x0080, 0x42) cpu.X = 0x43 cpu.Step() @@ -2551,7 +2552,7 @@ func TestCPXZeropage(t *testing.T) { // Less Than cpu.LoadProgram([]byte{0xE4, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x0A) + cpu.Bus.WriteByte(0x0080, 0x0A) cpu.X = 0x08 cpu.Step() @@ -2566,7 +2567,7 @@ func TestCPXAbsolute(t *testing.T) { // Equality cpu.LoadProgram([]byte{0xEC, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x42) + cpu.Bus.WriteByte(0x8000, 0x42) cpu.X = 0x42 cpu.Step() @@ -2577,7 +2578,7 @@ func TestCPXAbsolute(t *testing.T) { // Greater Than cpu.LoadProgram([]byte{0xEC, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x42) + cpu.Bus.WriteByte(0x8000, 0x42) cpu.X = 0x43 cpu.Step() @@ -2588,7 +2589,7 @@ func TestCPXAbsolute(t *testing.T) { // Less Than cpu.LoadProgram([]byte{0xEC, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x0A) + cpu.Bus.WriteByte(0x8000, 0x0A) cpu.X = 0x08 cpu.Step() @@ -2639,7 +2640,7 @@ func TestCPYZeropage(t *testing.T) { // Equality cpu.LoadProgram([]byte{0xC4, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x42) + cpu.Bus.WriteByte(0x0080, 0x42) cpu.Y = 0x42 cpu.Step() @@ -2650,7 +2651,7 @@ func TestCPYZeropage(t *testing.T) { // Greater Than cpu.LoadProgram([]byte{0xC4, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x42) + cpu.Bus.WriteByte(0x0080, 0x42) cpu.Y = 0x43 cpu.Step() @@ -2661,7 +2662,7 @@ func TestCPYZeropage(t *testing.T) { // Less Than cpu.LoadProgram([]byte{0xC4, 0x80}, 0x0300) - cpu.Bus.Write(0x0080, 0x0A) + cpu.Bus.WriteByte(0x0080, 0x0A) cpu.Y = 0x08 cpu.Step() @@ -2676,7 +2677,7 @@ func TestCPYAbsolute(t *testing.T) { // Equality cpu.LoadProgram([]byte{0xCC, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x42) + cpu.Bus.WriteByte(0x8000, 0x42) cpu.Y = 0x42 cpu.Step() @@ -2687,7 +2688,7 @@ func TestCPYAbsolute(t *testing.T) { // Greater Than cpu.LoadProgram([]byte{0xCC, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x42) + cpu.Bus.WriteByte(0x8000, 0x42) cpu.Y = 0x43 cpu.Step() @@ -2698,7 +2699,7 @@ func TestCPYAbsolute(t *testing.T) { // Less Than cpu.LoadProgram([]byte{0xCC, 0x00, 0x80}, 0x0300) - cpu.Bus.Write(0x8000, 0x0A) + cpu.Bus.WriteByte(0x8000, 0x0A) cpu.Y = 0x08 cpu.Step() @@ -2720,9 +2721,9 @@ func TestBRK(t *testing.T) { cpu.Step() assert.Equal(t, 0x1234, cpu.PC) - assert.Equal(t, 0x03, cpu.Bus.Read(0x01FF)) - assert.Equal(t, 0x02, cpu.Bus.Read(0x01FE)) - assert.Equal(t, status, cpu.Bus.Read(0x01FD)) + assert.Equal(t, 0x03, cpu.Bus.ReadByte(0x01FF)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x01FE)) + assert.Equal(t, status, cpu.Bus.ReadByte(0x01FD)) assert.True(t, cpu.getBreak()) } @@ -3004,9 +3005,9 @@ func TestBVS(t *testing.T) { func TestBITZeropage(t *testing.T) { cpu, _, _ := NewRamMachine() - cpu.Bus.Write(0x0000, 0xC0) - cpu.Bus.Write(0x0010, 0x40) - cpu.Bus.Write(0x0020, 0x80) + cpu.Bus.WriteByte(0x0000, 0xC0) + cpu.Bus.WriteByte(0x0010, 0x40) + cpu.Bus.WriteByte(0x0020, 0x80) cpu.LoadProgram([]byte{0x24, 0x00}, 0x0300) cpu.A = 0x01 @@ -3047,9 +3048,9 @@ func TestBITZeropage(t *testing.T) { func TestBITAbsolute(t *testing.T) { cpu, _, _ := NewRamMachine() - cpu.Bus.Write(0xC000, 0xC0) - cpu.Bus.Write(0xC010, 0x40) - cpu.Bus.Write(0xC020, 0x80) + cpu.Bus.WriteByte(0xC000, 0xC0) + cpu.Bus.WriteByte(0xC010, 0x40) + cpu.Bus.WriteByte(0xC020, 0x80) cpu.LoadProgram([]byte{0x2C, 0x00, 0xC0}, 0x0300) cpu.A = 0x01 @@ -3099,7 +3100,7 @@ func TestPHP(t *testing.T) { assert.Equal(t, 0x0301, cpu.PC) assert.Equal(t, 0xFE, cpu.SP) - assert.Equal(t, 0xB5, cpu.Bus.Read(0x01FF)) + assert.Equal(t, 0xB5, cpu.Bus.ReadByte(0x01FF)) } //// PLP @@ -3129,7 +3130,7 @@ func TestPHA(t *testing.T) { assert.Equal(t, 0x0301, cpu.PC) assert.Equal(t, 0xFE, cpu.SP) - assert.Equal(t, 0xB5, cpu.Bus.Read(0x01FF)) + assert.Equal(t, 0xB5, cpu.Bus.ReadByte(0x01FF)) } //// PLP @@ -3196,8 +3197,8 @@ func TestJSR(t *testing.T) { assert.Equal(t, 0xFD, cpu.SP) // We expect PC - 1 (e.g. 3rd byte of JSR) to be on the stack - assert.Equal(t, 0x03, cpu.Bus.Read(0x1FF)) - assert.Equal(t, 0x02, cpu.Bus.Read(0x1FE)) + assert.Equal(t, 0x03, cpu.Bus.ReadByte(0x1FF)) + assert.Equal(t, 0x02, cpu.Bus.ReadByte(0x1FE)) } //// RTS diff --git a/memory.go b/memory.go index d55c259..975e619 100644 --- a/memory.go +++ b/memory.go @@ -6,6 +6,6 @@ and become accessible by the Cpu. */ type Memory interface { Size() uint16 - Read(address uint16) byte - Write(address uint16, data byte) + ReadByte(address uint16) byte + WriteByte(address uint16, data byte) } diff --git a/ram.go b/ram.go index 7ceabbb..e9edcfa 100644 --- a/ram.go +++ b/ram.go @@ -16,10 +16,10 @@ func (r *Ram) Size() uint16 { return uint16(len(r.data)) } -func (r *Ram) Read(address uint16) byte { +func (r *Ram) ReadByte(address uint16) byte { return r.data[address] } -func (r *Ram) Write(address uint16, data byte) { +func (r *Ram) WriteByte(address uint16, data byte) { r.data[address] = data } diff --git a/ram_test.go b/ram_test.go index f958986..5262aa4 100644 --- a/ram_test.go +++ b/ram_test.go @@ -1,8 +1,9 @@ package i6502 import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestRamSize(t *testing.T) { @@ -18,6 +19,6 @@ func TestRamReadWrite(t *testing.T) { assert.Equal(t, 0x00, ram.data[i]) } - ram.Write(0x1000, 0x42) - assert.Equal(t, 0x42, ram.Read(0x1000)) + ram.WriteByte(0x1000, 0x42) + assert.Equal(t, 0x42, ram.ReadByte(0x1000)) } From 3ab4323f3b75adf395ba9a089d77502cbf653101 Mon Sep 17 00:00:00 2001 From: Ariejan de Vroom Date: Tue, 19 Aug 2014 22:01:41 +0200 Subject: [PATCH 7/7] Add tests for cpu/acia integration --- acia6551_test.go | 36 ++++++++++++++++++++++++++++++++++++ cpu.go | 6 ++++++ 2 files changed, 42 insertions(+) diff --git a/acia6551_test.go b/acia6551_test.go index f540e8e..8ce4889 100644 --- a/acia6551_test.go +++ b/acia6551_test.go @@ -119,3 +119,39 @@ func TestAciaStatusRegister(t *testing.T) { a.overrun = true assert.Equal(t, 0x04, a.ReadByte(aciaStatus)) } + +func TestAciaIntegration(t *testing.T) { + // Create a system + // * 32kB RAM at 0x0000-7FFFF + // * ACIA at 0x8800-8803 + ram, _ := NewRam(0x8000) + acia, _ := NewAcia6551() + bus, _ := NewAddressBus() + bus.Attach(ram, 0x0000) + bus.Attach(acia, 0x8800) + cpu, _ := NewCpu(bus) + + program := []byte{ + 0xA9, 0x00, // LDA #$00 + 0x8D, 0x01, 0x88, // STA AciaStatus (Reset) + 0xA9, 0x42, // LDA #$42 + 0x8D, 0x00, 0x88, // STA AciaData (Write) + 0xAD, 0x00, 0x88, // LDA AciaData (Read) + } + + cpu.LoadProgram(program, 0x0200) + cpu.Steps(2) + + acia.Write([]byte{0xAB}) + + cpu.Steps(3) + + value := make([]byte, 1) + bytesRead, _ := acia.Read(value) + + if assert.Equal(t, 1, bytesRead) { + assert.Equal(t, 0x42, value[0]) + } + + assert.Equal(t, 0xAB, cpu.A) +} diff --git a/cpu.go b/cpu.go index c7f7bec..d197e54 100644 --- a/cpu.go +++ b/cpu.go @@ -96,6 +96,12 @@ func (c *Cpu) LoadProgram(data []byte, location uint16) { c.PC = location } +func (c *Cpu) Steps(steps int) { + for i := 0; i < steps; i++ { + c.Step() + } +} + // Read and execute the instruction pointed to by the Program Counter (PC) func (c *Cpu) Step() { instruction := c.readNextInstruction()