summaryrefslogtreecommitdiff
path: root/main.myr
blob: feac70c3faf2cc5d6e8503cf50652e597ec9ab50 (plain)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use std
use sdl

const winwidth = 960
const winheight = 480

const scrwidth = 64
const scrheight = 32
const scrsz = scrwidth * scrheight

const memsz = 4096
const stksz = 16
const regsz = 16

const clrscr = {b, n
    var i
    for i = 0; i < n; i++;
        b#[i] = 0
    ;;
}

const loadprg = {b, fname
    var fd, i, buf
    match std.open(fname, std.Oread)
    | `std.Ok(n): fd = n
    | `std.Err(errno):
        std.fatal("could not open {}\n", fname)
    ;;

    buf = std.slalloc(0xfff - 0x200)
    match std.read(fd, buf)
    | `std.Ok(n):
        for i = 0; i < 0xfff - 0x200; i++;
            b#[i + 0x200] = (buf[i] : uint8)
        ;;
    | `std.Err(n):
        std.fatal("could not read {}\n", fname)
    ;;
}

const main = {
    var win, r, tex
    sdl.init(sdl.INIT_VIDEO)
    win = sdl.mkwin(("chip\0" : byte#),
        sdl.WIN_POS_UNSPEC,
        sdl.WIN_POS_UNSPEC,
        winwidth,
        winheight,
        sdl.WIN_OPENGL)
    r = sdl.mkrenderer(win, -1, sdl.RENDERER_ACCEL)
    tex = sdl.mktexture(r, sdl.PIXFMT_RGB332, sdl.TEXACCESS_STREAM, 64, 32)

    var gfx : uint8[scrsz]
    var mem : uint8[memsz]
    var reg : uint8[regsz]
    var stk : uint16[stksz]
    var pc : uint16 = 0x200
    var sp : uint16 = 0
    var I : uint16 = 0
    var delay : uint8 = 0
    var sound : uint8 = 0

    clrscr(&gfx, scrsz)
    loadprg(&mem, "/tmp/UFO")

    var opcode, arg

    while true
        sdl.delay(1000)
        opcode = mem[pc]
        arg = mem[pc + 1]
        std.put("\x1b[H\x1b[2J")
        std.put("v0: {}\n", reg[0])
        std.put("v1: {}\n", reg[1])
        std.put("v2: {}\n", reg[2])
        std.put("v3: {}\n", reg[3])
        std.put("v4: {}\n", reg[4])
        std.put("v5: {}\n", reg[5])
        std.put("v6: {}\n", reg[6])
        std.put("v7: {}\n", reg[7])
        std.put("v8: {}\n", reg[8])
        std.put("v9: {}\n", reg[9])
        std.put("va: {}\n", reg[10])
        std.put("vb: {}\n", reg[11])
        std.put("vc: {}\n", reg[12])
        std.put("vd: {}\n", reg[13])
        std.put("ve: {}\n", reg[14])
        std.put("vf: {}\n", reg[15])
        std.put("\n")
        std.put("I: {}, d: {}, s: {}\n", I, delay, sound)
        std.put("sp: {}, stk: {}\n", sp, stk)
        std.put("{p=0,x}: {p=0,x} {p=0,x}\n", pc, opcode, arg)
        match (opcode & 0xf0) >> 4
        | 0x0:
            // CLS
            if (opcode & 0x0f) == 0 && (arg & 0x0f) == 0
                clrscr(&gfx, scrsz)
            // RET
            elif (opcode & 0x0f) == 0
                if sp == 0
                    std.die("stack underflow")
                ;;
                pc = stk[sp]
                sp--
            // SYS
            else
                std.die("RCA 1802 SYS unimplemted")
            ;;
        // JP
        | 0x1:
            pc = (opcode & 0x0f : uint16)
            pc <<= 8
            pc |= (arg : uint16)
            pc -= 2
        // CALL
        | 0x2:
            sp++
            if sp >= stksz
                std.die("stack overflow")
            ;;
            stk[sp] = pc
            pc = (opcode & 0x0f : uint16)
            pc <<= 8
            pc |= (arg : uint16)
            pc -= 2
        // SE
        | 0x3:
            if reg[opcode & 0x0f] == arg
                pc += 2
            ;;
        // SNE
        | 0x4:
            if reg[opcode & 0x0f] != arg
                pc += 2
            ;;
        // SE
        | 0x5:
            if reg[opcode & 0x0f] == reg[(arg & 0xf0) >> 4]
                pc += 2
            ;;
        // LD
        | 0x6:
            reg[opcode & 0x0f] = arg
        // ADD (without overflow)
        | 0x7:
            reg[opcode & 0x0f] += arg
        | 0x8:
            match arg & 0x0f
            // LD
            | 0x0:
                reg[opcode & 0x0f] = reg[(arg & 0xf0) >> 8]
            // OR
            | 0x1:
                reg[opcode & 0x0f] |= reg[(arg & 0xf0) >> 8]
            // AND
            | 0x2:
                reg[opcode & 0x0f] &= reg[(arg & 0xf0) >> 8]
            // XOR
            | 0x3:
                reg[opcode & 0x0f] ^= reg[(arg & 0xf0) >> 8]
            // ADD
            | 0x4:
                if (0xff - reg[opcode & 0x0f]) < reg[(arg & 0xf0) >> 8]
                    reg[0xf] = 1
                else
                    reg[0xf] = 0
                ;;
                reg[opcode & 0x0f] += reg[(arg & 0xf0) >> 8]
            // SUB
            | 0x5:
                if (0xff - reg[opcode & 0x0f]) < reg[(arg & 0xf0) >> 8]
                    reg[0xf] = 1
                else
                    reg[0xf] = 0
                ;;
                reg[opcode & 0x0f] -= reg[(arg & 0xf0) >> 8]
            // SHR
            | 0x6:
                reg[0xf] = reg[opcode & 0x0f] & 1
                reg[opcode & 0x0f] >>= 1
            // SUBN
            | 0x7:
                if (0xff - reg[(arg & 0xf0) >> 8]) < reg[opcode & 0x0f]
                    reg[0xf] = 1
                else
                    reg[0xf] = 0
                ;;
                reg[opcode & 0x0f] = reg[(arg & 0xf0) >> 8] - reg[opcode & 0x0f]
            // SHL
            | 0xe:
                reg[0xf] = reg[opcode & 0x0f] & (1 << 7)
                reg[opcode & 0x0f] <<= 1
            | _: std.die("unimplemented opcode")
            ;;
        // SNE
        | 0x9:
            if reg[opcode & 0x0f] != reg[(arg & 0xf0) >> 4]
                pc += 2
            ;;
        // LD (I)
        | 0xa:
            I = (opcode & 0x0f : uint16)
            I <<= 8
            I |= (arg : uint16)
        // JP + V0
        | 0xb:
            pc = (opcode & 0x0f : uint16)
            pc <<= 8
            pc |= (arg : uint16)
            pc += (reg[0] : uint16)
            pc -= 2
        // RND
        | 0xc:
            // HACK: SIGFPE when calling std.rand(0, 256)
            reg[opcode & 0x0f] = (std.rand(0, 255) + std.rand(0, 2)) & arg
        // DRW
        | 0xd:
            var x = (reg[opcode & 0x0f] : uint16)
            var y = (reg[(arg & 0xf0) >> 8] : uint16)
            var n = (arg & 0x0f : uint16)
            var i, j
            for i = 0; i < n; i++;
                for j = 0; j < 8; j++
                    if (y + i) * scrwidth + x + j < scrsz
                        if mem[I + i * 8 + j] != 0
                            gfx[(y + i) * scrwidth + x + j] = 0xff
                        else
                            gfx[(y + i) * scrwidth + x + j] = 0x00
                        ;;
                    ;;
                ;;
            ;;
            sdl.update(tex, (gfx[:] : void#), 64)
            sdl.copy(r, tex)
            sdl.present(r)
        | 0xe:
            // SKP
            if arg == 0x9e
            // SKNP
            elif arg == 0xa1
                pc += 2
            else
                std.die("unimplemented opcode")
            ;;
        | 0xf:
            match arg
            | 0x07:
                reg[opcode & 0x0f] = delay
            | 0x0a:
                std.die("unimplemented opcode")
            | 0x15:
                delay = reg[opcode & 0x0f]
            | 0x18:
                sound = reg[opcode & 0x0f]
            | 0x1e:
                // TODO: Deal with overflow
                I += (reg[opcode & 0x0f] : uint16)
            | 0x29:
                // TODO
                I = 0
            | 0x33:
                // TODO
            | 0x55:
                // TODO
            | 0x65:
                // TODO
            | _:
                std.die("unimplemented opcode")
            ;;
        | _ : std.die("unimplemented opcode")
        ;;
        pc += 2
        if delay > 0
            delay--
        ;;
        if sound > 0
            sound--
        ;;
    ;;

    /* sdl.update(tex, (gfx[:] : void#), 64) */
    /* sdl.copy(r, tex) */
    /* sdl.present(r) */

    sdl.freerenderer(r)
    sdl.freewin(win)
    sdl.quit()
}