-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.go
105 lines (86 loc) · 1.97 KB
/
export.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
package main
import (
"image"
"image/color"
"image/png"
"os"
)
func ExportTileMap0() {
file, err := os.Create("/tmp/tile_map_0.png")
if err != nil {
panic(err)
}
defer file.Close()
out := image.NewGray(image.Rect(0, 0, 32*8, 32*8))
for y := 0; y < 32; y++ {
for x := 0; x < 32; x++ {
tileIndex := read(uint16(0x9800 + (y*32 + x)))
copyTile(out, getTile(tileIndex), x, y)
}
}
png.Encode(file, out)
}
func ExportTileData() {
file, err := os.Create("/tmp/tile_data.png")
if err != nil {
panic(err)
}
defer file.Close()
out := image.NewGray(image.Rect(0, 0, 16*8, 24*8))
for y := 0; y < 24; y++ {
for x := 0; x < 16; x++ {
copyTile(out, convertTile(uint16(0x8000+((y*16+x)<<4))), x, y)
}
}
png.Encode(file, out)
}
func copyTile(out *image.Gray, tile *image.Gray, x int, y int) {
for j := 0; j < 8; j++ {
for i := 0; i < 8; i++ {
out.SetGray((x*8)+i, (y*8)+j, tile.GrayAt(i, j))
}
}
}
func getTile(index uint8) *image.Gray {
// TODO read which tile memory bank to use
var addr uint16
switch {
case index < 0x80:
addr = 0x9000 + (uint16(index) << 4)
default:
addr = 0x8000 + (uint16(index) << 4)
}
return convertTile(addr)
}
func convertTile(addr uint16) *image.Gray {
out := image.NewGray(image.Rect(0, 0, 8, 8))
var y uint16
for y = 0; y < 8; y++ {
line := convertTileLine(read(addr+(2*y)), read(addr+(2*y+1)))
for x, v := range line {
out.SetGray(x, int(y), v)
}
}
return out
}
func convertTileLine(l uint8, h uint8) [8]color.Gray {
var out [8]color.Gray
for i := 0; i < 8; i++ {
bit := uint(7 - i)
paletteIndexL := ((l >> bit) & 0x01)
paletteIndexH := ((h >> bit) & 0x01) << 1
out[i] = convertTilePalette(paletteIndexH | paletteIndexL)
}
return out
}
func convertTilePalette(i uint8) color.Gray {
palette := read(REG_BGP)
colorMapIndex := (palette >> (i * 2)) & 0x03
return colorMap[colorMapIndex]
}
var colorMap = [4]color.Gray{
color.Gray{0xff},
color.Gray{0xaa},
color.Gray{0x55},
color.Gray{0x00},
}