load kernel into memory
This commit is contained in:
parent
f9992ee087
commit
b6c7546482
454
boot/boot.vc6asm
454
boot/boot.vc6asm
@ -1,8 +1,8 @@
|
|||||||
@include "arch.vc6asm"
|
@include "arch.vc6asm"
|
||||||
@include "macros.vc6asm"
|
@include "macros.vc6asm"
|
||||||
|
|
||||||
@const stack_base 0xfff
|
@const stack_base 0x1fff
|
||||||
@const heap_base 0x1000
|
@const heap_base 0x2000
|
||||||
|
|
||||||
@const block_size 0x200
|
@const block_size 0x200
|
||||||
|
|
||||||
@ -18,52 +18,83 @@ start:
|
|||||||
call println
|
call println
|
||||||
|
|
||||||
mov r1, 1
|
mov r1, 1
|
||||||
mov r2, 512
|
mov r2, block_size
|
||||||
jmp .l0_cond
|
jmp .l0_cond
|
||||||
.l0_body:
|
.l0_body:
|
||||||
call read_block
|
call read_block
|
||||||
|
|
||||||
add r1, 1
|
add r1, 1
|
||||||
add r2, 512
|
add r2, block_size
|
||||||
.l0_cond:
|
.l0_cond:
|
||||||
cmp r1, (BOOT_SIZE + 511) / 512
|
cmp r1, (BOOT_SIZE + block_size - 1) / block_size
|
||||||
jlt .l0_body
|
jl .l0_body
|
||||||
|
|
||||||
|
mov r1, .loading_kernel_str
|
||||||
|
call println
|
||||||
|
|
||||||
|
call fs_load
|
||||||
|
cmp r0, 0
|
||||||
|
jne .error
|
||||||
|
|
||||||
|
; load root inode
|
||||||
|
mov r1, 0
|
||||||
|
call fs_load_inode
|
||||||
|
|
||||||
|
mov r1, .kernel_filename
|
||||||
|
call fs_search_dir
|
||||||
|
cmp r0, 0
|
||||||
|
jne .error_no_kernel_file
|
||||||
|
|
||||||
|
; load /kernel.bin inode
|
||||||
|
mov r1, r0
|
||||||
|
call fs_load_inode
|
||||||
|
|
||||||
|
mov r1, 0x800
|
||||||
|
call fs_read
|
||||||
|
|
||||||
mov r1, .ok_str
|
mov r1, .ok_str
|
||||||
call println
|
call println
|
||||||
|
|
||||||
|
; mov r1, .entering_kernel_str
|
||||||
|
; call println
|
||||||
|
|
||||||
|
; jmp 0x800
|
||||||
|
|
||||||
|
jmp .spin
|
||||||
.spin:
|
.spin:
|
||||||
|
mov r0, 0xf0f0
|
||||||
hlt
|
hlt
|
||||||
jmp .spin
|
jmp .spin
|
||||||
|
|
||||||
|
.error_no_kernel_file:
|
||||||
|
mov r1, .no_kernel_file_str
|
||||||
|
call println
|
||||||
|
.error:
|
||||||
|
jmp abort
|
||||||
|
|
||||||
.splash_str:
|
.splash_str:
|
||||||
dw "VC6 BOOT LOADER\0"
|
dw "VC6 BOOT LOADER\0"
|
||||||
.ok_str:
|
.ok_str:
|
||||||
dw "OK\0"
|
dw "OK\0"
|
||||||
.loading_bootloader_str:
|
.loading_bootloader_str:
|
||||||
dw "Loading bootloader...\0"
|
dw "Loading bootloader.\0"
|
||||||
|
.loading_kernel_str:
|
||||||
read_block:
|
dw "Loading kernel.\0"
|
||||||
store [DISK_BLOCK_ID], r1
|
.entering_kernel_str:
|
||||||
store [DISK_ADDR], r2
|
dw "Entering kernel.\0"
|
||||||
mov r0, DISK_CTL_START
|
.no_kernel_file_str:
|
||||||
store [DISK_CTL], r0
|
dw "error: no kernel file\0"
|
||||||
ret
|
.kernel_filename:
|
||||||
|
dw "kernel.bin\0"
|
||||||
@const fs_superblock heap_base
|
|
||||||
@const fs_block_bitmap fs_superblock + block_size
|
|
||||||
|
|
||||||
@const fs_block_offset 4
|
|
||||||
|
|
||||||
fs_load:
|
|
||||||
mov r1, 4
|
|
||||||
mov r2, fs_superblock
|
|
||||||
call read_block
|
|
||||||
|
|
||||||
|
|
||||||
Display:
|
abort:
|
||||||
.x: dw 0
|
mov r1, .error_str
|
||||||
.y: dw 0
|
call println
|
||||||
|
.spin:
|
||||||
|
hlt
|
||||||
|
jmp .spin
|
||||||
|
.error_str:
|
||||||
|
dw "Error occured!\0"
|
||||||
|
|
||||||
println:
|
println:
|
||||||
push bp
|
push bp
|
||||||
@ -76,7 +107,7 @@ println:
|
|||||||
load r2, [r0]
|
load r2, [r0]
|
||||||
|
|
||||||
cmp r2, '\n'
|
cmp r2, '\n'
|
||||||
jeq .newline
|
je .newline
|
||||||
|
|
||||||
load r1, [Display.x]
|
load r1, [Display.x]
|
||||||
load r3, [Display.y]
|
load r3, [Display.y]
|
||||||
@ -89,7 +120,7 @@ println:
|
|||||||
add r1, 1
|
add r1, 1
|
||||||
|
|
||||||
cmp r1, 40
|
cmp r1, 40
|
||||||
jlt .no_newline
|
jl .no_newline
|
||||||
.newline:
|
.newline:
|
||||||
mov r1, 0
|
mov r1, 0
|
||||||
|
|
||||||
@ -104,7 +135,7 @@ println:
|
|||||||
.l0_cond:
|
.l0_cond:
|
||||||
load r1, [r0]
|
load r1, [r0]
|
||||||
cmp r1, '\0'
|
cmp r1, '\0'
|
||||||
jgt .l0_body
|
jg .l0_body
|
||||||
|
|
||||||
load r3, [Display.y]
|
load r3, [Display.y]
|
||||||
add r3, 40
|
add r3, 40
|
||||||
@ -118,6 +149,367 @@ println:
|
|||||||
pop bp
|
pop bp
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
Display:
|
||||||
|
.x: dw 0
|
||||||
|
.y: dw 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
; r1 block id
|
||||||
|
; r2 destination address
|
||||||
|
read_block:
|
||||||
|
store [DISK_BLOCK_ID], r1
|
||||||
|
store [DISK_ADDR], r2
|
||||||
|
mov r0, DISK_CTL_START
|
||||||
|
store [DISK_CTL], r0
|
||||||
|
ret
|
||||||
|
|
||||||
|
@const fs_superblock heap_base + block_size * 0
|
||||||
|
@const fs_bitmap heap_base + block_size * 1
|
||||||
|
@const fs_block0 heap_base + block_size * 2
|
||||||
|
@const fs_block1 heap_base + block_size * 3
|
||||||
|
@const fs_block2 heap_base + block_size * 4
|
||||||
|
|
||||||
|
@const fs_block_offset 4
|
||||||
|
|
||||||
|
@const sb_offset_magic 0
|
||||||
|
@const sb_offset_blocks_count 1
|
||||||
|
@const sb_offset_inodeblocks_count 2
|
||||||
|
@const sb_offset_inodeblocks 3
|
||||||
|
@const sb_offset_inodebitmap 35
|
||||||
|
|
||||||
|
; r1 block id
|
||||||
|
; r2 destination address
|
||||||
|
fs_read_block:
|
||||||
|
add r1, fs_block_offset
|
||||||
|
jmp read_block
|
||||||
|
|
||||||
|
; ---
|
||||||
|
; r0 status, 0 = ok
|
||||||
|
fs_load:
|
||||||
|
push bp
|
||||||
|
mov bp, sp
|
||||||
|
|
||||||
|
mov r1, 0
|
||||||
|
mov r2, fs_superblock
|
||||||
|
call fs_read_block
|
||||||
|
|
||||||
|
mov r1, 1
|
||||||
|
mov r2, fs_bitmap
|
||||||
|
call fs_read_block
|
||||||
|
|
||||||
|
load r0, [fs_superblock]
|
||||||
|
cmp r0, 0x5a5a
|
||||||
|
je .fs_found
|
||||||
|
mov r1, .no_fs_str
|
||||||
|
call println
|
||||||
|
mov r0, 1
|
||||||
|
jmp .leave
|
||||||
|
.fs_found:
|
||||||
|
|
||||||
|
mov r0, 0
|
||||||
|
.leave:
|
||||||
|
mov sp, bp
|
||||||
|
pop bp
|
||||||
|
ret
|
||||||
|
|
||||||
|
.no_fs_str:
|
||||||
|
dw "error: no filesystem found\0";
|
||||||
|
|
||||||
|
; r1 inode id
|
||||||
|
fs_load_inode:
|
||||||
|
push bp
|
||||||
|
mov bp, sp
|
||||||
|
|
||||||
|
sub sp, 4
|
||||||
|
; 0 inode_id
|
||||||
|
; 1 inode_block_idx
|
||||||
|
; 2 block_offset
|
||||||
|
; 3 block_id
|
||||||
|
|
||||||
|
store [sp], r1
|
||||||
|
|
||||||
|
; inode_block_idx = inode_id / inodes_per_block (32)
|
||||||
|
; = inode_id >> 5
|
||||||
|
load r1, [sp]
|
||||||
|
shr r1, 5
|
||||||
|
sto r0, r1, sp, 1
|
||||||
|
|
||||||
|
; block_offset = inode_id % inodes_per_block (32) * inode_size (16)
|
||||||
|
; = inode_id & 5 << 4
|
||||||
|
load r1, [sp]
|
||||||
|
and r1, 5
|
||||||
|
shl r1, 4
|
||||||
|
sto r0, r1, sp, 2
|
||||||
|
|
||||||
|
; block_id = superblock.inodeblocks[inode_block_idx]
|
||||||
|
ldo r1, sp, 1
|
||||||
|
add r1, fs_superblock + sb_offset_inodeblocks
|
||||||
|
load r1, [r1]
|
||||||
|
sto r0, r1, sp, 3
|
||||||
|
|
||||||
|
ldo r1, sp, 3
|
||||||
|
mov r2, fs_block0
|
||||||
|
call fs_read_block
|
||||||
|
|
||||||
|
mov r1, INode
|
||||||
|
ldo r2, sp, 2
|
||||||
|
add r2, fs_block0
|
||||||
|
mov r3, 16
|
||||||
|
call memcpy
|
||||||
|
|
||||||
|
.leave:
|
||||||
|
mov sp, bp
|
||||||
|
pop bp
|
||||||
|
ret
|
||||||
|
|
||||||
|
fs_maybe_load_ind_block:
|
||||||
|
load r0, [INode.blocks_count]
|
||||||
|
cmp r0, 11
|
||||||
|
jle .leave
|
||||||
|
|
||||||
|
mov r1, INode.blocks
|
||||||
|
add r1, 11
|
||||||
|
load r1, [r1]
|
||||||
|
mov r2, fs_block1
|
||||||
|
jmp fs_read_block
|
||||||
|
.leave:
|
||||||
|
ret
|
||||||
|
|
||||||
|
; r1 inode block idx
|
||||||
|
; r2 destination address
|
||||||
|
fs_inode_load_block:
|
||||||
|
load r0, [INode.blocks_count]
|
||||||
|
cmp r1, r0
|
||||||
|
jge .error_overflow
|
||||||
|
|
||||||
|
cmp r1, 11
|
||||||
|
jge .load_from_ind
|
||||||
|
|
||||||
|
add r1, INode.blocks
|
||||||
|
load r1, [r1]
|
||||||
|
jmp .read_block
|
||||||
|
|
||||||
|
.load_from_ind:
|
||||||
|
cmp r1, 11 + 512
|
||||||
|
jge .error_overflow
|
||||||
|
|
||||||
|
sub r1, 11
|
||||||
|
add r1, fs_block1
|
||||||
|
load r1, [r1]
|
||||||
|
|
||||||
|
.read_block:
|
||||||
|
call fs_read_block
|
||||||
|
|
||||||
|
ret
|
||||||
|
.error_overflow:
|
||||||
|
mov r1, .error_overflow_str
|
||||||
|
call println
|
||||||
|
jge abort
|
||||||
|
.error_overflow_str:
|
||||||
|
dw "error: i >= inode->blocks_count\0"
|
||||||
|
|
||||||
|
; r1 filename
|
||||||
|
; ---
|
||||||
|
; r0 status, 0 = found
|
||||||
|
fs_search_dir:
|
||||||
|
push bp
|
||||||
|
mov bp, sp
|
||||||
|
|
||||||
|
sub sp, 2
|
||||||
|
; 0 i
|
||||||
|
; 1 filename
|
||||||
|
; 2 filename_len
|
||||||
|
|
||||||
|
sto r0, r1, sp, 1
|
||||||
|
|
||||||
|
ldo r1, sp, 1
|
||||||
|
call strlen
|
||||||
|
sto r1, r0, sp, 2
|
||||||
|
|
||||||
|
call fs_maybe_load_ind_block
|
||||||
|
|
||||||
|
mov r0, 0
|
||||||
|
store [sp], r0
|
||||||
|
jmp .l0_cond
|
||||||
|
.l0_body:
|
||||||
|
|
||||||
|
load r1, [sp]
|
||||||
|
mov r2, fs_block2
|
||||||
|
call fs_inode_load_block
|
||||||
|
|
||||||
|
mov r0, 0
|
||||||
|
store [DirEntry.next], r0
|
||||||
|
|
||||||
|
.l1_body:
|
||||||
|
mov r1, DirEntry
|
||||||
|
load r2, [DirEntry.next]
|
||||||
|
add r2, fs_block2
|
||||||
|
mov r3, 64
|
||||||
|
call memcpy
|
||||||
|
|
||||||
|
ldo r0, sp, 2
|
||||||
|
load r1, [DirEntry.name_len]
|
||||||
|
cmp r0, r1
|
||||||
|
jne .not_a_match
|
||||||
|
mov r0, 0
|
||||||
|
jmp .leave
|
||||||
|
.not_a_match:
|
||||||
|
|
||||||
|
.l1_cond:
|
||||||
|
load r0, [DirEntry.next]
|
||||||
|
cmp r0, 0
|
||||||
|
jg .l1_body
|
||||||
|
|
||||||
|
load r0, [sp]
|
||||||
|
add r0, 1
|
||||||
|
store [sp], r0
|
||||||
|
.l0_cond:
|
||||||
|
load r0, [sp]
|
||||||
|
load r1, [INode.blocks_count]
|
||||||
|
cmp r0, r1
|
||||||
|
jl .l0_body
|
||||||
|
|
||||||
|
mov r0, 1
|
||||||
|
.leave:
|
||||||
|
mov sp, bp
|
||||||
|
pop bp
|
||||||
|
ret
|
||||||
|
|
||||||
|
; r1 destination address
|
||||||
|
; ---
|
||||||
|
; r0 blocks read
|
||||||
|
fs_read:
|
||||||
|
push bp
|
||||||
|
mov bp, sp
|
||||||
|
|
||||||
|
sub sp, 2
|
||||||
|
; 0 i
|
||||||
|
; 1 current address
|
||||||
|
|
||||||
|
sto r0, r1, sp, 1
|
||||||
|
|
||||||
|
call fs_maybe_load_ind_block
|
||||||
|
|
||||||
|
mov r0, 0
|
||||||
|
store [sp], r0
|
||||||
|
jmp .l0_cond
|
||||||
|
.l0_body:
|
||||||
|
|
||||||
|
load r1, [sp]
|
||||||
|
ldo r2, sp, 1
|
||||||
|
call fs_inode_load_block
|
||||||
|
|
||||||
|
ldo r2, sp, 1
|
||||||
|
add r2, block_size
|
||||||
|
sto r0, r2, sp, 1
|
||||||
|
|
||||||
|
load r0, [sp]
|
||||||
|
add r0, 1
|
||||||
|
store [sp], r0
|
||||||
|
.l0_cond:
|
||||||
|
load r0, [sp]
|
||||||
|
load r1, [INode.blocks_count]
|
||||||
|
cmp r0, r1
|
||||||
|
jl .l0_body
|
||||||
|
|
||||||
|
mov sp, bp
|
||||||
|
pop bp
|
||||||
|
ret
|
||||||
|
|
||||||
|
INode:
|
||||||
|
.id: dw 0
|
||||||
|
.mode: dw 0
|
||||||
|
.blocks_count: dw 0
|
||||||
|
.last_block_size: dw 0
|
||||||
|
.blocks: resw 12
|
||||||
|
|
||||||
|
DirEntry:
|
||||||
|
.inode: dw 0
|
||||||
|
.next: dw 0
|
||||||
|
.name_len: dw 0
|
||||||
|
.name: resw 61
|
||||||
|
|
||||||
|
; r1 destination
|
||||||
|
; r2 source
|
||||||
|
; r3 size
|
||||||
|
memcpy:
|
||||||
|
jmp .l0_cond
|
||||||
|
.l0_body:
|
||||||
|
load r0, [r2]
|
||||||
|
store [r1], r0
|
||||||
|
|
||||||
|
add r1, 1
|
||||||
|
add r2, 1
|
||||||
|
sub r3, 1
|
||||||
|
.l0_cond:
|
||||||
|
cmp r3, 0
|
||||||
|
jg .l0_body
|
||||||
|
ret
|
||||||
|
|
||||||
|
; r1 destination
|
||||||
|
; r2 value
|
||||||
|
; r3 size
|
||||||
|
memset:
|
||||||
|
jmp .l0_cond
|
||||||
|
.l0_body:
|
||||||
|
store [r1], r2
|
||||||
|
|
||||||
|
add r1, 1
|
||||||
|
sub r3, 1
|
||||||
|
.l0_cond:
|
||||||
|
cmp r3, 0
|
||||||
|
jg .l0_body
|
||||||
|
ret
|
||||||
|
|
||||||
|
; r1 a
|
||||||
|
; r2 b
|
||||||
|
; r3 len
|
||||||
|
; ---
|
||||||
|
; r0 if equal
|
||||||
|
strneq:
|
||||||
|
push r1
|
||||||
|
jmp .l0_cond
|
||||||
|
.l0_body:
|
||||||
|
load r1, [sp]
|
||||||
|
load r1, [r1]
|
||||||
|
load r0, [r2]
|
||||||
|
|
||||||
|
cmp r1, r0
|
||||||
|
je .equal
|
||||||
|
mov r0, 0
|
||||||
|
jmp .leave
|
||||||
|
.equal:
|
||||||
|
|
||||||
|
add r1, 1
|
||||||
|
add r2, 1
|
||||||
|
|
||||||
|
store [sp], r1
|
||||||
|
|
||||||
|
sub r3, 1
|
||||||
|
.l0_cond:
|
||||||
|
cmp r3, 0
|
||||||
|
jg .l0_body
|
||||||
|
mov r0, 1
|
||||||
|
.leave:
|
||||||
|
pop r1
|
||||||
|
ret
|
||||||
|
|
||||||
|
; r1 str
|
||||||
|
; ---
|
||||||
|
; r0 len
|
||||||
|
strlen:
|
||||||
|
mov r0, 0
|
||||||
|
jmp .l0_cond
|
||||||
|
.l0_body:
|
||||||
|
add r1, 1
|
||||||
|
add r0, 1
|
||||||
|
.l0_cond:
|
||||||
|
load r2, [r1]
|
||||||
|
cmp r2, '\0'
|
||||||
|
jne .l0_body
|
||||||
|
ret
|
||||||
|
|
||||||
@const BOOT_SIZE $
|
@const BOOT_SIZE $
|
||||||
@static_assert 0x800 - BOOT_SIZE & 0x8000 ^ 0x8000 ; BOOT_SIZE <= 0x800
|
@static_assert 0x800 - BOOT_SIZE & 0x8000 ^ 0x8000 ; BOOT_SIZE <= 0x800
|
||||||
|
|
||||||
|
|||||||
@ -21,18 +21,18 @@
|
|||||||
xor fl, fl
|
xor fl, fl
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@macro maskeq 0
|
@macro maske 0
|
||||||
; xxp
|
; xxp
|
||||||
and fl, FL_EQ ; 00p
|
and fl, FL_EQ ; 00p
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@macro masklt 0
|
@macro maskl 0
|
||||||
; xpx
|
; xpx
|
||||||
and fl, FL_LT ; 0p0
|
and fl, FL_LT ; 0p0
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
@macro maskgt 0
|
@macro maskg 0
|
||||||
; xpp
|
; xpp
|
||||||
and fl, FL_EQ | FL_LT ; 0pp
|
and fl, FL_EQ | FL_LT ; 0pp
|
||||||
cmp fl, 0 ; xxp
|
cmp fl, 0 ; xxp
|
||||||
@ -40,19 +40,19 @@
|
|||||||
@end
|
@end
|
||||||
|
|
||||||
@macro maskne 0
|
@macro maskne 0
|
||||||
maskeq ; 00p
|
maske ; 00p
|
||||||
not fl ; 11q
|
not fl ; 11q
|
||||||
and fl, FL_EQ ; 00q
|
and fl, FL_EQ ; 00q
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@macro maskgte 0
|
@macro maskge 0
|
||||||
masklt ; 0p0
|
maskl ; 0p0
|
||||||
not fl ; 1q1
|
not fl ; 1q1
|
||||||
and fl, FL_LT ; 0q0
|
and fl, FL_LT ; 0q0
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@macro masklte 0
|
@macro maskle 0
|
||||||
maskgt ; 00p
|
maskg ; 00p
|
||||||
not fl ; 11q
|
not fl ; 11q
|
||||||
and fl, FL_EQ ; 00q
|
and fl, FL_EQ ; 00q
|
||||||
@end
|
@end
|
||||||
@ -109,8 +109,8 @@
|
|||||||
mov ip, %0
|
mov ip, %0
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@macro jeq 1
|
@macro je 1
|
||||||
maskeq
|
maske
|
||||||
cmov ip, %0
|
cmov ip, %0
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@ -119,23 +119,23 @@
|
|||||||
cmov ip, %0
|
cmov ip, %0
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@macro jlt 1
|
@macro jl 1
|
||||||
masklt
|
maskl
|
||||||
cmov ip, %0
|
cmov ip, %0
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@macro jlte 1
|
@macro jle 1
|
||||||
masklte
|
maskle
|
||||||
cmov ip, %0
|
cmov ip, %0
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@macro jgt 1
|
@macro jg 1
|
||||||
maskgt
|
maskg
|
||||||
cmov ip, %0
|
cmov ip, %0
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@macro jgte 1
|
@macro jge 1
|
||||||
maskgte
|
maskge
|
||||||
cmov ip, %0
|
cmov ip, %0
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
@org 0x0800
|
@org 0x0800
|
||||||
|
|
||||||
main:
|
main:
|
||||||
|
mov r0, 0xa0a0
|
||||||
mov r1, 'V'
|
mov r1, 'V'
|
||||||
store [VCD_BEGIN], r1
|
store [VCD_BEGIN], r1
|
||||||
mov r1, 'C'
|
mov r1, 'C'
|
||||||
|
|||||||
39
src/asm.c
39
src/asm.c
@ -634,6 +634,9 @@ typedef struct {
|
|||||||
size_t ip;
|
size_t ip;
|
||||||
Map syms;
|
Map syms;
|
||||||
char *current_label;
|
char *current_label;
|
||||||
|
char **filenames;
|
||||||
|
size_t filenames_cap;
|
||||||
|
size_t filenames_size;
|
||||||
ARRAY_FIELD(IncludeExpansion, incl_expans);
|
ARRAY_FIELD(IncludeExpansion, incl_expans);
|
||||||
ARRAY_FIELD(Macro, macros);
|
ARRAY_FIELD(Macro, macros);
|
||||||
Map macro_map;
|
Map macro_map;
|
||||||
@ -653,6 +656,7 @@ typedef struct {
|
|||||||
void assembler_init(Assembler *a, Lexer *lexer)
|
void assembler_init(Assembler *a, Lexer *lexer)
|
||||||
{
|
{
|
||||||
size_t output_size = 65535;
|
size_t output_size = 65535;
|
||||||
|
size_t filenames_cap = 128;
|
||||||
|
|
||||||
*a = (Assembler) {
|
*a = (Assembler) {
|
||||||
.lexer = lexer,
|
.lexer = lexer,
|
||||||
@ -662,6 +666,9 @@ void assembler_init(Assembler *a, Lexer *lexer)
|
|||||||
.ip = 0,
|
.ip = 0,
|
||||||
.syms = { },
|
.syms = { },
|
||||||
.current_label = nullptr,
|
.current_label = nullptr,
|
||||||
|
.filenames = malloc(filenames_cap),
|
||||||
|
.filenames_cap = filenames_cap,
|
||||||
|
.filenames_size = 0,
|
||||||
ARRAY_FIELD_INIT(incl_expans),
|
ARRAY_FIELD_INIT(incl_expans),
|
||||||
ARRAY_FIELD_INIT(macros),
|
ARRAY_FIELD_INIT(macros),
|
||||||
.macro_map = { },
|
.macro_map = { },
|
||||||
@ -710,8 +717,10 @@ void assembler_destroy(Assembler *a)
|
|||||||
map_destroy(&a->syms);
|
map_destroy(&a->syms);
|
||||||
if (a->current_label)
|
if (a->current_label)
|
||||||
free(a->current_label);
|
free(a->current_label);
|
||||||
|
for (size_t i = 0; i < a->filenames_size; ++i)
|
||||||
|
free(a->filenames[i]);
|
||||||
|
free(a->filenames);
|
||||||
for (size_t i = 0; i < a->incl_expans_size; ++i) {
|
for (size_t i = 0; i < a->incl_expans_size; ++i) {
|
||||||
free(a->incl_expans[i].filename);
|
|
||||||
free(a->incl_expans[i].text);
|
free(a->incl_expans[i].text);
|
||||||
}
|
}
|
||||||
if (a->incl_expans)
|
if (a->incl_expans)
|
||||||
@ -783,7 +792,6 @@ static void a_step_without_expand(Assembler *a)
|
|||||||
if (a->directive_depth != 0)
|
if (a->directive_depth != 0)
|
||||||
print_error_exit(a->tok.loc, "expected '@end'");
|
print_error_exit(a->tok.loc, "expected '@end'");
|
||||||
|
|
||||||
free(expan->filename);
|
|
||||||
free(expan->text);
|
free(expan->text);
|
||||||
a->directive_depth = expan->prev_directive_depth;
|
a->directive_depth = expan->prev_directive_depth;
|
||||||
a->incl_expans_size -= 1;
|
a->incl_expans_size -= 1;
|
||||||
@ -931,14 +939,15 @@ static int64_t a_parse_operand(Assembler *a)
|
|||||||
ident = combined;
|
ident = combined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Sym *v = map_get(&a->syms, ident);
|
||||||
|
if (!v) {
|
||||||
if (!a->second_pass) {
|
if (!a->second_pass) {
|
||||||
free(ident);
|
free(ident);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Sym *v = map_get(&a->syms, ident);
|
|
||||||
if (!v)
|
|
||||||
print_error_exit(ident_tok.loc, "undefined identifier '%s'", ident);
|
print_error_exit(ident_tok.loc, "undefined identifier '%s'", ident);
|
||||||
|
}
|
||||||
free(ident);
|
free(ident);
|
||||||
return v->val;
|
return v->val;
|
||||||
}
|
}
|
||||||
@ -948,16 +957,21 @@ static int64_t a_parse_operand(Assembler *a)
|
|||||||
print_error_exit(a->tok.loc, "expected identifier");
|
print_error_exit(a->tok.loc, "expected identifier");
|
||||||
Tok ident_tok = a->tok;
|
Tok ident_tok = a->tok;
|
||||||
a_step(a);
|
a_step(a);
|
||||||
if (!a->second_pass)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
char *ident = strndup(ident_tok.text, ident_tok.len);
|
char *ident = strndup(ident_tok.text, ident_tok.len);
|
||||||
char *unique_ident = alloc_sprintf("%s.%s", a->current_label, ident);
|
char *unique_ident = alloc_sprintf("%s.%s", a->current_label, ident);
|
||||||
|
|
||||||
Sym *v = map_get(&a->syms, unique_ident);
|
Sym *v = map_get(&a->syms, unique_ident);
|
||||||
if (!v)
|
if (!v) {
|
||||||
|
if (!a->second_pass) {
|
||||||
|
free(ident);
|
||||||
|
free(unique_ident);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
print_error_exit(
|
print_error_exit(
|
||||||
ident_tok.loc, "undefined identifier '%s'", unique_ident);
|
ident_tok.loc, "undefined identifier '%s'", unique_ident);
|
||||||
|
}
|
||||||
free(ident);
|
free(ident);
|
||||||
free(unique_ident);
|
free(unique_ident);
|
||||||
return v->val;
|
return v->val;
|
||||||
@ -1020,7 +1034,7 @@ static void a_sym_define(Assembler *a, const char *ident, Loc loc, int64_t val)
|
|||||||
Sym *found = map_get(&a->syms, ident);
|
Sym *found = map_get(&a->syms, ident);
|
||||||
if (!a->second_pass && found) {
|
if (!a->second_pass && found) {
|
||||||
print_error(loc, "redefinitions of '%s'", ident);
|
print_error(loc, "redefinitions of '%s'", ident);
|
||||||
print_info(found->loc, "'%s' defined here (%ld)", ident, found->val);
|
print_info(found->loc, "macro defined here (%ld)", found->val);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1146,6 +1160,10 @@ static void a_parse_directive_include(Assembler *a)
|
|||||||
size_t included_filename_len = strlen(current_dir) + 1 + strlen(str_val);
|
size_t included_filename_len = strlen(current_dir) + 1 + strlen(str_val);
|
||||||
char *filename = calloc(included_filename_len + 1, sizeof(char));
|
char *filename = calloc(included_filename_len + 1, sizeof(char));
|
||||||
|
|
||||||
|
assert(a->filenames_size < a->filenames_cap);
|
||||||
|
a->filenames[a->filenames_size] = filename;
|
||||||
|
a->filenames_size += 1;
|
||||||
|
|
||||||
snprintf(
|
snprintf(
|
||||||
filename, included_filename_len + 1, "%s/%s", current_dir, str_val);
|
filename, included_filename_len + 1, "%s/%s", current_dir, str_val);
|
||||||
free(str_val);
|
free(str_val);
|
||||||
@ -1498,12 +1516,11 @@ static void a_parse_macro_expan(Assembler *a, size_t macro_idx, Loc loc)
|
|||||||
size_t *overload_idx = map_get(&a->macro_overload_map, overload_ident);
|
size_t *overload_idx = map_get(&a->macro_overload_map, overload_ident);
|
||||||
if (!overload_idx) {
|
if (!overload_idx) {
|
||||||
print_error(loc,
|
print_error(loc,
|
||||||
"macro '%s' (%d) not defined",
|
"macro '%s' not defined with %d parameters",
|
||||||
macro->ident,
|
macro->ident,
|
||||||
expan.args_size);
|
expan.args_size);
|
||||||
print_info(macro->loc,
|
print_info(macro->loc,
|
||||||
"macro '%s' (%d) defined here (different argument count)",
|
"macro defined here with %d parameters",
|
||||||
macro->ident,
|
|
||||||
macro->arg_count);
|
macro->arg_count);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -130,6 +130,7 @@ static void vm_disk_control(VM *vm, VMState *s, uint16_t control)
|
|||||||
if (is_write) {
|
if (is_write) {
|
||||||
vm->disk->write_block(vm->disk, &s->memory[addr], block_id);
|
vm->disk->write_block(vm->disk, &s->memory[addr], block_id);
|
||||||
} else {
|
} else {
|
||||||
|
printf("reading block %d into %x\n", block_id, addr);
|
||||||
vm->disk->read_block(vm->disk, &s->memory[addr], block_id);
|
vm->disk->read_block(vm->disk, &s->memory[addr], block_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -213,7 +214,7 @@ static int vm_run_op(VM *vm, VMState *s, const Op *op)
|
|||||||
void vm_run(VM *vm)
|
void vm_run(VM *vm)
|
||||||
{
|
{
|
||||||
// constexpr int ins_per_block = 512;
|
// constexpr int ins_per_block = 512;
|
||||||
constexpr int ins_per_block = 1;
|
constexpr int ins_per_block = 32;
|
||||||
|
|
||||||
vm->disk->read_block(vm->disk, vm->state.memory, 0);
|
vm->disk->read_block(vm->disk, vm->state.memory, 0);
|
||||||
|
|
||||||
@ -242,7 +243,7 @@ void vm_run(VM *vm)
|
|||||||
size_t op_size = op_decode(&op, s.memory, s.memory_size, *ip);
|
size_t op_size = op_decode(&op, s.memory, s.memory_size, *ip);
|
||||||
|
|
||||||
ok = op_size > 0;
|
ok = op_size > 0;
|
||||||
op_print_line(&op, *ip);
|
// op_print_line(&op, *ip);
|
||||||
*ip += op_size;
|
*ip += op_size;
|
||||||
|
|
||||||
if (ok)
|
if (ok)
|
||||||
|
|||||||
8
src/fs.c
8
src/fs.c
@ -699,16 +699,16 @@ void fs_dump_block_char(Fs *fs, uint16_t block_id)
|
|||||||
constexpr size_t values_per_row = 16;
|
constexpr size_t values_per_row = 16;
|
||||||
for (size_t i = 0; i < block_size; i += values_per_row) {
|
for (size_t i = 0; i < block_size; i += values_per_row) {
|
||||||
for (size_t j = 0; j < values_per_row; ++j) {
|
for (size_t j = 0; j < values_per_row; ++j) {
|
||||||
char ch = block[i + j];
|
char ch = (char)block[i + j];
|
||||||
printf("%s", j ? " " : "");
|
printf("%s", j ? " " : "");
|
||||||
if (ch == ' ')
|
if (ch == ' ')
|
||||||
printf("' '");
|
printf("' '");
|
||||||
else if (ch > ' ')
|
else if (ch > ' ')
|
||||||
printf("%2c ", ch);
|
printf("%2c ", ch);
|
||||||
else if (conversions[(size_t)ch])
|
else if (conversions[(unsigned char)ch])
|
||||||
printf("\\%-2c", conversions[(size_t)ch]);
|
printf("\\%-2c", conversions[(unsigned char)ch]);
|
||||||
else
|
else
|
||||||
printf("\\x%-2x", ch);
|
printf("\\x%-2x", (unsigned char)ch);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/fstool.c
16
src/fstool.c
@ -145,6 +145,22 @@ int main(int argc, char *argv[])
|
|||||||
resolve_path(fs, &parent, path_copy);
|
resolve_path(fs, &parent, path_copy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr size_t entries_cap = 16;
|
||||||
|
FsListEntry entries[entries_cap];
|
||||||
|
|
||||||
|
size_t entries_size = fs_list(fs, entries, entries_cap, &parent);
|
||||||
|
assert(entries_size < entries_cap);
|
||||||
|
|
||||||
|
uint16_t inode_id = search_entries(entries, entries_size, name);
|
||||||
|
if (inode_id) {
|
||||||
|
printf("file %s already exists in %s\n",
|
||||||
|
name,
|
||||||
|
*path_copy ? path_copy : "/");
|
||||||
|
free(path_copy);
|
||||||
|
close_without_saving(fs, &disk);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
printf("creating file %s in %s\n", name, *path_copy ? path_copy : "/");
|
printf("creating file %s in %s\n", name, *path_copy ? path_copy : "/");
|
||||||
INode inode;
|
INode inode;
|
||||||
fs_make_file(fs, &inode, &parent, name);
|
fs_make_file(fs, &inode, &parent, name);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user