/
software.go
223 lines (196 loc) · 4.87 KB
/
software.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package software
import (
"image"
"image/color"
"github.com/akatsuki105/dawngb/util"
)
const (
Z_BD = iota
Z_BG
Z_SPR
Z_MASTER_SPR = 100 // CGBモードでLCDC.0が0のときは必ずスプライトが前面に来る
)
var dmgPalette = [4]rgb555{
{0b11111, 0b11111, 0b11111},
{0b10001, 0b10001, 0b10001},
{0b01010, 0b01010, 0b01010},
{0b00000, 0b00000, 0b00000},
}
var screen = image.NewRGBA(image.Rect(0, 0, 256, 256))
type Software struct {
vram []uint8
oam []uint8
scanline [160]pixel
bg *bgLayer
win *windowLayer
sprite *spriteLayer
model int // 0: DMG, 1: CGB, 2: SGB
}
type pixel struct {
rgba rgb555
z int // z-index
colorID int
isBG bool
}
func New(vram, oam []uint8, model int) *Software {
r := &Software{
vram: vram,
oam: oam,
model: model,
}
r.bg = newBG(r)
r.win = newWindow(r)
r.sprite = newSpriteLayer(r)
return r
}
func (s *Software) DrawScanline(y int, scanline []color.RGBA) {
if y == 0 {
s.win.y = 0
}
for i := 0; i < 160; i++ {
s.scanline[i].z = -1
s.scanline[i].colorID = 0
s.scanline[i].isBG = false
}
s.bg.drawScanline(y, s.scanline[:])
s.win.drawScanline(y, s.scanline[:])
s.sprite.drawScanline(y, s.scanline[:])
for i := 0; i < 160; i++ {
scanline[i] = s.scanline[i].rgba.RGBA()
}
}
func (s *Software) SetLCDC(val uint8) {
s.bg.active = util.Bit(val, 0)
if s.model == 1 {
s.bg.active = true
if util.Bit(val, 0) {
s.sprite.z = 0
} else {
s.sprite.z = Z_MASTER_SPR
}
}
s.bg.tilemap = [2]uint16{0x1800, 0x1C00}[util.Btoi(util.Bit(val, 3))]
s.bg.tiledata = [2]int{0x800, 0x0}[util.Btoi(util.Bit(val, 4))]
s.win.active = s.bg.active && util.Bit(val, 5)
s.win.tilemap = [2]uint16{0x1800, 0x1C00}[util.Btoi(util.Bit(val, 6))]
s.sprite.active = util.Bit(val, 1)
s.sprite.height = [2]int{8, 16}[util.Btoi(util.Bit(val, 2))]
}
func (s *Software) SetBGP(val uint8) {
if s.model != 1 {
s.bg.palette[0] = dmgPalette[val&0b11]
s.bg.palette[1] = dmgPalette[(val>>2)&0b11]
s.bg.palette[2] = dmgPalette[(val>>4)&0b11]
s.bg.palette[3] = dmgPalette[(val>>6)&0b11]
}
}
func (s *Software) SetOBP0(val uint8) {
if s.model != 1 {
s.sprite.palette[0] = dmgPalette[val&0b11]
s.sprite.palette[1] = dmgPalette[(val>>2)&0b11]
s.sprite.palette[2] = dmgPalette[(val>>4)&0b11]
s.sprite.palette[3] = dmgPalette[(val>>6)&0b11]
}
}
func (s *Software) SetOBP1(val uint8) {
if s.model != 1 {
s.sprite.palette[4] = dmgPalette[val&0b11]
s.sprite.palette[5] = dmgPalette[(val>>2)&0b11]
s.sprite.palette[6] = dmgPalette[(val>>4)&0b11]
s.sprite.palette[7] = dmgPalette[(val>>6)&0b11]
}
}
func (s *Software) SetSCX(val uint8) { s.bg.scx = int(val) }
func (s *Software) SetSCY(val uint8) { s.bg.scy = int(val) }
func (s *Software) SetWX(val uint8) { s.win.wx = int(val) - 7 }
func (s *Software) SetWY(val uint8) { s.win.wy = int(val) }
func (s *Software) SetBGPI(val uint8) { s.bg.bgpi = val }
func (s *Software) GetBGPD() uint8 {
val := uint8(0xFF)
if s.model == 1 {
palID := int((s.bg.bgpi & 0x3F) / 8)
colorID := int(s.bg.bgpi&7) >> 1
rgb := &s.bg.palette[palID*4+colorID]
isHi := util.Bit(s.bg.bgpi, 0)
if isHi {
// 0b0BBBBBGG
val = (rgb.b << 2)
val |= ((rgb.g >> 3) & 0b11)
} else {
// 0bGGGRRRRR
val = rgb.r
val |= (rgb.g << 5)
}
}
return val
}
func (s *Software) SetBGPD(val uint8) uint8 {
if s.model == 1 {
palID := int((s.bg.bgpi & 0x3F) / 8)
colorID := int(s.bg.bgpi&7) >> 1
rgb := &s.bg.palette[palID*4+colorID]
isHi := util.Bit(s.bg.bgpi, 0)
if isHi {
// 0b0BBBBBGG
rgb.g &= 0b111
rgb.g |= ((val & 0b11) << 3)
rgb.b = ((val >> 2) & 0b11111)
} else {
// 0bGGGRRRRR
rgb.r = (val & 0b11111)
rgb.g &= 0b11000
rgb.g |= ((val >> 5) & 0b111)
}
if util.Bit(s.bg.bgpi, 7) {
bgpi := (s.bg.bgpi + 1) & 0x3F
s.bg.bgpi &= 0xC0
s.bg.bgpi |= bgpi
}
}
return s.bg.bgpi
}
func (s *Software) SetOBPI(val uint8) { s.sprite.obpi = val }
func (s *Software) GetOBPD() uint8 {
val := uint8(0xFF)
if s.model == 1 {
palID := int((s.sprite.obpi & 0x3F) / 8)
colorID := int(s.sprite.obpi&7) >> 1
rgb := &s.sprite.palette[palID*4+colorID]
isHi := util.Bit(s.sprite.obpi, 0)
if isHi {
// 0b0BBBBBGG
val = (rgb.b << 2)
val |= ((rgb.g >> 3) & 0b11)
} else {
// 0bGGGRRRRR
val = rgb.r
val |= (rgb.g << 5)
}
}
return val
}
func (s *Software) SetOBPD(val uint8) uint8 {
if s.model == 1 {
palID := int((s.sprite.obpi & 0x3F) / 8)
colorID := int(s.sprite.obpi&7) >> 1
rgb := &s.sprite.palette[palID*4+colorID]
isHi := util.Bit(s.sprite.obpi, 0)
if isHi {
// 0b0BBBBBGG
rgb.g &= 0b111
rgb.g |= ((val & 0b11) << 3)
rgb.b = ((val >> 2) & 0b11111)
} else {
// 0bGGGRRRRR
rgb.r = (val & 0b11111)
rgb.g &= 0b11000
rgb.g |= ((val >> 5) & 0b111)
}
if util.Bit(s.sprite.obpi, 7) {
obpi := (s.sprite.obpi + 1) & 0x3F
s.sprite.obpi &= 0xC0
s.sprite.obpi |= obpi
}
}
return s.sprite.obpi
}