forked from djhworld/gomeboycolor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CGB.go
88 lines (73 loc) · 2.16 KB
/
CGB.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package gpu
import (
"fmt"
"types"
)
//Colour GB graphics register addresses
const (
CGB_VRAM_BANK_SELECT types.Word = 0xFF4F
CGB_BGP_WRITESPEC_REGISTER = 0xFF68
CGB_BGP_WRITEDATA_REGISTER = 0xFF69
CGB_OBJP_WRITESPEC_REGISTER = 0xFF6A
CGB_OBJP_WRITEDATA_REGISTER = 0xFF6B
)
//Represents the attribute data for a background tile
type CGBBackgroundTileAttrs struct {
HasPriority bool
FlipHorizontally bool
FlipVertically bool
BankNo int
PaletteNo int
}
func NewCGBBackgroundTileAttrs(attributeData byte) *CGBBackgroundTileAttrs {
var cbc *CGBBackgroundTileAttrs = new(CGBBackgroundTileAttrs)
cbc.HasPriority = (attributeData & 0x80) == 0x80
cbc.FlipVertically = (attributeData & 0x40) == 0x40
cbc.FlipHorizontally = (attributeData & 0x20) == 0x20
cbc.BankNo = int((attributeData & 0x08) >> 3)
cbc.PaletteNo = int(attributeData & 0x07)
return cbc
}
func (cattr *CGBBackgroundTileAttrs) String() string {
return fmt.Sprintf("%#v", cattr)
}
//Represents a color
type CGBColor types.Word
func (c CGBColor) ToRGB() types.RGB {
return types.RGB{
Red: byte(c&0x001F) * 8,
Green: byte(c&0x03E0>>5) * 8,
Blue: byte(c&0x7C00>>10) * 8}
}
func (c CGBColor) High() byte {
return byte((c & 0xFF00) >> 8)
}
func (c CGBColor) Low() byte {
return byte(c & 0x00FF)
}
//Represents a color palette (4 colors)
type CGBPalette [4]CGBColor
func (cp *CGBPalette) UpdateHigh(colorNo int, value byte) {
cp[colorNo] = (cp[colorNo] & 0x00FF) | CGBColor(value)<<8
}
func (cp *CGBPalette) UpdateLow(colorNo int, value byte) {
cp[colorNo] = (cp[colorNo] & 0xFF00) | CGBColor(value)
}
//Represents the write specification register for a color palette
type CGBPaletteSpecRegister struct {
Value byte
PalleteNo int
PalleteDataNo int
High bool
IncrementOnNext bool
}
func (psr *CGBPaletteSpecRegister) Update(value byte) {
psr.Value = value
psr.PalleteNo = int((value & 0x38) >> 3)
psr.PalleteDataNo = int((value & 0x06) >> 1)
psr.High = (value & 0x01) == 0x01
psr.IncrementOnNext = (value & 0x80) == 0x80
}
func (psr *CGBPaletteSpecRegister) Increment() {
psr.Update(psr.Value + 1)
}