deleted old files and modified some dsa source files
This commit is contained in:
@@ -11,7 +11,7 @@ setup_idt:
|
||||
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
irt
|
||||
return
|
||||
|
||||
setup_hard_fault_handler:
|
||||
push bpr
|
||||
@@ -22,7 +22,7 @@ setup_hard_fault_handler:
|
||||
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
irt
|
||||
return
|
||||
|
||||
dw hard_fault_err: "FATAL: Illegal Instruction or Memory Access!"
|
||||
handle_hard_fault:
|
||||
|
||||
@@ -5,27 +5,20 @@ fib_n:
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8 // load arg
|
||||
mov rg1, rg2
|
||||
lwi 1, rg1
|
||||
lwi 0, rg1
|
||||
lwi 1, rg2
|
||||
|
||||
start:
|
||||
_start:
|
||||
add rg1, rg2, rg3
|
||||
|
||||
pusha 4
|
||||
push rg1
|
||||
call print::print_hex_byte
|
||||
call print::print_newline
|
||||
pop zero
|
||||
popa 4
|
||||
|
||||
mov rg2, rg1
|
||||
mov rg3, rg2
|
||||
|
||||
dec rg0
|
||||
cmp rg0, zero
|
||||
jgt start
|
||||
jgt _start
|
||||
|
||||
stw rg1, bpr, 8
|
||||
stw rg3, bpr, 8
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
dw global_arena_start: 0x30000
|
||||
dw global_arena_current: 0x30000
|
||||
dw global_arena_end: 0x40000
|
||||
|
||||
arena_alloc:
|
||||
// Just like bump allocator
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8 // size argument
|
||||
ldw global_arena_current, rg1
|
||||
|
||||
add rg1, rg0, rg2 // new_current = current + size
|
||||
ldw global_arena_end, rg3
|
||||
|
||||
cmp rg2, rg3
|
||||
jgt out_of_memory
|
||||
|
||||
stw rg2, global_arena_current
|
||||
mov rg1, acc // return old current
|
||||
stw acc, bpr, 8
|
||||
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
|
||||
arena_reset:
|
||||
// Reset to start
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw global_arena_start, rg0
|
||||
stw rg0, global_arena_current
|
||||
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
+44
-38
@@ -1,45 +1,51 @@
|
||||
include fib: "./lib/maths/fib.dsa"
|
||||
include maths: "./lib/maths/core.dsa"
|
||||
include print: "./lib/io/print.dsa"
|
||||
|
||||
dw idt: 0xFFFF0000
|
||||
// GENERATED BY DSC COMPILER
|
||||
// Generated at 2026-02-04 01:44:06
|
||||
|
||||
// Imports
|
||||
include print: "./lib/io/print.dsa"
|
||||
include fib: "./lib/maths/fib.dsa"
|
||||
|
||||
// Globals & Reserved Memory
|
||||
|
||||
|
||||
// Entry Point
|
||||
dw stack: 0x10000
|
||||
init:
|
||||
// setup interrupt handlers
|
||||
ldw idt, idr
|
||||
lwi handle_hard_fault, rg0
|
||||
stw rg0, idr, 4
|
||||
// set up a stack.
|
||||
db message: "Process Exited with code:"
|
||||
_init:
|
||||
ldw stack, bpr
|
||||
mov bpr, spr
|
||||
|
||||
dw string: "hello world"
|
||||
start:
|
||||
lwi 100, rg0
|
||||
lwi 10, rg1
|
||||
|
||||
push rg1
|
||||
push rg0
|
||||
call maths::new_divide
|
||||
pop rg0
|
||||
pop rg1
|
||||
hlt
|
||||
|
||||
pop rg0
|
||||
pop zero
|
||||
push rg0
|
||||
call print::print_num
|
||||
pop zero
|
||||
|
||||
hlt
|
||||
|
||||
// fault handler in case we fail DSA.
|
||||
dw hard_fault_err: "FATAL: Illegal Instruction or Memory Access!"
|
||||
handle_hard_fault:
|
||||
call print::clear
|
||||
call print::reset
|
||||
lwi hard_fault_err, rg0
|
||||
push zero
|
||||
call main
|
||||
call print::print_newline
|
||||
lwi message, rg0
|
||||
push rg0
|
||||
call print::print
|
||||
pop zero
|
||||
hlt
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
hlt
|
||||
|
||||
|
||||
// Return
|
||||
_ret:
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
|
||||
// Compiled Code Starts...
|
||||
main:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
lli 6, rg0
|
||||
push rg0 // bpr-4: x
|
||||
push rg0 // push arg 0
|
||||
call fib::fib_n
|
||||
pop rg1
|
||||
push rg1 // bpr-8: y
|
||||
push rg1 // push arg 0
|
||||
call print::print_num
|
||||
pop zero
|
||||
jmp _ret
|
||||
|
||||
|
||||
+172
-25
@@ -1,12 +1,14 @@
|
||||
|
||||
// GENERATED BY DSC COMPILER
|
||||
// Generated at 2026-02-03 02:08:02
|
||||
// Generated at 2026-02-03 23:37:16
|
||||
|
||||
// Imports
|
||||
include print: "./lib/io/print.dsa"
|
||||
|
||||
// Globals & Reserved Memory
|
||||
|
||||
dw heap_start: 196608
|
||||
dw heap_end: 262144
|
||||
dw heap_current: 196608
|
||||
|
||||
// Entry Point
|
||||
dw stack: 0x10000
|
||||
@@ -37,31 +39,176 @@ main:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
lli 5, rg0
|
||||
db str_1: "Hello world"
|
||||
lwi str_1, rg1
|
||||
db str_2: "test"
|
||||
lwi str_2, rg2
|
||||
push rg0
|
||||
push rg1
|
||||
push rg2
|
||||
db str_3: "hello world 2 electric boogaloo"
|
||||
lwi str_3, rg3
|
||||
push rg3
|
||||
call print::println
|
||||
pop zero
|
||||
lli 0, rg0
|
||||
push rg0 // bpr-4: x
|
||||
subi bpr 4 rg1
|
||||
lli 512, rg0
|
||||
push rg1 // bpr-8: y
|
||||
push rg0 // push arg 0
|
||||
call arena_create
|
||||
pop rg2
|
||||
pop rg1
|
||||
pop rg0
|
||||
push rg0
|
||||
push rg1
|
||||
push rg2
|
||||
lli 213, rg3
|
||||
push rg3
|
||||
lli 32, rg0
|
||||
push rg2 // bpr-12: alloc
|
||||
push rg0 // push arg 1
|
||||
push rg2 // push arg 0
|
||||
call arena_alloc
|
||||
pop rg3
|
||||
pop zero
|
||||
lli 32, rg0
|
||||
subi bpr 12 rg2
|
||||
ldw rg2, rg2 // bpr-20: alloc
|
||||
push rg3 // bpr-16: ptr1
|
||||
push rg2 // bpr-20: alloc
|
||||
push rg0 // push arg 1
|
||||
push rg2 // push arg 0
|
||||
call arena_alloc
|
||||
pop rg4
|
||||
pop zero
|
||||
subi bpr 20 rg0
|
||||
ldw rg0, rg0 // bpr-28: alloc
|
||||
push rg4 // bpr-24: ptr2
|
||||
push rg0 // bpr-28: alloc
|
||||
push rg0 // push arg 0
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
call print::print_newline
|
||||
subi bpr 16 rg0
|
||||
ldw rg0, rg0 // bpr-24: ptr1
|
||||
push rg0 // bpr-32: ptr1
|
||||
push rg0 // push arg 0
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
call print::print_newline
|
||||
subi bpr 24 rg0
|
||||
ldw rg0, rg0 // bpr-32: ptr2
|
||||
push rg0 // bpr-36: ptr2
|
||||
push rg0 // push arg 0
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
call print::print_newline
|
||||
subi bpr 36 rg0
|
||||
ldw rg0, rg0 // bpr-44: ptr2
|
||||
ldw rg0, rg2
|
||||
push rg0 // bpr-40: ptr2
|
||||
push rg2 // push arg 0
|
||||
call print::print_num
|
||||
pop zero
|
||||
pop rg2
|
||||
pop rg1
|
||||
pop rg0
|
||||
call print::print_newline
|
||||
lli 42, rg2
|
||||
subi bpr 40 rg5
|
||||
ldw rg5, rg5 // bpr-48: ptr2
|
||||
stw rg2, rg5
|
||||
push rg5 // bpr-44: ptr2
|
||||
push rg5 // push arg 0
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
call print::print_newline
|
||||
subi bpr 44 rg2
|
||||
ldw rg2, rg2 // bpr-52: ptr2
|
||||
ldw rg2, rg5
|
||||
push rg2 // bpr-48: ptr2
|
||||
push rg5 // push arg 0
|
||||
call print::print_num
|
||||
pop zero
|
||||
call print::print_newline
|
||||
db str_1: "end"
|
||||
lwi str_1, rg5
|
||||
push rg5 // push arg 0
|
||||
call print::println
|
||||
pop zero
|
||||
lli 0, rg5
|
||||
stw rg5, bpr, 8
|
||||
jmp _ret
|
||||
|
||||
arena_create:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lli 12, rg1
|
||||
add rg0, rg1, rg2
|
||||
ldw heap_current, rg1
|
||||
add rg1, rg2, rg3
|
||||
ldw heap_end, rg4
|
||||
cmp rg3, rg4
|
||||
lli 0, rg5
|
||||
jle _cmp_end_2
|
||||
lli 1, rg5
|
||||
_cmp_end_2:
|
||||
cmp rg5, zero
|
||||
jeq _else_4
|
||||
_then_3:
|
||||
lli 0, rg4
|
||||
stw rg4, bpr, 8
|
||||
jmp _ret
|
||||
jmp _end_5
|
||||
_else_4:
|
||||
nop
|
||||
_end_5:
|
||||
lli 12, rg4
|
||||
add rg1, rg4, rg5
|
||||
add rg1, rg2, rg4
|
||||
stw rg5, rg1
|
||||
lli 4, rg6
|
||||
add rg1, rg6, rg7
|
||||
stw rg5, rg7
|
||||
lli 8, rg6
|
||||
add rg1, rg6, rg7
|
||||
stw rg4, rg7
|
||||
stw rg3, heap_current
|
||||
stw rg1, bpr, 8
|
||||
jmp _ret
|
||||
|
||||
arena_alloc:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
ldw bpr, rg1, 12
|
||||
lli 4, rg2
|
||||
add rg0, rg2, rg3
|
||||
ldw rg3, rg2
|
||||
lli 8, rg3
|
||||
add rg0, rg3, rg4
|
||||
ldw rg4, rg3
|
||||
add rg2, rg1, rg4
|
||||
cmp rg4, rg3
|
||||
lli 0, rg5
|
||||
jle _cmp_end_6
|
||||
lli 1, rg5
|
||||
_cmp_end_6:
|
||||
cmp rg5, zero
|
||||
jeq _else_8
|
||||
_then_7:
|
||||
lli 0, rg5
|
||||
stw rg5, bpr, 8
|
||||
jmp _ret
|
||||
jmp _end_9
|
||||
_else_8:
|
||||
nop
|
||||
_end_9:
|
||||
lli 4, rg5
|
||||
add rg0, rg5, rg6
|
||||
stw rg4, rg6
|
||||
stw rg2, bpr, 8
|
||||
jmp _ret
|
||||
|
||||
arena_destroy:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lli 0, rg1
|
||||
stw rg1, bpr, 8
|
||||
jmp _ret
|
||||
|
||||
reset_all:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw heap_start, rg0
|
||||
stw rg0, heap_current
|
||||
lli 0, rg0
|
||||
stw rg0, bpr, 8
|
||||
jmp _ret
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,110 +0,0 @@
|
||||
fn main() -> u32 {
|
||||
|
||||
let x: u32 = 0;
|
||||
let y: u32 = &x;
|
||||
|
||||
let alloc: u32 = arena_create(512);
|
||||
let ptr1: u32 = arena_alloc(alloc, 32);
|
||||
let ptr2: u32 = arena_alloc(alloc, 32);
|
||||
|
||||
print_hex(alloc);
|
||||
print_newline();
|
||||
print_hex(ptr1);
|
||||
print_newline();
|
||||
print_hex(ptr2);
|
||||
print_newline();
|
||||
printnum(*ptr2);
|
||||
print_newline();
|
||||
*ptr2 = 42;
|
||||
|
||||
print_hex(ptr2);
|
||||
print_newline();
|
||||
printnum(*ptr2);
|
||||
print_newline();
|
||||
println("end");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Arena Allocator
|
||||
// Supports multiple arenas that can be destroyed independently
|
||||
// Much more practical than a simple bump allocator
|
||||
|
||||
// Global heap management
|
||||
static heap_start: u32 = 0x30000;
|
||||
static heap_end: u32 = 0x40000;
|
||||
static heap_current: u32 = 0x30000;
|
||||
|
||||
// Arena structure (stored at the start of each arena):
|
||||
// [0-3]: start_address (u32)
|
||||
// [4-7]: current_position (u32)
|
||||
// [8-11]: end_address (u32)
|
||||
// Total header size: 12 bytes
|
||||
|
||||
// Create a new arena with given size
|
||||
// Returns pointer to arena handle (or 0 if failed)
|
||||
fn arena_create(size: u32) -> u32 {
|
||||
let total_size: u32 = size + 12;
|
||||
let arena_ptr: u32 = heap_current;
|
||||
let new_current: u32 = arena_ptr + total_size;
|
||||
|
||||
// Check if we have space
|
||||
if new_current > heap_end {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Calculate arena data region
|
||||
let data_start: u32 = arena_ptr + 12;
|
||||
let data_end: u32 = arena_ptr + total_size;
|
||||
|
||||
// Initialize arena header
|
||||
// Note: In real implementation, you'd use pointer writes here
|
||||
// For now, using placeholder comments:
|
||||
*arena_ptr = data_start; // start_address
|
||||
*(arena_ptr + 4) = data_start; // current_position
|
||||
*(arena_ptr + 8) = data_end; // end_address
|
||||
|
||||
heap_current = new_current;
|
||||
|
||||
return arena_ptr;
|
||||
}
|
||||
|
||||
// Allocate from an arena
|
||||
// Returns pointer to allocated memory (or 0 if failed)
|
||||
fn arena_alloc(arena: u32, size: u32) -> u32 {
|
||||
// Read current position from arena
|
||||
let current: u32 = *(arena + 4);
|
||||
let end: u32 = *(arena + 8);
|
||||
|
||||
let new_current: u32 = current + size;
|
||||
|
||||
// Check if arena has space
|
||||
if new_current > end {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Update current position in arena
|
||||
*(arena + 4) = new_current;
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
// Destroy an arena (in bump allocator, this is a no-op)
|
||||
// In a real allocator, you'd mark the memory as free
|
||||
fn arena_destroy(arena: u32) {
|
||||
// In a true allocator, mark memory as reusable
|
||||
// For bump allocator, we can't reclaim memory
|
||||
// unless we destroy ALL arenas and reset
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Reset entire heap (destroys ALL arenas)
|
||||
fn reset_all() {
|
||||
heap_current = heap_start;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user