forked from nesbox/TIC-80
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tic80.zig
494 lines (420 loc) · 15 KB
/
tic80.zig
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// ****************************************************************************
// ****************************************************************************
//
// DO NOT MODIFY THIS FILE. THIS IS A COPY, NO THE ORIGINAL.
//
// ****************************************************************************
// ****************************************************************************
//
// It is merely a copy of `./templates/zig/src/tic80.zig`
//
// Modify that file then run `./tools/zig_sync` to update all libraries
//
// (yes even the original has this exact comment, be careful)
const std = @import("std");
comptime { std.testing.refAllDecls(@This()); }
// types
const MAX_STRING_SIZE = 200;
const Offset_i8 = extern struct {
x: i8,
y: i8,
};
const RGBColor = extern struct {
r: u8,
g: u8,
b: u8,
};
const TextureSource = enum(i32) {
TILES = 0,
MAP,
VBANK1
};
// ------------------------
// HARDWARE REGISTERS / RAM
pub const WIDTH: u32 = 240;
pub const HEIGHT: u32 = 136;
pub const FRAMEBUFFER: *allowzero volatile [16320]u8 = @intToPtr(*allowzero volatile [16320]u8, 0);
pub const TILES : *[8192]u8 = @intToPtr(*[8192]u8, 0x4000);
pub const SPRITES : *[8192]u8 = @intToPtr(*[8192]u8, 0x6000);
pub const MAP : *[32640]u8 = @intToPtr(*[32640]u8, 0x8000);
pub const GAMEPADS : *[4]u8 = @intToPtr(*[4]u8, 0xFF80);
pub const MOUSE : *[4]u8 = @intToPtr(*[4]u8, 0xFF84);
pub const KEYBOARD : *[4]u8 = @intToPtr(*[4]u8, 0xFF88);
pub const SFX_STATE: *[16]u8 = @intToPtr(*[16]u8, 0xFF8C);
pub const SOUND_REGISTERS: *[72]u8 = @intToPtr(*[72]u8, 0xFF9C);
pub const WAVEFORMS: *[256]u8 =@intToPtr(*[256]u8, 0xFFE4);
pub const SFX: *[4224]u8 =@intToPtr(*[4224]u8, 0x100E4);
pub const MUSIC_PATTERNS: *[11520]u8 =@intToPtr(*[11520]u8, 0x11164);
pub const MUSIC_TRACKS: *[408]u8 =@intToPtr(*[408]u8, 0x13E64);
pub const SOUND_STATE: *[4]u8 =@intToPtr(*[4]u8, 0x13FFC);
pub const STEREO_VOLUME: *[4]u8 =@intToPtr(*[4]u8, 0x14000);
pub const PERSISTENT_RAM: *[1024]u8 =@intToPtr(*[1024]u8, 0x14004);
pub const PERSISTENT_RAM_u32: *[256]u32 =@intToPtr(*[256]u32, 0x14004);
pub const SPRITE_FLAGS: *[512]u8 =@intToPtr(*[512]u8, 0x14404);
pub const SYSTEM_FONT: *[2048]u8 =@intToPtr(*[2048]u8, 0x14604);
// vbank 0
const PaletteMap = packed struct {
color0: u4,
color1: u4,
color2: u4,
color3: u4,
color4: u4,
color5: u4,
color6: u4,
color7: u4,
color8: u4,
color9: u4,
color10: u4,
color11: u4,
color12: u4,
color13: u4,
color14: u4,
color15: u4,
};
pub const PALETTE: *[16]RGBColor = @intToPtr(*[16]RGBColor, 0x3FC0);
pub const PALETTE_u8: *[48]u8 = @intToPtr(*[48]u8, 0x3FC0);
pub const PALETTE_MAP: *PaletteMap = @intToPtr(*PaletteMap, 0x3FF0);
pub const PALETTE_MAP_u8: *[8]u8 = @intToPtr(*[8]u8, 0x3FF0);
// TODO: this doesn't work unless it's packed, which I dno't think we can do?
// pub const PALETTE_MAP: *[16]u4 = @intToPtr(*[16]u4, 0x3FF0);
pub const BORDER_COLOR: *u8 = @intToPtr(*u8, 0x3FF8);
pub const SCREEN_OFFSET: *Offset_i8 = @intToPtr(*Offset_i8, 0x3FF9);
pub const MOUSE_CURSOR: *u8 = @intToPtr(*u8, 0x3FFB);
pub const BLIT_SEGMENT: *u8 = @intToPtr(*u8, 0x3FFC);
// vbank 1
pub const OVR_TRANSPARENCY: *u8 = @intToPtr(*u8, 0x3FF8);
// import the RAW api
pub const raw = struct {
pub extern fn btn(id: i32) i32;
pub extern fn btnp(id: i32, hold: i32, period: i32 ) bool;
pub extern fn clip(x: i32, y: i32, w: i32, h: i32) void;
pub extern fn cls(color: i32) void;
pub extern fn circ(x: i32, y: i32, radius: i32, color: i32) void;
pub extern fn circb(x: i32, y: i32, radius: i32, color: i32) void;
pub extern fn exit() void;
pub extern fn elli(x: i32, y: i32, a: i32, b: i32, color: i32) void;
pub extern fn ellib(x: i32, y: i32, a: i32, b: i32, color: i32) void;
pub extern fn fget(id: i32, flag: u8) bool;
pub extern fn font(text: [*:0]u8, x: u32, y: i32, trans_colors: ?[*]const u8, color_count: i32, char_width: i32, char_height: i32, fixed: bool, scale: i32, alt: bool) i32;
pub extern fn fset(id: i32, flag: u8, value: bool) bool;
pub extern fn key(keycode: i32) bool;
pub extern fn keyp(keycode: i32, hold: i32, period: i32 ) bool;
pub extern fn line(x0: i32, y0: i32, x1: i32, y1: i32, color: i32) void;
pub extern fn map(x: i32, y: i32, w: i32, h: i32, sx: i32, sy: i32, trans_colors: ?[*]const u8, color_count: i32, scale: i32, remap: i32) void;
pub extern fn memcpy(to: u32, from: u32, length: u32) void;
pub extern fn memset(addr: u32, value: u8, length: u32) void;
pub extern fn mget(x: i32, y:i32) i32;
pub extern fn mouse(data: *MouseData) void;
pub extern fn mset(x: i32, y:i32, value: bool) void;
pub extern fn music(track: i32, frame: i32, row: i32, loop: bool, sustain: bool, tempo: i32, speed: i32) void;
pub extern fn peek(addr: u32, bits: i32) u8;
pub extern fn peek4(addr4: u32) u8;
pub extern fn peek2(addr2: u32) u8;
pub extern fn peek1(bitaddr: u32) u8;
pub extern fn pix(x: i32, y:i32, color: i32) void;
pub extern fn pmem(index: u32, value: i64) u32;
pub extern fn poke(addr: u32, value: u8, bits: i32) void;
pub extern fn poke4(addr4: u32, value: u8) void;
pub extern fn poke2(addr2: u32, value: u8) void;
pub extern fn poke1(bitaddr: u32, value: u8) void;
pub extern fn print(text: [*:0]const u8, x: i32, y: i32, color: i32, fixed: bool, scale: i32, smallfont: bool) i32;
pub extern fn rect(x: i32, y: i32, w: i32, h:i32, color: i32) void;
pub extern fn rectb(x: i32, y: i32, w: i32, h:i32, color: i32) void;
pub extern fn reset() void;
pub extern fn sfx(id: i32, note: i32, octave: i32, duration: i32, channel: i32, volumeLeft: i32, volumeRight: i32, speed: i32) void;
pub extern fn spr(id: i32, x: i32, y: i32, trans_colors: ?[*]const u8, color_count: i32, scale: i32, flip: i32, rotate: i32, w: i32, h: i32) void;
pub extern fn sync(mask: i32, bank: i32, tocart: bool) void;
pub extern fn ttri(x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, u1: f32, v1: f32, u2: f32, v2: f32, u3: f32, v3: f32, texture_source: i32, trans_colors: ?[*]const u8, color_count: i32, z1: f32, z2: f32, z3: f32, depth: bool) void;
pub extern fn tri(x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, color: i32) void;
pub extern fn trib(x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, color: i32) void;
pub extern fn time() f32;
pub extern fn trace(text: [*:0]const u8, color: i32) void;
pub extern fn tstamp() u64;
pub extern fn vbank(bank: i32) u8;
};
// -----
// INPUT
const MouseData = extern struct {
x: i16,
y: i16,
scrollx: i8,
scrolly: i8,
left: bool,
middle: bool,
right: bool,
};
pub const key = raw.key;
// TODO: nicer API?
pub const keyp = raw.keyp;
pub const held = raw.btnp;
pub fn pressed(id: i32) bool {
return raw.btnp(id, -1, -1);
}
pub fn btn(id: i32) bool {
return raw.btn(id) != 0;
}
pub fn anybtn() bool {
return raw.btn(-1) != 0;
}
// -----------------
// DRAW / DRAW UTILS
pub const clip = raw.clip;
pub fn noclip() void {
raw.clip(0, 0, WIDTH, HEIGHT);
}
pub const cls = raw.cls;
pub const circ = raw.circ;
pub const circb = raw.circb;
pub const elli = raw.elli;
pub const ellib = raw.ellib;
pub const fget = raw.fget;
pub const fset = raw.fset;
pub const line = raw.line;
pub const mset = raw.mset;
pub const mget = raw.mget;
pub const mouse = raw.mouse;
// map [x=0 y=0] [w=30 h=17] [sx=0 sy=0] [colorkey=-1] [scale=1] [remap=nil]
// TODO: remap should be what????
// pub extern fn map(x: i32, y: i32, w: i32, h: i32, sx: i32, sy: i32, trans_colors: ?[*]u8, color_count: i32, scale: i32, remap: i32) void;
const MapArgs = struct {
x: i32 = 0,
y: i32 = 0,
w: i32 = 30,
h: i32 = 17,
sx: i32 = 0,
sy: i32 = 0,
transparent: []const u8 = &.{},
scale: u8 = 1,
remap: i32 = -1, // TODO
};
pub fn map(args: MapArgs) void {
const color_count = @intCast(u8,args.transparent.len);
const colors = args.transparent.ptr;
std.debug.assert(color_count < 16);
raw.map(args.x, args.y, args.w, args.h, args.sx, args.sy, colors, color_count, args.scale, args.remap);
}
pub fn pix(x: i32, y: i32, color: u8) void {
raw.pix(x,y,color);
}
pub fn getpix(x: i32, y: i32) u8 {
raw.pix(x,y, -1);
}
// pub extern fn spr(id: i32, x: i32, y: i32, trans_colors: [*]u8, color_count: i32, scale: i32, flip: i32, rotate: i32, w: i32, h: i32) void;
pub const Flip = enum(u2) {
no = 0,
horizontal = 1,
vertical = 2,
both = 3,
};
pub const Rotate = enum(u2) {
no = 0,
by90 = 1,
by180 = 2,
by270 = 3,
};
const SpriteArgs = struct {
w: i32 = 1,
h: i32 = 1,
transparent: []const u8 = &.{},
scale: u8 = 1,
flip: Flip = Flip.no,
rotate: Rotate = Rotate.no,
};
pub fn spr(id: i32, x: i32, y: i32, args: SpriteArgs) void {
const color_count = @intCast(u8,args.transparent.len);
const colors = args.transparent.ptr;
std.debug.assert(color_count < 16);
raw.spr(id, x, y, colors, color_count, args.scale, @enumToInt(args.flip), @enumToInt(args.rotate), args.w, args.h);
}
pub const rect = raw.rect;
pub const rectb = raw.rectb;
pub const tri = raw.tri;
pub const trib = raw.trib;
const TextriArgs = struct {
texture_source : TextureSource = TextureSource.TILES,
transparent: []const u8 = &.{},
z1 : f32 = 0,
z2 : f32 = 0,
z3 : f32 = 0,
depth : bool = false,
};
pub fn ttri(x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, @"u1": f32, v1: f32, @"u2": f32, v2: f32, @"u3": f32, v3: f32, args: TextriArgs) void {
const color_count = @intCast(u8,args.transparent.len);
const trans_colors = args.transparent.ptr;
raw.ttri(x1, y1, x2, y2, x3, y3, @"u1", v1, @"u2", v2, @"u3", v3, @enumToInt(args.texture_source), trans_colors, color_count, args.z1, args.z2, args.z3, args.depth);
}
// ----
// TEXT
const PrintArgs = struct {
color: u8 = 15,
fixed : bool = false,
small_font : bool = false,
scale : u8 = 1,
};
const FontArgs = struct {
transparent: []const u8 = &.{},
char_width: u8,
char_height: u8,
fixed: bool = false,
scale: u8 = 1,
alt: bool = false
};
fn sliceToZString(text: []const u8, buff: [*:0]u8, maxLen: u16) void {
var len = std.math.min(maxLen, text.len);
std.mem.copy(u8, buff[0..len], text[0..len]);
buff[len] = 0;
}
pub fn print(text: []const u8, x: i32, y: i32, args: PrintArgs) i32 {
var buff : [MAX_STRING_SIZE:0]u8 = undefined;
sliceToZString(text, &buff, MAX_STRING_SIZE);
return raw.print(&buff, x, y, args.color, args.fixed, args.scale, args.small_font);
}
pub fn printf(comptime fmt: []const u8, fmtargs: anytype, x: i32, y: i32, args: PrintArgs) i32 {
var buff : [MAX_STRING_SIZE:0]u8 = undefined;
_ = std.fmt.bufPrintZ(&buff, fmt, fmtargs) catch unreachable;
return raw.print(&buff, x, y, args.color, args.fixed, args.scale, args.small_font);
}
pub fn font(text: []const u8, x: u32, y: i32, args: FontArgs) i32 {
const color_count = @intCast(u8,args.transparent.len);
const colors = args.transparent.ptr;
var buff : [MAX_STRING_SIZE:0]u8 = undefined;
sliceToZString(text, &buff, MAX_STRING_SIZE);
return raw.font(&buff, x, y, colors, color_count, args.char_width, args.char_height, args.fixed, args.scale, args.alt);
}
// -----
// AUDIO
// music [track=-1] [frame=-1] [row=-1] [loop=true] [sustain=false] [tempo=-1] [speed=-1]
const MusicArgs = struct {
frame: i8 = -1,
row: i8 = -1,
loop: bool = true,
sustain: bool = false,
tempo: i8 = -1,
speed: i8 = -1,
};
pub fn music(track:i32, args: MusicArgs) void {
raw.music(track, args.frame, args.row, args.loop, args.sustain, args.tempo, args.speed);
}
pub fn nomusic() void {
music(-1, .{});
}
// sfx id [note] [duration=-1] [channel=0] [volume=15] [speed=0]
// void tic_api_sfx(tic_mem* memory, s32 index, s32 note, s32 octave, s32 duration, s32 channel, s32 left, s32 right, s32 speed)
const MAX_VOLUME : i8 = 15;
const SfxArgs = struct {
sfx: i8 = -1,
note: i8 = -1,
octave: i8 = -1,
duration: i8 = -1,
channel: i8 = 0,
volume: i8 = 15,
volumeLeft: i8 = 15,
volumeRight: i8 = 15,
speed: i8 = 0,
};
// pub extern fn sfx(id: i32, note: i32, duration: i32, channel: i32, volume: i32, speed: i32) void;
const SFX_NOTES = [_][] const u8 {"C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-"};
fn parse_note(name: []const u8, noteArgs: *SfxArgs) bool {
var note_signature : []const u8 = undefined;
var octave : u8 = undefined;
if (name.len == 2) {
note_signature = &.{ name[0], '-' };
octave = name[1];
} else {
note_signature = name[0..2];
octave = name[2];
}
var i: i8 = 0;
for (SFX_NOTES) |music_note| {
if (std.mem.eql(u8, music_note, note_signature)) {
noteArgs.note = i;
noteArgs.octave = @intCast(i8, octave - '1');
return true;
}
i+=1;
}
return false;
}
pub fn note(name: []const u8, args: SfxArgs) void {
var largs : SfxArgs = args;
if (parse_note(name, &largs)) {
sfx(args.sfx, largs);
}
}
pub fn sfx(id: i32, args: SfxArgs) void {
var largs = args;
if (args.volume != MAX_VOLUME) {
largs.volumeLeft = args.volume;
largs.volumeRight = args.volume;
}
raw.sfx(id, args.note, args.octave, args.duration, args.channel, largs.volumeLeft, largs.volumeRight, args.speed);
}
pub fn nosfx() void {
sfx(-1, .{});
}
// ------
// MEMORY
pub fn pmemset(index: u32, value: u3) void {
raw.pmem(index, value);
}
pub fn pmemget(index: u32) u32 {
return raw.pmem(index, -1);
}
pub const memcpy = raw.memcpy;
pub const memset = raw.memset;
pub fn poke(addr: u32, value: u8) void {
raw.poke(addr, value, 8);
}
pub const poke4 = raw.poke4;
pub const poke2 = raw.poke2;
pub const poke1 = raw.poke1;
pub fn peek(addr: u32) u8 {
return raw.peek(addr, 8);
}
pub const peek4 = raw.peek4;
pub const peek2 = raw.peek2;
pub const peek1 = raw.peek1;
pub const vbank = raw.vbank;
// SYSTEM
pub const exit = raw.exit;
pub const reset = raw.reset;
pub fn trace(text: []const u8) void {
var buff : [MAX_STRING_SIZE:0]u8 = undefined;
sliceToZString(text, &buff, MAX_STRING_SIZE);
raw.trace(&buff);
}
const SectionFlags = packed struct {
// tiles = 1<<0 -- 1
// sprites = 1<<1 -- 2
// map = 1<<2 -- 4
// sfx = 1<<3 -- 8
// music = 1<<4 -- 16
// palette = 1<<5 -- 32
// flags = 1<<6 -- 64
// screen = 1<<7 -- 128 (as of 0.90)
tiles: bool = false,
sprites: bool = false,
map: bool = false,
sfx: bool = false,
music: bool = false,
palette: bool = false,
flags: bool = false,
screen: bool = false,
};
// const SectionFlagsMask = packed union {
// sections: SectionFlags,
// value: u8
// };
const SyncArgs = struct {
sections: SectionFlags,
bank: u8,
toCartridge: bool = false,
fromCartridge: bool = false,
};
pub fn sync(args: SyncArgs) void {
// var mask = SectionFlagsMask { .sections = args.sections };
raw.sync(@bitCast(u8, args.sections), args.bank, args.toCartridge);
}
// TIME KEEPING
pub const time = raw.time;
pub const tstamp = raw.tstamp;