Skip to content

Commit abdf956

Browse files
committed
BIT instruction; Bit Test.
1 parent 54f02a5 commit abdf956

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

cpu/cpu.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ func (c *Cpu) execute(in Instruction) {
218218
c.BCS(in)
219219
case beq:
220220
c.BEQ(in)
221+
case bit:
222+
c.BIT(in)
221223
case bmi:
222224
c.BMI(in)
223225
case bne:
@@ -361,6 +363,14 @@ func (c *Cpu) BEQ(in Instruction) {
361363
}
362364
}
363365

366+
// BIT: Bit Test.
367+
func (c *Cpu) BIT(in Instruction) {
368+
value := c.resolveOperand(in)
369+
c.setStatus(sZero, value&c.AC == 0)
370+
c.setStatus(sOverflow, value&(1<<6) != 0)
371+
c.setStatus(sNegative, value&(1<<7) != 0)
372+
}
373+
364374
// BMI: Branch if negative.
365375
func (c *Cpu) BMI(in Instruction) {
366376
if c.getStatus(sNegative) {

cpu/cpu_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cpu
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/pda/go6502/bus"
8+
"github.com/pda/go6502/memory"
9+
)
10+
11+
func createCpu() *Cpu {
12+
ram := &memory.Ram{}
13+
addressBus, _ := bus.CreateBus()
14+
addressBus.Attach(ram, "ram", 0x8000) // upper 32K
15+
cpu := &Cpu{Bus: addressBus}
16+
cpu.Reset()
17+
return cpu
18+
}
19+
20+
func TestBitInstruction(t *testing.T) {
21+
cpu := createCpu()
22+
cpu.Bus.Write(0x8000, 0xAA)
23+
24+
instruction := Instruction{OpType: optypes[0x2C], Op16: 0x8000}
25+
expectedName := "BIT absolute $8000"
26+
actualName := instruction.String()
27+
if actualName != expectedName {
28+
t.Error(fmt.Sprintf("expected %s, got %s\n", expectedName, actualName))
29+
}
30+
31+
cpu.BIT(instruction)
32+
33+
expectedStatus := "n-_b-iz-"
34+
actualStatus := cpu.statusString()
35+
if actualStatus != expectedStatus {
36+
t.Error(fmt.Sprintf("SR expected %s got %s\n", expectedStatus, actualStatus))
37+
}
38+
}

0 commit comments

Comments
 (0)