Skip to content

Commit

Permalink
moved tests to appropriate test file
Browse files Browse the repository at this point in the history
  • Loading branch information
koron committed Apr 22, 2024
1 parent f0f7ec7 commit b4ba875
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 118 deletions.
2 changes: 2 additions & 0 deletions accum.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package z80

// This file includes functions which related to "accumulator" or "ACL"

import "math/bits"

func (cpu *CPU) updateFlagArith8(r, a, b uint16, subtract bool) {
Expand Down
115 changes: 115 additions & 0 deletions accum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,121 @@ import (
"github.com/google/go-cmp/cmp"
)

const (
// C is an index for carry flag.
C = 0

// N is an index for add/subtract flag.
N = 1

// PV is an index for parity/overflow flag.
PV = 2

// X3 is reserved index for unused flag.
//X3 = 3

// H is an index for half carry flag.
H = 4

// X5 is reserved index for unused flag.
//X5 = 5

// Z is an index for zero flag.
Z = 6

// S is an index for sign flag.
S = 7
)

// flagOp provides flag operation. At initial this will keep all bits.
type flagOp struct {
Nand uint8
Or uint8
}

// ApplyOn applies flag operation on uint8 in place.
func (fo flagOp) ApplyOn(v *uint8) {
*v = *v&^fo.Nand | fo.Or
}

// Keep marks bit-n as keeping.
func (fo flagOp) Keep(n int) flagOp {
var m uint8 = ^(0x01 << n)
fo.Nand &= m
fo.Or &= m
return fo
}

// Set marks bit-n as being 1.
func (fo flagOp) Set(n int) flagOp {
var b uint8 = 0x01 << n
fo.Nand |= b
fo.Or |= b
return fo
}

// Reset marks bit-n as being 0.
func (fo flagOp) Reset(n int) flagOp {
var b uint8 = 0x01 << n
fo.Nand |= b
fo.Or &= ^b
return fo
}

// Put modify bit-n with boolean value.
func (fo flagOp) Put(n int, v bool) flagOp {
if v {
return fo.Set(n)
}
return fo.Reset(n)
}

func TestFlagOp_ApplyOn(t *testing.T) {
t.Parallel()
for _, c := range []struct {
op flagOp
v []uint8
exp []uint8
}{
{flagOp{0x00, 0x00}, []uint8{0x01, 0x00}, []uint8{0x01, 0x00}},
{flagOp{0x01, 0x00}, []uint8{0x01, 0x00}, []uint8{0x00, 0x00}},
{flagOp{0x01, 0x01}, []uint8{0x01, 0x00}, []uint8{0x01, 0x01}},
// undefined behavior
{flagOp{0x00, 0x01}, []uint8{0x01, 0x00}, []uint8{0x01, 0x01}},

{flagOp{0x00, 0x00}, []uint8{0x10, 0x00}, []uint8{0x10, 0x00}},
{flagOp{0x10, 0x00}, []uint8{0x10, 0x00}, []uint8{0x00, 0x00}},
{flagOp{0x10, 0x10}, []uint8{0x10, 0x00}, []uint8{0x10, 0x10}},
// undefined behavior
{flagOp{0x00, 0x10}, []uint8{0x10, 0x00}, []uint8{0x10, 0x10}},

{flagOp{0x00, 0x00}, []uint8{0x11}, []uint8{0x11}},
{
flagOp{0x11, 0x00},
[]uint8{0x11, 0x10, 0x01, 0x00},
[]uint8{0x00, 0x00, 0x00, 0x00},
},
{
flagOp{0x11, 0x11},
[]uint8{0x11, 0x10, 0x01, 0x00},
[]uint8{0x11, 0x11, 0x11, 0x11},
},
// undefined behavior
{
flagOp{0x00, 0x11},
[]uint8{0x11, 0x10, 0x01, 0x00},
[]uint8{0x11, 0x11, 0x11, 0x11},
},
} {
for i, v := range c.v {
c.op.ApplyOn(&v)
if v != c.exp[i] {
t.Fatalf("failed %#v.ApplyOn(0x%02x): expect=0x%02x actual=0x%02x", c.op, c.v[i], c.exp[i], v)
}
}
}
}

func carry2n(c bool) int {
if c {
return 1
Expand Down
118 changes: 0 additions & 118 deletions flag_test.go

This file was deleted.

0 comments on commit b4ba875

Please sign in to comment.