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
|
const std = @import("std");
const ray = @import("raylib.zig");
//const ray = @cImport({
// @cInclude("raylib.h");
//});
const print = std.debug.print;
const fs = std.fs;
const fmt = std.fmt;
const scale: u32 = 15;
const screenWidth: u32 = 64 * scale;
const screenHeight: u32 = (32 * scale) + 24 + 60;
const RndGen = std.rand.DefaultPrng;
var Rnd = RndGen.init(0);
var chip8_screen: ray.RenderTexture = undefined;
var temp_canvas: ray.RenderTexture = undefined;
var shader_stretcher: Shader = undefined;
var shader_stretcher_stretch_amount: c_int = undefined;
var shader_colour_splitter: ShaderColourSplitter = undefined;
var shader_pixalizer: ShaderPixalizer = undefined;
var slider: f32 = 0;
const slider_max: f32 = 0;
const slider_min: f32 = -250;
const Shader = struct {
shader: ray.Shader = undefined,
pub fn new(p_shader: ray.Shader) Shader {
return Shader{ .shader = p_shader };
}
pub fn set_float(self: *Shader, p_id: c_int, p_new_val: f32) void {
//const temp_var_id = ray.GetShaderLocation(self.*.shader, p_var_name);
ray.SetShaderValue(self.*.shader, p_id, &p_new_val, ray.SHADER_UNIFORM_FLOAT);
}
};
const ShaderStretcher = struct {
shader: ray.Shader = undefined,
stretch_amount: f32 = 1.0,
stretch_amount_location: c_int = undefined,
texture_width: f32 = 64.0,
texture_width_location: c_int = undefined,
texture_height: f32 = 32.0,
texture_height_location: c_int = undefined,
pub fn new(shader_val: ray.Shader) ShaderStretcher {
const temp_stretch_amount_loc = ray.GetShaderLocation(shader_val, "stretch_amount");
const temp_texture_width_loc = ray.GetShaderLocation(shader_val, "renderWidth");
const temp_texture_height_loc = ray.GetShaderLocation(shader_val, "renderHeight");
var result = ShaderStretcher{
.shader = shader_val,
.stretch_amount_location = temp_stretch_amount_loc,
.texture_width_location = temp_texture_width_loc,
.texture_height_location = temp_texture_height_loc,
};
ray.SetShaderValue(result.shader, result.stretch_amount_location, &result.stretch_amount, ray.SHADER_UNIFORM_FLOAT);
ray.SetShaderValue(result.shader, result.texture_width_location, &result.texture_width, ray.SHADER_UNIFORM_FLOAT);
ray.SetShaderValue(result.shader, result.texture_height_location, &result.texture_height, ray.SHADER_UNIFORM_FLOAT);
return result;
}
pub fn set_stretch_amount(self: *ShaderStretcher, new_grid_size: f32) f32 {
self.*.stretch_amount = new_grid_size;
ray.SetShaderValue(self.*.shader, self.*.stretch_amount_location, &self.*.stretch_amount, ray.SHADER_UNIFORM_FLOAT);
return self.*.stretch_amount;
}
pub fn set_render_width(self: *ShaderStretcher, new_render_width: f32) f32 {
self.*.render_width = new_render_width;
ray.SetShaderValue(self.*.shader, self.*.render_width_location, &self.*.render_width, ray.SHADER_UNIFORM_FLOAT);
return self.*.render_width;
}
pub fn set_render_height(self: *ShaderStretcher, new_render_height: f32) f32 {
self.*.render_height = new_render_height;
ray.SetShaderValue(self.*.shader, self.*.render_height_location, &self.*.render_height, ray.SHADER_UNIFORM_FLOAT);
return self.*.render_height;
}
};
const ShaderColourSplitter = struct {
shader: ray.Shader = undefined,
texture_width: f32 = 64.0,
texture_width_location: c_int = undefined,
texture_height: f32 = 32.0,
texture_height_location: c_int = undefined,
pub fn new(shader_val: ray.Shader) ShaderColourSplitter {
const temp_texture_width_loc = ray.GetShaderLocation(shader_val, "renderWidth");
const temp_texture_height_loc = ray.GetShaderLocation(shader_val, "renderHeight");
var result = ShaderColourSplitter{
.shader = shader_val,
.texture_width_location = temp_texture_width_loc,
.texture_height_location = temp_texture_height_loc,
};
//var result.texture_width: f32 = 64.0 * 3.0;
//var temp_shader_texture_height: f32 = 32.0;
ray.SetShaderValue(result.shader, result.texture_width_location, &result.texture_width, ray.SHADER_UNIFORM_FLOAT);
ray.SetShaderValue(result.shader, result.texture_height_location, &result.texture_height, ray.SHADER_UNIFORM_FLOAT);
return result;
}
pub fn set_render_width(self: *ShaderPixalizer, new_render_width: f32) f32 {
self.*.render_width = new_render_width;
ray.SetShaderValue(self.*.shader, self.*.render_width_location, &self.*.render_width, ray.SHADER_UNIFORM_FLOAT);
return self.*.render_width;
}
pub fn set_render_height(self: *ShaderPixalizer, new_render_height: f32) f32 {
self.*.render_height = new_render_height;
ray.SetShaderValue(self.*.shader, self.*.render_height_location, &self.*.render_height, ray.SHADER_UNIFORM_FLOAT);
return self.*.render_height;
}
};
const ShaderPixalizer = struct {
shader: ray.Shader = undefined,
grid_size: f32 = 1.0,
grid_size_location: c_int = undefined,
texture_width: f32 = 64.0 * 3.0, // this makes it split by 3 per pixel instead of 1 per pixel
texture_width_location: c_int = undefined,
texture_height: f32 = 32.0,
texture_height_location: c_int = undefined,
pub fn new(shader_val: ray.Shader) ShaderPixalizer {
const temp_grid_size_loc = ray.GetShaderLocation(shader_val, "grid_size");
const temp_texture_width_loc = ray.GetShaderLocation(shader_val, "renderWidth");
const temp_texture_height_loc = ray.GetShaderLocation(shader_val, "renderHeight");
var result = ShaderPixalizer{
.shader = shader_val,
.grid_size_location = temp_grid_size_loc,
.texture_width_location = temp_texture_width_loc,
.texture_height_location = temp_texture_height_loc,
};
ray.SetShaderValue(result.shader, result.grid_size_location, &result.grid_size, ray.SHADER_UNIFORM_FLOAT);
ray.SetShaderValue(result.shader, result.texture_width_location, &result.texture_width, ray.SHADER_UNIFORM_FLOAT);
ray.SetShaderValue(result.shader, result.texture_height_location, &result.texture_height, ray.SHADER_UNIFORM_FLOAT);
return result;
}
pub fn set_grid_size(self: *ShaderPixalizer, new_grid_size: f32) f32 {
self.*.grid_size = new_grid_size;
ray.SetShaderValue(self.*.shader, self.*.grid_size_location, &self.*.grid_size, ray.SHADER_UNIFORM_FLOAT);
return self.*.grid_size;
}
pub fn set_render_width(self: *ShaderPixalizer, new_render_width: f32) f32 {
self.*.render_width = new_render_width;
ray.SetShaderValue(self.*.shader, self.*.render_width_location, &self.*.render_width, ray.SHADER_UNIFORM_FLOAT);
return self.*.render_width;
}
pub fn set_render_height(self: *ShaderPixalizer, new_render_height: f32) f32 {
self.*.render_height = new_render_height;
ray.SetShaderValue(self.*.shader, self.*.render_height_location, &self.*.render_height, ray.SHADER_UNIFORM_FLOAT);
return self.*.render_height;
}
};
const Chip8 = struct {
opcode: u16 = 0,
memory: [4096]u8 = [_]u8{0} ** 4096,
V: [16]u8 = [_]u8{0} ** 16,
I: u16 = 0,
pc: u16 = 0x200,
gfx: [64 * 32]bool = [_]bool{false} ** (64 * 32),
delay_timer: u8 = 0,
sound_timer: u8 = 0,
stack: [16]u16 = [_]u16{0} ** 16,
sp: u16 = 0,
key: u8 = 0,
pub fn new() Chip8 {
// TODO Initialize registers and memory once
return Chip8{};
}
pub fn load_game(self: *Chip8, file_name: []const u8) void {
const file = std.fs.cwd().openFile(file_name, .{ .mode = .read_write }) catch |err| {
print("{}\n", .{err});
return;
};
defer file.close();
var buf: [512 * 7]u8 = undefined; // max memory size that can be loaded into CHIP-8
const chars_read = file.readAll(&buf) catch |err| {
print("{}\n", .{err});
return;
};
//var length: u32 = 0;
for (buf) |char, index| {
if (index == chars_read) {
break;
}
self.*.memory[index + 512] = char;
//length += 1;
}
//length = (length + 514) / 2;
//var iter: u32 = 510 / 2;
//while (iter < length) {
// defer iter += 1;
// print("{}, {}: {x:0>4}\n", .{ iter * 2, iter * 2 + 1, @intCast(u16, self.*.memory[iter * 2]) << 8 | self.*.memory[iter * 2 + 1] });
//}
//print("\n\n", .{});
}
pub fn emulate_cycles(self: *Chip8, cycles: u32) void {
var i: u32 = cycles;
while (i > 0) : (i -= 1) {
// Merge to create Opcode
self.*.opcode = @intCast(u16, self.*.memory[self.*.pc]) << 8 | self.*.memory[self.*.pc + 1];
// Resolve Opcode
switch (self.*.opcode & 0xF000) {
0x0000 => { // 00E0: Clear Screen
defer print("\n", .{});
print("{x:0>4} - Clear Screen\n", .{self.*.opcode});
self.*.gfx = [_]bool{false} ** (64 * 32);
self.*.pc += 2;
},
0x1000 => { // 1NNN: Jump to NNN
defer print("\n", .{});
print("{x:0>4} - Jump to 1NNN\n", .{self.*.opcode});
//self.*.stack[self.*.sp] = self.*.pc;
self.*.pc = @intCast(u12, self.*.opcode & 0x0FFF);
},
0x6000 => { // 6XNN: Set VX to NN
defer print("\n", .{});
print("{x:0>4} - Set VX to 6XNN\n", .{self.*.opcode});
self.*.V[(self.*.opcode & 0x0F00) >> 8] = @intCast(u8, self.*.opcode & 0x00FF);
self.*.pc += 2;
},
0x7000 => { // 7XNN: Add NN to VX (dont change carry flag)
defer print("\n", .{});
print("{x:0>4} - Add 7XNN to VX\n", .{self.*.opcode});
self.*.V[(self.*.opcode & 0x0F00) >> 8] += @intCast(u8, self.*.opcode & 0x00FF);
self.*.pc += 2;
},
0xA000 => { // ANNN: Set I to adress NNN
defer print("\n", .{});
print("{x:0>4} - Set I to ANNN\n", .{self.*.opcode});
self.*.I = self.*.opcode & 0x0FFF;
self.*.pc += 2;
},
0xD000 => { // DXYN: Draw a sprite
defer print("\n", .{});
print("{x:0>4} - Draw Sprite DXYN\n", .{self.*.opcode});
const data = .{
.x = @as(u16, self.*.V[((self.*.opcode & 0x0F00) >> 8)]),
.y = @as(u16, self.*.V[((self.*.opcode & 0x00F0) >> 4)]),
.height = @as(u16, self.*.opcode & 0x000F),
};
var height: u8 = 0;
while (height < data.height) {
defer height += 1;
var pixel_row: u16 = self.*.memory[self.*.I + height];
var width: u8 = 0;
while (width < 8) {
defer width += 1;
if ((pixel_row & (@as(u8, 0x80) >> @intCast(u3, width))) != 0) {
if (self.*.gfx[(width + data.x) + ((height + data.y) * 64)]) {
self.*.gfx[(width + data.x) + ((height + data.y) * 64)] = false;
self.*.V[0xF] = 1;
} else {
self.*.gfx[(width + data.x) + ((height + data.y) * 64)] = true;
self.*.V[0xF] = 0;
}
} else {
self.*.V[0xF] = 0;
}
}
}
self.*.pc += 2;
},
else => {
defer print("\n", .{});
// opcode not found
print("{x:0>4} - OPCODE NOT FOUND\n", .{self.*.opcode});
},
}
// Update timers
_ = self;
}
}
};
var chip8 = Chip8.new();
pub fn main() !void {
setup_graphics();
defer close_graphics();
var exitWindow: bool = false;
const execute_delay = 4;
var delay: u32 = 0;
chip8.load_game("roms/IBM_Logo.ch8");
while (!ray.WindowShouldClose() and !exitWindow) {
_ = shader_pixalizer.set_grid_size(1.35);
//_ = shader_stretcher.set_stretch_amount(slider / 100.0);
shader_stretcher.set_float(shader_stretcher_stretch_amount, slider / 100.0);
ray.BeginDrawing();
defer ray.EndDrawing();
exitWindow = ray.GuiWindowBox(ray.Rectangle{ .x = 0, .y = 0, .height = screenHeight, .width = screenWidth }, "CHIP-8");
var slider_buf: [5]u8 = undefined;
const slider_str = fmt.bufPrint(&slider_buf, "{d:02.2}", .{slider / 100.0}) catch |err| {
print("{}\n", .{err});
return;
};
slider = ray.GuiSlider(ray.Rectangle{ .x = 70, .y = 32 * scale + 24 + 10, .width = (64 * scale) - 128, .height = 30 }, "Pixel Size", @ptrCast([*c]const u8, slider_str), slider, slider_min, slider_max);
if (delay == execute_delay) {
chip8.emulate_cycles(1);
delay = 0;
} else {
delay += 1;
}
update_screen(&chip8_screen, chip8.gfx);
}
}
fn get_coords(x: u32, y: u32) u32 {
return y * 64 + x;
}
fn setup_graphics() void {
ray.InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
chip8_screen = ray.LoadRenderTexture(64, 32);
temp_canvas = ray.LoadRenderTexture(64 * scale, 32 * scale);
shader_stretcher = Shader.new(ray.LoadShader(0, "shaders/stretcher.fs"));
shader_stretcher_stretch_amount = ray.GetShaderLocation(shader_stretcher.shader, "stretch_amount");
shader_stretcher.set_float(ray.GetShaderLocation(shader_stretcher.shader, "renderWidth"), 64.0);
shader_stretcher.set_float(ray.GetShaderLocation(shader_stretcher.shader, "renderHeight"), 32.0);
shader_stretcher.set_float(shader_stretcher_stretch_amount, slider / 100.0);
shader_colour_splitter = ShaderColourSplitter.new(ray.LoadShader(0, "shaders/colour_splitter.fs"));
shader_pixalizer = ShaderPixalizer.new(ray.LoadShader(0, "shaders/pixalizer.fs"));
ray.SetTargetFPS(60);
}
fn close_graphics() void {
ray.UnloadShader(shader_pixalizer.shader);
ray.CloseWindow();
}
fn update_screen(screen: *ray.RenderTexture, pixels: [2048]bool) void {
ray.ClearBackground(ray.RAYWHITE);
ray.BeginTextureMode(screen.*);
for (pixels) |pixel, index_usize| {
var on: u8 = undefined;
if (pixel) {
on = 255;
} else {
on = 0;
}
var index: u32 = @intCast(u32, index_usize);
ray.DrawRectangleRec(ray.Rectangle{ .x = @intToFloat(f32, @mod(index, 64)), .y = @intToFloat(f32, (index / 64)), .width = 1, .height = 1 }, ray.Color{ .r = on, .g = on, .b = on, .a = 255 });
}
//ray.DrawRectangleRec(ray.Rectangle{ .x = 0, .y = 0, .width = 64, .height = 32 }, ray.WHITE);
ray.EndTextureMode();
ray.BeginTextureMode(temp_canvas);
ray.BeginShaderMode(shader_pixalizer.shader);
ray.DrawTexturePro(
screen.texture,
ray.Rectangle{ .x = 0, .y = 0, .width = @intToFloat(f32, screen.texture.width), .height = @intToFloat(f32, screen.texture.height) },
ray.Rectangle{ .x = 0, .y = 0, .width = 64 * scale, .height = 32 * scale },
ray.Vector2{ .x = 0, .y = 0 },
0,
ray.WHITE,
);
ray.EndShaderMode();
ray.EndTextureMode();
ray.BeginTextureMode(temp_canvas);
ray.BeginShaderMode(shader_colour_splitter.shader);
ray.DrawTexturePro(
temp_canvas.texture,
ray.Rectangle{ .x = 0, .y = 0, .width = @intToFloat(f32, temp_canvas.texture.width), .height = @intToFloat(f32, temp_canvas.texture.height) },
ray.Rectangle{ .x = 0, .y = 24, .width = 64 * scale, .height = 32 * scale },
ray.Vector2{ .x = 0, .y = 0 },
0,
ray.WHITE,
);
ray.EndShaderMode();
ray.EndTextureMode();
ray.BeginShaderMode(shader_stretcher.shader);
ray.DrawTexturePro(
temp_canvas.texture,
ray.Rectangle{ .x = 0, .y = 0, .width = @intToFloat(f32, temp_canvas.texture.width), .height = @intToFloat(f32, temp_canvas.texture.height) },
ray.Rectangle{ .x = 0, .y = 24, .width = 64 * scale, .height = 32 * scale },
ray.Vector2{ .x = 0, .y = 0 },
0,
ray.WHITE,
);
ray.EndShaderMode();
}
|