Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Task
- TASKKILL: macro
- jp KILL
- endm
- TASKWAIT: macro
- call WAIT
- endm
- TASKSLEEP: macro frames
- push af
- ld a,frames
- ld (iy+task.waitframe),a
- pop af
- call WAIT
- endm
- STRUCT task
- sp defw 1
- waitframe defb 1
- active defb 1
- vars defb 16 ;
- stack defb 1
- ENDS
- //RSSET 0
- //tk_sp: rw 1 ;(dw) current task stack pointer (iy always points to this data)
- //tk_waitfram: rb 1 ;(db) if one run on excute else wait until frames
- //tk_active: rb 1 ;(db) none zero if task is active
- //tk_vars: rb 16 ;(16) user varible data for task
- //tk_stack rb 0 ;top of stack
- tk_stack_end equ 62 ;top of stack
- tk_size: equ 64 ; size of each task blob
- ; ************************************************************************
- ; task data 64ytes each = 8 tasks max
- ; ************************************************************************
- Tasks: ds 1024 ;16 tasks @64bytes each
- db 0xff,0xff ;;mark end using 0xff ,0xff in stack address
- TasksRunning: db 0
- ; ************************************************************************
- TestTaskSystem:
- ld hl,TestTask1
- call AddTask
- ld hl,TestTask2
- call AddTask
- .loop:
- ld a,20
- call WaitForLine
- call RunAllTasks
- jr .loop
- ; ************************************************************************
- ; AddTask
- ; hl is function start
- ; returns
- ; carry set if fails to allocate task
- ; ************************************************************************
- AddTask:
- ld ix,Tasks ;point ix at the tasks
- .loop
- ld a,(ix+task.active)
- cp 0
- jr nz,.NextTask
- ld a,1
- ld (ix+task.active),a
- ld a,1
- ld (ix+task.waitframe),a
- ;found one!
- ;store the current stack address
- ld (.RunTasks_SP+1),sp
- ;function address now in de
- ex de,hl
- //set the task stack pointer to the end anmd set it
- ld a,ixl
- ld l,a
- ld a,ixh
- ld h,a
- add hl,tk_stack_end
- ld sp,hl
- //this is the task start address push
- push de
- //push dummy start registers so we can start task as normal
- push af
- push hl
- push de
- push bc
- push hl
- push de
- push bc
- push ix
- //store the stack pointer in task data
- ld a,ixl
- ld l,a
- ld a,ixh
- ld h,a
- ld (.SMC_SP+2),hl
- .SMC_SP:
- ld (0000),sp
- //put the calling stack back and return
- .RunTasks_SP:
- ld sp,0
- ;ok so clear carry flag
- or a
- ret
- .NextTask:
- ld de,tk_size
- add ix,de
- ld a,(ix+task.sp+1)
- cp 0xff
- jr nz,.loop
- //no free tasks , so set carry flag
- scf
- ret
- ; ************************************************************************
- ; RunAllTasks
- ; IY CANNOT BE USED IN TASKS!
- ; ************************************************************************
- RunAllTasks:
- xor a
- ld (TasksRunning),a
- ld (RunTasks_SP+1),sp
- ld iy,Tasks ;point iy at the tasks
- RunAllTasksLoop:
- ;check the active flag
- ld a,(iy+task.active)
- cp 0
- jr z,NextTask
- ;;check to see if this task is waiting muiple frames to run
- ld a,(iy+task.waitframe)
- dec a
- ld (iy+task.waitframe),a
- cp 0
- jr nz,NextTask
- //tell task run on next frame
- ld a,1
- ld (iy+task.waitframe),a
- ;run task
- ;first put the task SP back
- ld l,(iy+task.sp)
- ld h,(iy+task.sp+1)
- ld sp,hl
- ld a,(TasksRunning)
- inc a
- ld (TasksRunning),a
- ;restore all the registers
- exx
- pop ix
- pop bc
- pop de
- pop hl
- exx
- pop bc
- pop de
- pop hl
- pop af
- ;jump to current task address, we will jump back to NextTask
- ret
- NextTask:
- ;;increase task list pointer
- ld de,tk_size
- add iy,de
- ;check for end of task list
- ld a,(iy+task.sp+1)
- cp 0xff
- jr nz,RunAllTasksLoop
- ;put the stack back and return
- RunTasks_SP:
- ld sp,0
- ret
- ; ************************************************************************
- ; WAIT
- ; ************************************************************************
- WAIT:
- ;store all the regs on task stack
- push af
- push hl
- push de
- push bc
- exx
- push hl
- push de
- push bc
- exx
- push ix
- ;store the stack pointer
- ld a,iyl
- ld l,a
- ld a,iyh
- ld h,a
- ld (SMC_SP+2),hl
- SMC_SP:
- ld (0000),sp
- jr NextTask
- ; ************************************************************************
- ; KILL
- ; ************************************************************************
- KILL:
- ;kill taks bu setting the active flasg to zero
- xor a
- ld (iy+task.active),a
- jr NextTask
- ; ************************************************************************
- ; RegisterSetTest
- ; ************************************************************************
- RegisterSetTest:
- ld a,1
- ld bc,0x0203
- ld de,0x0405
- ld hl,0x0607
- exx
- ld bc,0x090A
- ld de,0x0B0C
- ld hl,0x0D0E
- exx
- ret
- ; ************************************************************************
- ; RegisterCheckTest
- ; ************************************************************************
- RegisterCheckTest:
- cp 1
- jr nz,.Fail
- ld a,b
- cp 2
- jr nz,.Fail
- ld a,c
- cp 3
- jr nz,.Fail
- ld a,d
- cp 4
- jr nz,.Fail
- ld a,e
- cp 5
- jr nz,.Fail
- ld a,h
- cp 6
- jr nz,.Fail
- ld a,l
- cp 7
- jr nz,.Fail
- exx
- ld a,b
- cp 9
- jr nz,.Fail
- ld a,c
- cp 0xa
- jr nz,.Fail
- ld a,d
- cp 0xb
- jr nz,.Fail
- ld a,e
- cp 0xc
- jr nz,.Fail
- ld a,h
- cp 0xd
- jr nz,.Fail
- ld a,l
- cp 0xe
- jr nz,.Fail
- exx
- ret
- .Fail
- BREAK
- TestTask1:
- ld b,100
- .xl
- push bc
- BORDER RED
- ld b,20
- .l
- ex (sp), hl
- ex (sp), hl
- djnz .l
- BORDER BLACK
- TASKWAIT ; wait until next frame
- BORDER GREEN
- ld b,20
- .l2
- ex (sp), hl
- ex (sp), hl
- djnz .l2
- BORDER BLACK
- TASKSLEEP 1 ;sleep for 2 frames
- pop bc
- djnz .xl
- TASKKILL
- TestTask2:
- ld b,200
- .xl
- push bc
- BORDER CYAN
- ld b,50
- .l
- ex (sp), hl
- ex (sp), hl
- djnz .l
- BORDER BLACK
- TASKWAIT ; wait until next frame
- BORDER BLUE
- ld b,50
- .l2
- ex (sp), hl
- ex (sp), hl
- djnz .l2
- BORDER BLACK
- //TASKSLEEP(1) ;sleep for 2 frames
- TASKWAIT
- pop bc
- djnz .xl
- ld b,13
- .tkl
- push bc
- ld hl,TestTask1
- call AddTask
- TASKSLEEP 5
- pop bc
- djnz .tkl
- TASKSLEEP 250
- ld hl,TestTask2
- call AddTask
- TASKKILL
- ENDMODULE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement