Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

realloc refectoring and still drafting floppy

  • Loading branch information...
commit d25df52b8936de4d17bf9e4cde81eabf45f25b6f 1 parent 4d146fc
orlof authored
View
18 array.dasm16
@@ -2,24 +2,6 @@
; struct handle *array_create(uint16 len, uint16 type)
; ugly hack to mimic call conventions
;----------------------------------------------------------------
-:array_create2
- set push, a
-
- set push, [sp + 3] ; size
- add [sp], 1
- set push, [sp + 3] ; type
- jsr alloc
-
- set a, [x]
- set [a], [sp + 3]
-
- ; restore stack
- set a, pop
- set [sp], pop
- set [sp], pop
-
- set pc, pop
-
:array_create
set push, [sp + 2] ; size
add [sp], 1
View
5 data.dasm16
@@ -89,10 +89,11 @@
:STR_EX DAT STR_EX + 2, TYPE_NAME, 2, "ex"
:STR_ME DAT STR_ME + 2, TYPE_NAME, 2, "me"
-:STR_TRUE DAT STR_TRUE+2, TYPE_STRING, 4, "true"
-:STR_FALSE DAT STR_FALSE+2, TYPE_STRING, 5, "false"
+:STR_TRUE DAT STR_TRUE + 2, TYPE_STRING, 4, "true"
+:STR_FALSE DAT STR_FALSE + 2, TYPE_STRING, 5, "false"
:STR_ERROR DAT STR_ERROR + 2, TYPE_STRING, 5, "ERROR"
:STR_PROMPT DAT STR_PROMPT + 2, TYPE_STRING, 1, ">"
+:STR_FS_ROOT DAT STR_FS_ROOT + 2, TYPE_STRING, 1, "/"
:STR_EMPTY DAT STR_EMPTY + 2, TYPE_STRING, 0
:NONE DAT 0, TYPE_NONE
View
8 defs.dasm16
@@ -163,3 +163,11 @@
#define SCREEN_START 0
#define SCREEN_END 1
#define SCREEN_CURSOR 2
+
+; struct file_handle {
+; struct handle *first_sector;
+; struct handle *length;
+; }
+#define FILE_HANDLE__FIRST_SECTOR 1
+#define FILE_HANDLE__LENGTH 2
+
View
451 floppy.dasm16
@@ -1,20 +1,117 @@
-#define FILE_FREE_NODE 0
-#define FILE_DIRECTORY 1
-
+; memory buffer
#define FILE_BUFFER_START 49152
#define FILE_BUFFER_END 49664
+; these defines are only foor mbr
+#define FILE_FREE_HEAD FILE_BUFFER_START
+#define FILE_FREE_TAIL FILE_BUFFER_START + 1
+#define FILE_DIR_SECTOR FILE_BUFFER_START + 2
+
+; all other sectors
+#define FILE_NEXT_SECTOR FILE_BUFFER_START
+
+; current file status
:file_position dat 0
:file_buffer_position dat 0
:file_current_sector dat 0
-:file_free_node dat 0
-:file_directory dat 0
+;----------------------------------------------------------------
+; struct handle *file__dir__read(void)
+;----------------------------------------------------------------
+:file__dir__read
+ ;---------------------------------------
+ ; read mbr
+ ;---------------------------------------
+ jsr file__mbr__read
+
+ ;---------------------------------------
+ ; read directory
+ ;---------------------------------------
+ set push, [FILE_DIR_SECTOR]
+ jsr file__read_item_from_sector
+
+ set pc, pop
+
+;----------------------------------------------------------------
+; void file__dir__write(struct handle *dir)
+;----------------------------------------------------------------
+:file__dir__write
+ set push, 1
+ set push, 1
+ jsr preamble
+
+ set push, [z+10]
+ jsr file__write
+ set [z-1], x
+
+ ; get first_sector
+ set x, [x]
+ set push, [x + FILE_HANDLE__FIRST_SECTOR]
+ jsr int_to_int16
+ set a, x
+
+ ;---------------------------------------
+ ; read mbr
+ ;---------------------------------------
+ jsr file__mbr__read
+ set [FILE_DIR_SECTOR], a
+ jsr file__mbr__write
+
+ set pc, postamble
;----------------------------------------------------------------
-; void serialize(struct handle *root)
+; void file__format()
;----------------------------------------------------------------
-:serialize
+:file__format
+ set push, 0
+ set push, 1
+ jsr preamble
+
+ set [FILE_FREE_TAIL], 1439
+ set [FILE_DIR_SECTOR], 1
+
+ set i, 0
+:file__format__next_sector
+ sti [file_current_sector], i
+ ife i, 1440
+ set i, 0
+ set [FILE_NEXT_SECTOR], i
+
+ jsr file__write_sector
+
+ ifn i, 0
+ set pc, file__format__next_sector
+
+ set push, 0
+ set push, TYPE_DICT
+ jsr array_create
+ set [z-1], x
+
+ set push, 1
+ jsr file__open
+
+ set push, [z-1]
+ jsr file__serialize
+
+ jsr file__close
+
+ set pc, postamble
+
+;----------------------------------------------------------------
+; struct handle *file__deserialize(void)
+;----------------------------------------------------------------
+:file__deserialize
+ set push, 1
+ set push, 0
+ jsr preamble
+
+ set pc, recover
+ set pc, postamble
+
+;----------------------------------------------------------------
+; void file__serialize(struct handle *item)
+;----------------------------------------------------------------
+:file__serialize
set push, 1
set push, 0
jsr preamble
@@ -24,70 +121,281 @@
set push, [z+10] ; put root item to queue
-:serialize_loop
- sub b, 1
- set y, [b] ; next todo -item
+:file__serialize__next_item
+ sub b, 1 ; take next item in fifo order
- set push, y ; id
- jsr file_write_word
+ ;---------------------------------------
+ ; write ID
+ ;---------------------------------------
+ set y, [b] ; id == struct handle *
+ set push, y
+ jsr file__write_word
+ ;---------------------------------------
+ ; write TYPE
+ ;---------------------------------------
set c, [y + HANDLE_TYPE]
-
set push, c ; type
- jsr file_write_word
+ jsr file__write_word
+ ;---------------------------------------
+ ; write SIZE (actual)
+ ;---------------------------------------
set y, [y] ; move y to data
- set i, y
- add i, 1
+
+ set j, [y] ; array size
+ ife c, TYPE_FLOAT ; if item is float
+ set j, SIZEOF_FLOAT ; float size
- set push, SIZEOF_FLOAT ; I should reshape float
- ifn c, TYPE_FLOAT
- set push, [y]
+ set push, j
+ jsr file__write_word
- add y, peek ; last item *
-
- jsr file_write_word
+ ;---------------------------------------
+ ; write DATA
+ ;---------------------------------------
+ set i, y
+ ifn c, TYPE_FLOAT
+ add i, 1
-:serialize_list_loop
+ add y, j
+
+:file__serialize__data__loop
ifg i, y
ifl sp, b
- set pc, serialize_loop
+ set pc, file__serialize__next_item
ifg i, y
set pc, postamble
-
+
set push, [i]
- jsr file_write_word
+ jsr file__write_word
- ifc c, TYPE_TUPLE + TYPE_LIST + TYPE_DICT
- sti pc, serialize_list_loop
+ ifc c, TYPE_GROUP_CONTAINER
+ sti pc, file__serialize__data__loop
-; here starts the todo insertion loop
+; here starts the todo insertion loop
set j, a
-:serialize_todo_insert_loop
+:file__serialize__search_item
sub j, 1
ife [j], [i] ; match
- sti pc, serialize_list_loop
+ sti pc, file__serialize__data__loop
ifg j, sp
- set pc, serialize_todo_insert_loop ; no match
+ set pc, file__serialize__search_item ; no match
+ ; no match found
set push, [i]
- sti pc, serialize_list_loop
-
+ sti pc, file__serialize__data__loop
;----------------------------------------------------------------
-:file_write_word
+; void save(struct handle *filename, struct handle *root)
+;----------------------------------------------------------------
+:file__save
+ set push, 2
+ set push, 2
+ jsr preamble
+
+ ;---------------------------------------
+ ; store item to disk
+ ;---------------------------------------
+ set push, [z+10]
+ jsr file__write
+ set [z-1], x
+
+ ;---------------------------------------
+ ; update directory
+ ;---------------------------------------
+ jsr file__dir__read
+ set [z-2], x
+
+ set push, x
+ set push, [z+11]
+ jsr dict_get
+
+ ife x, 0
+ set pc, file__save__update_dir
+
+ set push, x
+ jsr file__delete
+
+:file__save__update_dir
+ set push, [z-2]
+ set push, [z+11]
+ set push, [z-1]
+ jsr dict_set
+
+ set push, [z-2]
+ jsr file__dir__write
+
+ set pc, postamble
+
+;----------------------------------------------------------------
+; struct handle *file__read(struct handle *file_handle)
+;----------------------------------------------------------------
+:file__read
+ set push, 1
+ set push, 0
+ jsr preamble
+
+ set x, [z+10] ; struct handle *tuple
+ set x, [x] ; struct tuple *file_handle
+
+ set push, [x + FILE_HANDLE__FIRST_SECTOR]
+ jsr int_to_int16
+
+ set push, x
+ jsr file__read_item_from_sector
+
+ set pc, postamble
+
+;----------------------------------------------------------------
+; struct handle *file__delete(struct handle *file_handle)
+;----------------------------------------------------------------
+:file__delete
+ set push, 1
+ set push, 0
+ jsr preamble
+
+ jsr file__mbr__read
+
+ set push, [FILE_FREE_TAIL]
+ jsr file__read_sector
+
+ set x, [z+10] ; struct handle *tuple
+ set x, [x] ; struct tuple *file_handle
+
+ set push, [x + FILE_HANDLE__FIRST_SECTOR]
+ jsr int_to_int16
+
+ set [FILE_NEXT_SECTOR], x
+ jsr file__write_sector
+
+ set pc, postamble
+
+;----------------------------------------------------------------
+; struct handle *file__read_item_from_sector(uint16 sector)
+;----------------------------------------------------------------
+:file__read_item_from_sector
+ set x, [sp+1]
+ set [sp], pop
+
+ set push, x
+ jsr file__open
+ jsr file__deserialize
+
+ set pc, pop
+
+;----------------------------------------------------------------
+; struct handle *file__write(struct handle *item)
+;----------------------------------------------------------------
+:file__write
+ set push, 1
+ set push, 1
+ jsr preamble
+
+ ;---------------------------------------
+ ; read mbr
+ ;---------------------------------------
+ jsr file__mbr__read
+ set a, [FILE_FREE_HEAD] ; first sector
+
+ ;---------------------------------------
+ ; write file to free space
+ ;---------------------------------------
+ set push, a
+ jsr file__open
+
+ ; serialize object
+ set push, [z+10]
+ jsr file__serialize
+
+ set b, [file_position] ; file length
+
+ ;---------------------------------------
+ ; terminate file and update list of free sectors
+ ;---------------------------------------
+ jsr file__close
+
+ ;---------------------------------------
+ ; return file handle (first_sector, length)
+ ;---------------------------------------
+ set push, 2
+ set push, TYPE_TUPLE
+ jsr array_create
+ set [z-1], x
+
+ set c, [x]
+
+ set push, a ; first_sector
+ jsr uint16_to_int
+ set [c + 1], x
+
+ set push, b ; length
+ jsr uint16_to_int
+ set [c + 2], x
+
+ set pc, postamble
+
+;----------------------------------------------------------------
+:file__open
+;----------------------------------------------------------------
+ set [file_current_sector], [sp+1]
+ jsr file__read_sector
+
+ ; initialize position pointers
+ set [file_buffer_position], 1
+ set [file_position], 0
+
+ set [sp], pop
+ set pc, pop
+
+;----------------------------------------------------------------
+:file__close
+;----------------------------------------------------------------
+ ; store next free sector
+ set push, [FILE_NEXT_SECTOR]
+
+ ; terminate current sector list
+ set [FILE_NEXT_SECTOR], 0
+ jsr file__write_sector
+
+ ; update mbr
+ jsr file__mbr__read
+ set [FILE_FREE_HEAD], pop
+ jsr file__mbr__write
+
+ set pc, pop
+
+;----------------------------------------------------------------
+:file__mbr__read
+;----------------------------------------------------------------
+ set [file_current_sector], 0
+ jsr file__read_sector
+ set pc, pop
+
+;----------------------------------------------------------------
+:file__mbr__write
+;----------------------------------------------------------------
+ set [file_current_sector], 0
+ jsr file__write_sector
+ set pc, pop
+
+;----------------------------------------------------------------
+; LOW LEVEL SUBROUTINES
+;----------------------------------------------------------------
+
+;----------------------------------------------------------------
+:file__write_word
;----------------------------------------------------------------
ife [file_buffer_position], FILE_BUFFER_END
- set pc, file_write_word_1
+ set pc, file__write_word__add_to_buffer
- jsr file_write_sector
- jsr file_next_sector
+ jsr file__write_sector
+ jsr file__next_sector
-:file_write_word_1
+:file__write_word__add_to_buffer
set x, [file_buffer_position]
set [x], [sp+1]
@@ -98,10 +406,10 @@
set pc, pop
;----------------------------------------------------------------
-:file_read_word
+:file__read_word
;----------------------------------------------------------------
ife [file_buffer_position], FILE_BUFFER_END
- jsr file_next_sector
+ jsr file__next_sector
set c, [file_buffer_position]
set [c], c
@@ -112,9 +420,9 @@
set pc, pop
;----------------------------------------------------------------
-; void file_write_sector()
+; void file__write_sector()
;----------------------------------------------------------------
-:file_write_sector
+:file__write_sector
set push, 0
set push, 0
jsr preamble
@@ -125,17 +433,17 @@
hwi [floppy]
set a, 0
-:file_write_sector_loop
+:file__write_sector__loop
hwi [floppy]
ifn b, 0x0001
- set pc, file_write_sector_loop
+ set pc, file__write_sector__loop
set pc, postamble
;----------------------------------------------------------------
-; void file_read_sector()
+; void file__read_sector()
;----------------------------------------------------------------
-:file_read_sector
+:file__read_sector
set push, 0
set push, 0
jsr preamble
@@ -147,65 +455,24 @@
hwi [floppy]
set a, 0
-:file_read_sector_loop
+:file__read_sector__loop
hwi [floppy]
ifb b, 0x0003
- set pc, file_read_sector_loop
+ set pc, file__read_sector__loop
set pc, postamble
;----------------------------------------------------------------
-:file_next_sector
+:file__next_sector
;----------------------------------------------------------------
; get the next sector number
set [file_current_sector], [FILE_BUFFER_START]
; read the sector to memory buffer
- jsr file_read_sector
+ jsr file__read_sector
; reset buffer pointer
set [file_buffer_position], FILE_BUFFER_START + 1
set pc, pop
-;----------------------------------------------------------------
-; void save(struct handle *filename, struct handle *root)
-;----------------------------------------------------------------
-:file_save
- set push, 2
- set push. 0
- jsr preamble
-
- ; check disk is loaded
-
- set [file_current_sector], 0
- jsr file_read_block
-
- set [file_free_node], [FILE_BUFFER_START + FILE_FREE_NODE]
- set [file_directory], [FILE_BUFFER_START + FILE_DIRECTORY]
-
- set [file_current_sector], [file_free_node]
- jsr file_read_block
-
- set [file_buffer_position], 1
- set [file_position], 0
-
- set push, [z+10]
- jsr serialize
-
- ; update directory
-
- set pc, postamble
-
-;----------------------------------------------------------------
-:file_open
-;----------------------------------------------------------------
-;----------------------------------------------------------------
-:file_close
-;----------------------------------------------------------------
-
-
-
-
-
-
View
134 memory.dasm16
@@ -7,46 +7,74 @@
set push, 0
jsr preamble
- set a, [z+11]
- ifl [a + HANDLE_SIZE], [z + 10]
- set pc, realloc__enlarge
+ set c, [z+11] ; struct handle *
+ set a, [z+10] ; size
+
+ ifl [c + HANDLE_SIZE], a
+ set pc, realloc__grow
- ; shrink is easy
- set [a + HANDLE_SIZE], [z+10]
+ ; shrinking is easy
+ set [c + HANDLE_SIZE], a
set pc, postamble
-:realloc__enlarge
- ; calculate required extra space
- set c, [z + 10] ; c = new_size - old_size
- sub c, [a + HANDLE_SIZE]
-
- ; calculate available space
- set b, [heap_handle_start]
- sub b, [heap_free_start]
+:realloc__grow
+ jsr ensure_memory
+
+ ; init allocated space
+ set i, [heap_free_start] ; first word of allocated memory region
+ add a, i ; first word after allocated memory
- ifl b, c ; if available space < required space
- set pc, realloc__garbage_collect
+ set j, [c + HANDLE_PTR] ; first word of old region
+ set b, j ; first word after the old region
+ add b, [c + HANDLE_SIZE]
- set [a + HANDLE_SIZE], [z + 10]
- jsr compact
+:realloc__copy_loop
+ ife j, b
+ set pc, realloc__zero_loop
+
+ set [i], [j] ; default init with zero
+ sti pc, realloc__copy_loop
- set pc, postamble
+:realloc__zero_loop
+ ife i, a
+ set pc, realloc__remove_from_list
-:realloc__garbage_collect
- jsr mark
- jsr compact
+ set [i], 0 ; default init with zero
+ sti pc, realloc__zero_loop
+
+:realloc__remove_from_list
+ ife [heap_handle_tail], c
+ set pc, realloc__set_handle_values
- ; calculate available space
- set b, [heap_handle_start]
- sub b, [heap_free_start]
+ ; remove handle from reserved list
+ set b, [heap_handle_head]
+ sub b, HANDLE_NEXT
- ifl b, c ; if available space < required space
- jsr recover
+:realloc__remove_from_list__loop
+ ife [b + HANDLE_NEXT], c
+ set pc, realloc__remove_from_list__break
+
+ set b, [b + HANDLE_NEXT]
+ set pc, realloc__remove_from_list__loop
- set [a + HANDLE_SIZE], [z + 10]
- jsr compact
+:realloc__remove_from_list__break
+ set [b + HANDLE_NEXT], [c + HANDLE_NEXT]
- set pc, postamble
+ ; append new handle to tail reserved handles list
+ set b, [heap_handle_tail] ; store pointer to last item in reserved handles list
+ set [heap_handle_tail], c ; set reserved handles tail pointer to point in new handle
+ set [b + HANDLE_NEXT], c ; set previous tail's next to point to new handle
+
+:realloc__set_handle_values
+ ; initialize handle structure
+ set [c + HANDLE_PTR], [heap_free_start] ; pointer to allocated memory region
+ set [c + HANDLE_SIZE], [z+10] ; size of allocated memory region
+ set [c + HANDLE_NEXT], 0 ; next handle in reserved handles list
+
+ set [heap_free_start], a ; update unallocated heap space pointer
+
+ set x, c ; return pointer to new handle
+ set pc, postamble
;----------------------------------------------------------------
; struct handle *alloc(uint16 size, uint16 type)
@@ -61,12 +89,7 @@
set a, [z+11]
add a, SIZEOF_HANDLE
- ; calculate space in heap gap
- set b, [heap_handle_start]
- sub b, [heap_free_start]
-
- ifl b, a ; if available space < required space
- set pc, alloc__garbage_collect
+ jsr ensure_memory
;---------------------------------------
; handle cleanup
@@ -75,19 +98,7 @@
ife [heap_counter], HEAP_GC_TRIGGER
jsr mark
- set pc, alloc__reserve_handle
-
-:alloc__garbage_collect
- jsr mark
- jsr compact
-
- set b, [heap_handle_start]
- sub b, [heap_free_start]
-
- ifl b, a ; if available space < required space
- jsr recover
-
-:alloc__reserve_handle
+ ; try to get recycled handle
set c, [heap_handle_free]
; remove handle from free list
@@ -137,6 +148,33 @@
set pc, postamble
;----------------------------------------------------------------
+:ensure_memory
+; fastcall
+; a: required memory
+; return
+; b: available space
+; x: polluted
+;----------------------------------------------------------------
+ set x, ensure_memory__out_of_memory
+
+:ensure_memory__check
+ ; calculate space in heap gap
+ set b, [heap_handle_start]
+ sub b, [heap_free_start]
+
+ ifl b, a ; if available space < required space
+ set pc, x
+
+ set pc, pop
+
+:ensure_memory__out_of_memory
+ jsr mark
+ jsr compact
+
+ set x, recover
+ set pc, ensure_memory__check
+
+;----------------------------------------------------------------
; void compact()
;----------------------------------------------------------------
:compact
Please sign in to comment.
Something went wrong with that request. Please try again.