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
+28 -30
View File
@@ -16,15 +16,17 @@ Instruction :: struct {
gui_file_loader :: proc(rect: rl.Rectangle, sim: ^Simulator) {
rl.DrawRectangleLinesEx(rect, 1, rl.GRAY)
// FILE SECTION:
// ---------------------------------------------
top_bounds_height := (rect.height - (PADDING_Y * 2)) / 3
bounds := rl.Rectangle {
x = rect.x + PADDING_X,
y = rect.y + PADDING_Y,
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 {
bounds.x,
bounds.y,
@@ -48,6 +50,9 @@ gui_file_loader :: proc(rect: rl.Rectangle, sim: ^Simulator) {
}
sim.rom_loaded = true
// Free old instructions on sim
delete(sim.disasm)
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
text: cstring = "Drop a CHIP-8 ROM here"
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
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) {
path_str := string(dropped_file.paths[0])
log.info("file dropped: ", path_str)
// @TODO: Stop sim, reset mem etc, load new rom
} else {
log.info("File dropped outside drop zone, ignoring")
}
}
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 {
@@ -167,28 +190,3 @@ disassemble_rom :: proc(memory: []u8, start: u16, end_addr: u16) -> []Instructio
return entries[:]
}