I wrote this out in my senior economics class. The notes are sometimes cryptic, and there were bugs. I tried to fix some as I typed it up though. These are just the parts that I feel like trying to understand.
Code:
Code:
;EventQueue structure
;.dw address, message; the message can be an address, a value, or nothing at all
EventHandlerHalt:;this section was added 5/21/10
halt
EventHandler:
ld a,(EventQueueSize)
or a
;jp z,EventHandler;/upon rereading, I should halt to save battery
jp z,EventHandlerHalt
ld hl,(EventQueue+2)
ex de,hl
ld hl,ReturnToHere
push hl
ld hl,(EventQueue)
;call hl;/not a valid instruction
jp (hl);will return to ReturnToHere
ReturnToHere:;added later because call hl is not real
ld hl,(EventQueue+4)
ld de,(EventQueue)
ld bc,(EventQueueSize)
ldir
ld a,(EventQueueSize)
sub 4
ld (EventQueueSize),a
jp EventHandler
ScheduleEvent:
push af
ld c,4;b=0 /that's what the notes said
add hl,bc
ld de, EventQueue
ld a,(EventQueueSize)
push hl
ld l,a \ ld h,0
add hl,de
ex de,hl,hl
pop hl; hl=addr, bc = 4 /I can only assume hl is the address of the event
add a,4
jp c,EQoverflowHandler
ldir
ld c,8
pop af
ret
;TaskScheduleSize
;.db size
;TaskSchedule
;.dw timer, maxtimer, address, message
TaskManager:;although not marked in the notes, this is called from the interrupt
ld a,(ScheduleSize)
ld hl,TaskSchedule
ld b,0; added later to fix the bc enigma
TaskManagerLoop:;just called it loop in the notes
ld de,1;to begin with, the notes are cryptic, and it's barely legible on this line. it was added as an afterthought and I have no idea what it is
;ld bc,8 ;the notes are cryptic about this one. The event scheduler that it calls seems to need b to be 0
;I think I may have figured out what the bc is, so I'm commenting out the line to fix the bug
dec (hl)
push hl;added later to fix obvious bug
call z,ScheduleEvent
pop hl;added to match the push
sub 8;
ret z
ld bc,8 ; this fixes that bc enigma
add hl,bc
jp TaskManagerLoop
ScheduleEventHL: ;schedule event for code other than the task manager
EQoverflowHandler:
;remove all tasks in the removable task list
;remove all events in the removable event list
;move tasks together (condense them)
;move events together
;if recovery was successful, schedule the recovery event and return
;if recovery was a failure, try to preserve data in RAM then display a panic screen, then soft reset and try to determine the cause