.org	$8000

	JMP	L8037
        JMP	L809A
        .byte   $82
        .byte   $25
        .byte   $01
L8009   .DB     "68008 on the BBC"
        .byte      $00
        .byte	"Version 1.1"
        .byte      $00
        .byte      "(C) Vivaway 1985"
        .byte      $00
L8037   LDX     #$FF
	TXS			; Stack = $00FF
	JMP	L80E3
        .byte      "OS9"
        .byte      $00
        .byte      $0D  ;'cr'
        .byte      $82
        .byte      "OS9 - OS-9/68000 from Cumana"
        .byte      $0d  ;'cr'
        .byte      $00
        .byte      $0d  ;'cr'
        .byte      $82
L8061   .byte      "68008 Upgrade Board for the BBC"
        .byte      $0D  ;'cr'
        .byte      $81
        .byte      "        from Cumana"
        .byte      $0D  ;'cr'
        .byte      $0D  ;'cr'
        .byte      $00

L809A	PHA
	TYA
	PHA
	TXA
	PHA
	TSX
        LDA     $0103,X
        CMP     #$09
	BNE	L80CB
        LDA     ($F2),Y
        CMP     #$0D
	BEQ	L80BA
        LDA     #$80
        STA     $A9
        LDA     #$3D
        STA     $A8
	JSR	L81AC
	BCC	L80C5
L80BA   LDA     #$80
        STA     $A9
        LDA     #$41
        STA     $A8
	JSR	OSpline
L80C5	PLA
	TAX
	PLA
	TAY
	PLA
	RTS
L80CB   CMP     #$04
        BNE     $80C5
        LDA     #$80
        STA     $A9
        LDA     #$3D
        STA     $A8
        JSR     L81AC
        BCC     L80C5
        LDA     #$8E
        LDX     $F4
        JMP     ($020A)
L80E3   LDA     #$80            ; $8061 "68008 Upgrade Board for the BBC"
        STA     $A9
        LDA     #$61
        STA     $A8
        JSR     L8193           ; print line
	SEI
        LDA     #$00		; set 68008 vectors
        STA     $2000
        STA     $2001
        STA     $2004
        STA     $2005
        STA     $2003
        STA     $2400
        LDA     #$40
        STA     $2002
        LDA     $8206		; $8662
        STA     $2006
        LDA     $8207
        STA     $2007
        LDA     $FE4E		; System Via Int en reg
        STA     $2480		; Save at $0480
        LDA     #$7F		;
        STA     $FE4E		; Disable all ints
        LDA     #$34
        STA     $FC47		; release 68008 reset
L8124   LDA     $2400
	BEQ	L8124

L8129	PHA
        LDA     $FE4E
        STA     $2481
        AND     #$7F
        STA     $FE4E
	CLI
        LDA     $2480
        STA     $FE4E
	PLA
        CMP     #$01
	BEQ	L8179		; Exit to BBC OS
        CMP     #$02
	BEQ	L8170		; Get Char
        CMP     #$03
	BEQ	L8150		; 
        CMP     #$04
        BEQ     L8182
        JMP     L8156
L8150   LDA     $2401
        JSR     OSWRCH		; print char
L8156	SEI
        LDA     $FE4E
        STA     $2480
        AND     #$7F
        STA     $FE4E
        LDA     $2481
        STA     $FE4E
        LDA     #$00
        STA     $2400
	JMP	L8124
L8170   JSR     OSRDCH		; get char OS $FFE0
        STA     $2401
	JMP	L8156
L8179   LDA     #$3C		; Reset 68008
        STA     $FC47
	CLI
	JMP	L818C
L8182   LDA     $00EC
	BNE	L8170
        STA     $2401
	JMP	L8156
L818C	PLA			; Return to BBC OS
	TAX
	PLA
	TAY
        LDA     #$00
	RTS
L8193   PHA                     ; print line
	TXA
	PHA
        LDX     #$00
L8198   LDA     ($A8),X)
        BEQ     L81A8           ; if 0 exit
        JSR     OSASCI          ; OSASCI write char if = $0D print $0A as well
        INC     $A8
	BNE	L8198
        INC     $A9
	JMP	L8198
L81A8	PLA
	TAX
	PLA
	RTS
L81AC   LDA     ($F2),Y
        LDX     #$00
        CMP     #$2E
        BEQ     $81D3
        CMP     #$21
	BCS	L81BE
        LDA     ($A8,X)
	BEQ	L81D3
L81BC	CLC
	RTS
L81BE   LDA     ($A8,X)
	BEQ	L81BC
	JSR	L81D5
        CMP     ($F2),Y
	BNE	L81BC
	INY
        INC     $A8
	BNE	L81AC
        INC     $A9
	JMP	L81AC
L81D3	SEC
	RTS
L81D5   CMP     #$61
	BCC	L81E0
        CMP     #$7B
	BCS	L81E0
	SEC
        SBC     #$20
L81E0	RTS


L81E4   .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
        .byte      $00
L81FF   .byte      $55

L8200   .org	   $8200	 ; OS9 TABLE
#INCLUDE	os9_tab.asm











	.org	$A000
Extracode

;==================================================================================


OSpline PHA                     ; OS print line
	TXA
	PHA
        LDX     #$00
OSpl    LDA     ($A8),X)
        BEQ     OSex            ; if 0 exit
        JSR     $FFEE           ; 
        INC     $A8
        BNE     OSpl
        INC     $A9
        JMP     OSpl
OSex    PLA
	TAX
	PLA
	RTS




OSASCI
WRSERIAL
	PHA
	LDA $FD08
	AND #$02
        BEQ WRSERIAL         ; wait to TX empty
	PLA
	STA $FD09
        CMP     #$0D
        BNE     EX_SER       ; exit if not 'CR'
        LDA     #$0A
        JMP     WRSERIAL     ; print 'LF'
EX_SER
        RTS

INIT_MC6850
	LDA #03	; reset 6850
	STA $FD08
	LDA #$16	; SET 6850 / CLK DIV 64
	STA $FD08
        JMP OS9           ; Jump to OS9 Start up



OSRDCH	;JSR	$FFE0		; OSRDCH $8170
	RTS

OSWRCH	RTS			; OS write char

rst68k	LDA	#$3C		; $3C RST 68008	  + INTR CONTROL
	STA	$FC47		; 6821 REG 3 (RTC) CONTROL B set CB2 PIN 68008 RESET
	CLI			; ENABLE INTS
	JMP	return


return	PLA			; Exit from os9 system
	TAX
	PLA
	TAY
	LDA	#0
	RTS

putchr	;JMP	($020E)		; WRCHV
        ; JMP $E0A4           ; OSWRCH



;==========================================

	.org	$BF00
OS9	; start point

LFF00   LDA     #$40	; NMI Instruction   (ROM ADDRESS BF00)
        STA     $0D00
	SEI
	CLD
        LDX     #$FF
	TXS
        LDX     #$04
        STX     $01		; (00) = $0440
        STA     $00
	TAY
LFF11   STA     ($00),Y		; Clear ram $440 - $8000
        CMP     $01
	BEQ	LFF20
	INY
	BNE	LFF11
	INY
	INX
        INC     $01
	BPL	LFF11		; Branch until $8000

LFF20   LDX     #$0F					;(ROM ADDRESS BF20)
        STX     $FE42		;6522 SYSTEM VIA DATA Dir Reg 1=O/P
	DEX
        STX     $FE40		;6522 SYSTEM VIA IN/OUT reg B
        LDX     #$00
LFF2B   STX     $FE60		;6522 User VIA IN/OUT reg B
	DEX
	BNE	LFF2B		; Send $FF to 0 to User via (must be for test)
	JMP	$FF80
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	PHA
        LDA     #$FF
        STA     $FE40
        LDA     #$00
        STA     $FE40
	PLA
	RTI

        .byte      $FF

	.org	$BF80

LFF80   SEI			; setup 68000 vectors
	LDA     #$7F		; Disable all Ints            (ROM ADDRESS BF80)
	STA	$FE4E		; 6522 SYSTEM VIA INT EN reg 
        LDA     #$03		; PAGE 3
        STA     $FE30		; ROM Page Select  
        LDA     #$00
        STA     $2000
        STA     $2001
        STA     $2004
        STA     $2005
        STA     $2003
        STA     $2400
        LDA     #$40
        STA     $2002
	LDA	#$86		; reset vector msb
        STA     $2006
	LDA	#$62		; reset vector lsb ($8662)
        STA     $2007
	LDA	#$34		; 68008 (nreset) CB2 low (start 68008)
        STA     $FE60		; 6522 USER VIA = $34 (for testing)
        STA     $FC47		; MC6821 (RTC) control(3) reg B

LFFB6	LDA	#$00		; Get 68000 command
        STA     $2400
	JMP	$FFB6		; loop for ever

	.org	$BFFA
LFFFA   .word      $0D00        ; NMI $0D00  (ROM ADDRESS $BFFA)
;       .word   $FF00        ; Reset $FF00 (start) 
;       .word	$8000	        ; Start at $8000
	.word	INIT_MC6850	; start here
        .word      $FF3C        ; INT $FF3C

	.end	



