summaryrefslogtreecommitdiffhomepage
path: root/src/main.zig
blob: 20202d0c5f4fb71f63b412357da04f9c4f980e68 (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
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 screenWidth: u32 = 64 * 10;
const screenHeight: u32 = (32 * 10) + 24;
const RndGen = std.rand.DefaultPrng;
var Rnd = RndGen.init(0);
var chip8_screen: ray.RenderTexture = undefined;

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;
        const chars_read = file.readAll(&buf) catch |err| {
            print("{}\n", .{err});
            return;
        };

        print("{s}\n", .{buf});
        for (buf) |char, index| {
            if (index == chars_read) {
                break;
            }
            print("{}: {}\n", .{ index, char });
            self.*.memory[index + 512] = char;
        }
    }

    pub fn emulate_cycles(self: *Chip8, cycles: u32) void {
        var i: u32 = cycles;
        while (i > 0) : (i -= 1) {
            //print("{}\n", .{i});
            // Fetch Opcode
            self.*.opcode = @intCast(u16, self.*.memory[self.*.pc]) << 8 | self.*.memory[self.*.pc + 1];
            // Decode Opcode
            // Execute Opcode
            switch (self.*.opcode & 0xF000) {
                0x0000 => { // 00E0: Clear Screen
                    self.*.gfx = [_]bool{false} ** (64 * 32);
                    self.*.pc += 2;
                },
                0xA000 => { // ANNN: Set I to adress NNN
                    self.*.I = self.*.opcode & 0x0FFF;
                    self.*.pc += 2;
                },
                0x1000 => { // 1NNN: Jump to NNN
                    //self.*.stack[self.*.sp] = self.*.pc;
                    self.*.pc = self.*.opcode & 0x0FFF;
                },
                0x6000 => { // 6XNN: Set VX to NN
                    self.*.V[@intCast(u8, self.*.opcode & 0x0F00 >> 8)] = @intCast(u8, self.*.opcode & 0x00FF);
                    self.*.pc += 2;
                },
                0x7000 => { // 7XNN: Add NN to VX (dont change carry flag)
                    self.*.V[@intCast(u8, self.*.opcode & 0x0F00 >> 8)] += @intCast(u8, self.*.opcode & 0x00FF);
                    self.*.pc += 2;
                },
                0xD000 => { // DXYN: Draw a sprite
                    const x: u16 = self.*.V[@intCast(u16, self.*.opcode & 0x0F00 >> 8)];
                    const y: u16 = self.*.V[@intCast(u16, self.*.opcode & 0x00F0 >> 4)];
                    const height: u16 = @intCast(u16, self.*.opcode & 0x000F);
                    var pixel: u16 = undefined;

                    self.*.V[0xF] = 0;
                    var yline: u32 = 0;
                    while (yline < height) : (yline += 1) {
                        pixel = self.*.memory[self.*.I + yline];
                        var xline: u32 = 0;
                        while (xline < 8) : (xline += 1) {
                            //while (times(&eight)) |width| {
                            if ((pixel & (@intCast(u32, 0x80) >> @intCast(u5, xline))) != 0) {
                                if (self.*.gfx[(x + xline + ((y + yline) * 64))] == true) {
                                    self.*.V[0xF] = 1;
                                }
                                self.*.gfx[x + xline + ((y + yline) * 64)] = true;
                            }
                        }
                    }
                    self.*.pc += 2;
                },
                else => {
                    // opcode not found
                },
            }
            // Update timers
            _ = self;
        }
    }
};

var chip8 = Chip8.new();

pub fn main() !void {
    setup_graphics();
    defer ray.CloseWindow();
    var exitWindow: bool = false;

    chip8.load_game("roms/IBM_Logo.ch8");

    while (!ray.WindowShouldClose() and !exitWindow) {
        ray.BeginDrawing();
        defer ray.EndDrawing();

        exitWindow = ray.GuiWindowBox(ray.Rectangle{ .x = 0, .y = 0, .height = screenHeight, .width = screenWidth }, "CHIP-8");

        chip8.emulate_cycles(1);

        update_screen(&chip8_screen, chip8.gfx);
    }
}

fn pow(base: u32, power: u32) u32 {
    var iter: u32 = 0;
    var result: u32 = base;
    while (iter < power) : (iter += 1) {
        result = result * base;
    }
    return result;
}

fn get_coords(x: u32, y: u32) u32 {
    return y * 64 + x;
}

fn setup_graphics() void {

    //for (chip8_screen) |*pixel, index_usize| {
    //    var index: u32 = @intCast(u32, index_usize);
    //    pixel.* = chip8_pixel{ .location = ray.Rectangle{ .x = 10 * @intToFloat(f32, @mod(index, 64)), .y = 24 + 10 * @intToFloat(f32, (index / 64)), .width = 10, .height = 10 } };
    //}

    ray.InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
    chip8_screen = ray.LoadRenderTexture(64, 32);
    ray.SetTargetFPS(6);
}

fn update_screen(screen: *ray.RenderTexture, pixels: [2048]bool) void {
    ray.ClearBackground(ray.RAYWHITE);
    ray.BeginTextureMode(screen.*);
    for (pixels) |pixel, index_usize| {
        //var rnd: u8 = Rnd.random().int(u8);
        //if (rnd < (256 / 2)) {
        //    rnd = 255;
        //} else {
        //    rnd = 0;
        //}

        var on: u8 = undefined;
        if (pixel) {
            on = 255;
        } else {
            on = 0;
        }

        var index: u32 = @intCast(u32, index_usize);
        //var index_char: u8 = @intCast(u8, @mod(@intCast(u32, index_usize), 256));
        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.EndTextureMode();
    ray.DrawTextureEx(screen.texture, ray.Vector2{ .x = 0, .y = 24 }, 0, 10, ray.WHITE);
}