; -----------------------------------------------------------------------------
; QDOS v0.01 layer for CiscOS (c) Eelco Huininga 2015
; DOS Filename   : QDOS.asm
; Load address   : ???????? for 68020+ / ???????? for 68000 / ???????? for 68008
; Exec address   : ???????? for 68020+ / ???????? for 68000 / ???????? for 68008
; Length         : ????????
; -----------------------------------------------------------------------------

; -----------------------------------------------------------------------------
; ALIGN macro
; -----------------------------------------------------------------------------

ALIGN   MACRO
	IFARG   1
	DS.B    (*-1)/\1*\1+\1-*
	MEXIT
	ENDC
	DS.B    (*+3)&-4-*
	ENDM

; -----------------------------------------------------------------------------
; Compiler constants
; -----------------------------------------------------------------------------


DEBUG	SET 0                                     ; Set DEBUG to 0 for real hardware operation
                                                  ; Set DEBUG to 1 for Easy68k simulator operation

SWI	                     EQU	12			; Use TRAP #12 for SWI calls
qdos_version	             SET	'v0.01 (20150730)'	; QDOS version
qdos_startaddress            EQU	$00001000               ; Load address of QDOS

; -----------------------------------------------------------------------------
; System variables
; -----------------------------------------------------------------------------
SYSBASE	                     EQU	$00280000
SYS_IDNT	             EQU	SYSBASE+$0000           ; System variables identifier
SYS_CHPB	             EQU	SYSBASE+$0004           ; Common HeaP Base
SYS_CHPF	             EQU	SYSBASE+$0008           ; Common HeaP Free space pointer
SYS_FSBB	             EQU	SYSBASE+$000C           ; Filing system Slave Block area Base
SYS_SBAB	             EQU	SYSBASE+$0010           ; 'QL SuperBASIC' Area Base
SYS_TPAB	             EQU	SYSBASE+$0014           ; Transient Program Area Base
SYS_TPAF	             EQU	SYSBASE+$0018           ; Transient Program Area Free space pointer
SYS_RPAB	             EQU	SYSBASE+$001C           ; Resident Procedure Area Base
SYS_RAMT	             EQU	SYSBASE+$0020           ; User RAM Top (+1)
SYS_MXFR	             EQU	SYSBASE+$0024           ; Maximum return from free memory call [SMS]
SYS_RTC	                     EQU	SYSBASE+$0028           ; Real time (seconds) [SMS]
SYS_RTCF	             EQU	SYSBASE+$002C           ; Real time fractional, count down [SMS]
SYS_RAND	             EQU	SYSBASE+$002E           ; RANDom number
SYS_PICT	             EQU	SYSBASE+$0030           ; Polling Interupt CounT
sys_dtyp	             EQU	SYSBASE+$0032           ; Byte Display TYPe (0=normal, 1=TV 625, 2=TV 525)
sys_dfrz	             EQU	SYSBASE+$0033           ; Byte Display FRoZen (T or F)
sys_qlmr	             EQU	SYSBASE+$0034           ; Byte QL Master chip Register value (Copy of MC_STAT)
sys_qlir	             EQU	SYSBASE+$0035           ; Byte QL Interrupt Register value (Copy of PC_INTR)
sys_rshd	             EQU	SYSBASE+$0036           ; Byte true to reschedule [SMS]
sys_nnnr	             EQU	SYSBASE+$0037           ; Bbyte Network Node NumbeR
sys_exil	             EQU	SYSBASE+$0038           ; EXternal Interrupt action List
sys_poll	             EQU	SYSBASE+$003c           ; POLled action List
sys_shdl	             EQU	SYSBASE+$0040           ; ScHeDuler loop action List
sys_iodl	             EQU	SYSBASE+$0044           ; IO Driver List
sys_fsdl	             EQU	SYSBASE+$0048           ; Filing System Driver List
sys_ckyq	             EQU	SYSBASE+$004c           ; Current Keyboard Queue
sys_ertb	             EQU	SYSBASE+$0050           ; Exception Redirection Table Base
sys_sbrp	             EQU	SYSBASE+$0054           ; Slave Block Running Pointer
sys_sbtb	             EQU	SYSBASE+$0058           ; Slave Block Table Base
sys_sbtt	             EQU	SYSBASE+$005c           ; Slave Block Table Top
sys_jbtg	             EQU	SYSBASE+$0060           ; next JoB TaG
sys_jbtp	             EQU	SYSBASE+$0062           ; highest JoB in table (ToP one)
sys_jbpt	             EQU	SYSBASE+$0064           ; current JoB PoinTer
sys_jbtb	             EQU	SYSBASE+$0068           ; JoB Table Base
sys_jbtt	             EQU	SYSBASE+$006c           ; JoB Table Top
sys_chtg	             EQU	SYSBASE+$0070           ; next CHannel TaG
sys_chtp	             EQU	SYSBASE+$0072           ; highest CHannel in table (ToP one)
sys_chpt	             EQU	SYSBASE+$0074           ; last checked CHannel PoinTer
sys_chtb	             EQU	SYSBASE+$0078           ; CHannel Table Base
sys_chtt	             EQU	SYSBASE+$007c           ; CHannel Table Top
sys_frbl	             EQU	SYSBASE+$0080           ; FRee Block List (to be returned to common heap) [SMS]
sys_tsdd	             EQU	SYSBASE+$0084           ; Thor flag [THOR only]
sys_caps	             EQU	SYSBASE+$0088           ; CAPS lock (0 if off, msbyte set if on)
sys_lchr	             EQU	SYSBASE+$008a           ; Last CHaRacter (for auto-repeat)
sys_rdel	             EQU	SYSBASE+$008c           ; Repeat DELay (20ms units)
sys_rtim	             EQU	SYSBASE+$008e           ; Repeat TIMe (20ms units)
sys_rcnt	             EQU	SYSBASE+$0090 word Repeat CouNTer (decremented every 20ms)
sys_swtc	             EQU	SYSBASE+$0092 word SWiTch queues Character
sys_qlbp	             EQU	SYSBASE+$0096 byte QL BeePing
sys_brk	             EQU	SYSBASE+$0097 byte set by keyboard break [SMSQ]
sys_ser1	             EQU	SYSBASE+$0098 long receive channel 1 queue address [QL]
sys_ser2	             EQU	SYSBASE+$009c long receive channel 2 queue address [QL]
sys_tmod	             EQU	SYSBASE+$00a0 byte ZX8302 transmit mode (includes baudrate) (copy of PC_TCTRL) [QL]
sys_ptyp	             EQU	SYSBASE+$00a1 byte PRoCeSsor type $00=68000/8, $30=68030 etc. [SMSQ]
sys_csub	             EQU	SYSBASE+$00a2 long subroutine to jump to on capslock
sys_stmo	             EQU	SYSBASE+$00a6 word serial xmit timeout [QL]
sys_dmiu	             EQU	SYSBASE+$00a6 byte DMA in use [SMS2, ST, SMSQ]
sys_mtyp	             EQU	SYSBASE+$00a7 byte Machine TYPe / emulator type [SMS,ST]
sys_polf	             EQU	SYSBASE+$00a8 word polling frequency [SMSQ]
sys_cfst	             EQU	SYSBASE+$00aa word flashing cursor status
sys_prgd	             EQU	SYSBASE+$00ac long pointer to PRoGram Default [EXT]
sys_datd	             EQU	SYSBASE+$00b0 long pointer to DATa Default [EXT]
sys_dstd	             EQU	SYSBASE+$00b4 long pointer to DeSTination Default [EXT]
sys_thgl	             EQU	SYSBASE+$00b8 long pointer to THinG List [EXT]
sys_psf	             EQU	SYSBASE+$00bc long Primary stack frame pointer [SMSQ]
sys_200i	             EQU	SYSBASE+$00c0 byte 200 Hz in service [SMSQ]
sys_50i	             EQU	SYSBASE+$00c1 byte 50 Hz in service [SMSQ]
sys_10i	             EQU	SYSBASE+$00c2 byte 10 Hz in service [SMSQ]
sys_plrq	             EQU	SYSBASE+$00c3 byte poll requested (-ve for request) [SMSQ]
sys_clnk	             EQU	SYSBASE+$00c4 long pointer to console linkage [SMSQ]
sys_castat	             EQU	SYSBASE+$00c8 byte -1 cache on, +1 instruction cache temp off [SMSQ]
sys_casup	             EQU	SYSBASE+$00c9 byte cache suppressd timer [SMSQ]
sys_iopr	             EQU	SYSBASE+$00ca word IO priority [SMSQ]
sys_cbas	             EQU	SYSBASE+$00cc long current basic (copy of sys_jbpt) [SMSQ]
sys_fpu	             EQU	SYSBASE+$00d0 16 bytes [SMSQ]
sys_prtc	             EQU	SYSBASE+$00e0 byte set if real time clock protected [SMSQ]
sys_pmem	             EQU	SYSBASE+$00e1 byte memory protection level [SMSQ, ST]
sys_slug	             EQU	SYSBASE+$00e2 word slug level [SMSQ]
sys_mdrn	             EQU	SYSBASE+$00ee byte which mdv drive is running? [QL]
sys_mdct	             EQU	SYSBASE+$00ef byte mdv run-up run-down counter [QL]
sys_mdid	             EQU	SYSBASE+$00f0 8*byte drive ID*4 of each microdrive [QL]
sys_mdst	             EQU	SYSBASE+$00f8 8*byte status: 0=no pending ops [QL]
sys_fsdd	             EQU	SYSBASE+$0100 16*long pointers to Filing System Drive Definitions
sys_fsdt	             EQU	SYSBASE+$0140 Filing System drive Definition table Top
sys_fsch	             EQU	SYSBASE+$0140 long linked list of Filing System CHannel blocks
sys_xact	             EQU	SYSBASE+$0144 byte set if XLATE active [QDOS V1.10+, not SMS2]
sys_xtab	             EQU	SYSBASE+$0146 long pointer to XLATE table [QDOS V1.10+, not SMS2]
sys_erms	             EQU	SYSBASE+$014a long pointer to (QDOS) error message table [QDOS V1.10+, not SMS2]
sys_mstab	             EQU	SYSBASE+$014e long pointer to (SMSQ) message table [SMSQ]
sys_taskm	             EQU	SYSBASE+$0154 4 long used by Taskmaster - conflicts with
sys_turbo	             EQU	SYSBASE+$0160 long used by Turbo
sys_qsound	             EQU	SYSBASE+$0164 long used by QSound
sys_ldmlst	             EQU	SYSBASE+$0168 long language dependent module list [SMSQ]
sys_lang	             EQU	SYSBASE+$016c word current language [SMSQ]
sys_vers	             EQU	SYSBASE+$0170 long operating system version [SMSQ]
sys_top	             EQU	SYSBASE+$0180 TOP of system vars - bottom of Supervisor Stack



; -----------------------------------------------------------------------------
; Start of code
; -----------------------------------------------------------------------------

START
	INCLUDE	"SWIs.inc"
	ORG	qdos_startaddress


trap0_handler
	MOVE.L	#OS_EnterOS, A0                ; SWI OS_EnterOS: Switch to supervisor mode
	TRAP	#SWI
	RTE


trap1_handler
	CMP	#$3B, D0
	BLS	trap1_handler2

trap1_handler_error
	MOVE.L	#err_trap1_handler_error, D0
	ORI	#$02, CCR                         ; Set overflow fag
	RTS

trap1_handler2
	LEA	_trap1_table, A0
	LSL.L	#2, D0                            ; D0 = D0 * 4
	ADD.L	D0, A0                            ; A0 = A0+(D0*3)
	ADD.L	D0, A0
	ADD.L	D0, A0
	MOVE.L	(A0)+, D1
	BEQ	trap1_handler_error               ; Mnemonic not implemented

SWI_OS_ReadDefaultHandler_exit
	RTS
