added section for file loading + memory cleanup

Make sure to clean up any memory allocated to the sim in terms of
disassembly instructions etc
This commit is contained in:
2026-06-18 08:25:29 +02:00
parent 1a6346895a
commit 6f701560df
2 changed files with 34 additions and 32 deletions
+6 -2
View File
@@ -115,6 +115,9 @@ run_gui :: proc(sim: ^Simulator) {
rl.UnloadSound(beep) rl.UnloadSound(beep)
rl.CloseAudioDevice() rl.CloseAudioDevice()
rl.CloseWindow() rl.CloseWindow()
// Free any allocated memory for the simulator
delete(sim.disasm)
} }
// @TODO: If this grows lets move it into its own file // @TODO: If this grows lets move it into its own file
@@ -128,6 +131,7 @@ calc_layout :: proc(screen_width: f32, screen_height: f32) -> Layout {
sidebar_width := screen_width * SIDEBAR_PERCENT sidebar_width := screen_width * SIDEBAR_PERCENT
display_width := screen_width - (sidebar_width * 2) - sidebar_width display_width := screen_width - (sidebar_width * 2) - sidebar_width
display_height := visible_height * DISPLAY_V_RATIO display_height := visible_height * DISPLAY_V_RATIO
keypad_height := (y_pos + visible_height - visible_height / 2)
x_center := sidebar_width x_center := sidebar_width
y_center := screen_width - sidebar_width * 2 y_center := screen_width - sidebar_width * 2
@@ -144,11 +148,11 @@ calc_layout :: proc(screen_width: f32, screen_height: f32) -> Layout {
x = 0, x = 0,
y = y_pos, y = y_pos,
width = sidebar_width, width = sidebar_width,
height = visible_height / 3 height = keypad_height - y_pos
}, },
keypad = rl.Rectangle{ keypad = rl.Rectangle{
x = 0, x = 0,
y = (y_pos + visible_height - visible_height / 2), y = keypad_height,
width = sidebar_width, width = sidebar_width,
height = visible_height / 2 height = visible_height / 2
}, },
+28 -30
View File
@@ -16,15 +16,17 @@ Instruction :: struct {
gui_file_loader :: proc(rect: rl.Rectangle, sim: ^Simulator) { gui_file_loader :: proc(rect: rl.Rectangle, sim: ^Simulator) {
rl.DrawRectangleLinesEx(rect, 1, rl.GRAY) rl.DrawRectangleLinesEx(rect, 1, rl.GRAY)
// FILE SECTION:
// ---------------------------------------------
top_bounds_height := (rect.height - (PADDING_Y * 2)) / 3
bounds := rl.Rectangle { bounds := rl.Rectangle {
x = rect.x + PADDING_X, x = rect.x + PADDING_X,
y = rect.y + PADDING_Y, y = rect.y + PADDING_Y,
width = rect.width - (PADDING_X * 2), width = rect.width - (PADDING_X * 2),
height = rect.height - (PADDING_Y * 2), height = top_bounds_height
} }
rl.GuiPanel(bounds, "Rom / File") rl.GuiPanel(bounds, nil)
// open rom button below drop-zone
btn_rect := rl.Rectangle { btn_rect := rl.Rectangle {
bounds.x, bounds.x,
bounds.y, bounds.y,
@@ -48,6 +50,9 @@ gui_file_loader :: proc(rect: rl.Rectangle, sim: ^Simulator) {
} }
sim.rom_loaded = true sim.rom_loaded = true
// Free old instructions on sim
delete(sim.disasm)
sim.disasm = disassemble_rom(sim.machine.memory[:], 0x200, 0x200 + u16(rom_size)) sim.disasm = disassemble_rom(sim.machine.memory[:], 0x200, 0x200 + u16(rom_size))
} }
@@ -63,7 +68,7 @@ gui_file_loader :: proc(rect: rl.Rectangle, sim: ^Simulator) {
// centered drop-zone text // centered drop-zone text
text: cstring = "Drop a CHIP-8 ROM here" text: cstring = "Drop a CHIP-8 ROM here"
text_width := rl.MeasureText(text, BIG_FONT_SIZE) text_width := rl.MeasureText(text, BIG_FONT_SIZE)
text_x := drop_zone.x + (drop_zone.width - f32(text_width)) / 2 text_x := drop_zone.x + (drop_zone.width - f32(text_width))
text_y := drop_zone.y + (drop_zone.height - f32(BIG_FONT_SIZE)) / 2 text_y := drop_zone.y + (drop_zone.height - f32(BIG_FONT_SIZE)) / 2
rl.DrawTextEx(sim.font, text, {text_x, text_y}, BIG_FONT_SIZE, 1, rl.WHITE) rl.DrawTextEx(sim.font, text, {text_x, text_y}, BIG_FONT_SIZE, 1, rl.WHITE)
@@ -76,13 +81,31 @@ gui_file_loader :: proc(rect: rl.Rectangle, sim: ^Simulator) {
if rl.CheckCollisionPointRec(mouse, drop_zone) { if rl.CheckCollisionPointRec(mouse, drop_zone) {
path_str := string(dropped_file.paths[0]) path_str := string(dropped_file.paths[0])
log.info("file dropped: ", path_str) log.info("file dropped: ", path_str)
// @TODO: Stop sim, reset mem etc, load new rom
} else { } else {
log.info("File dropped outside drop zone, ignoring") log.info("File dropped outside drop zone, ignoring")
} }
} }
rl.UnloadDroppedFiles(dropped_file) rl.UnloadDroppedFiles(dropped_file)
} }
// FOLDER SECTION:
// ---------------------------------------------
bottom_bounds := rl.Rectangle {
x = bounds.x,
y = bounds.y + top_bounds_height,
width = rect.width - (PADDING_X * 2),
height = rect.height - top_bounds_height - (PADDING_X * 2)
}
rl.GuiPanel(bottom_bounds, nil)
bottom_btn_rect := rl.Rectangle {
bottom_bounds.x,
bottom_bounds.y,
rect.width - (PADDING_X * 2),
BUTTON_HEIGHT,
}
rl.GuiButton(bottom_btn_rect, "Open ROM Folder")
} }
disassemble_rom :: proc(memory: []u8, start: u16, end_addr: u16) -> []Instruction { disassemble_rom :: proc(memory: []u8, start: u16, end_addr: u16) -> []Instruction {
@@ -167,28 +190,3 @@ disassemble_rom :: proc(memory: []u8, start: u16, end_addr: u16) -> []Instructio
return entries[:] return entries[:]
} }