 lib environment
 lib inttab
 lib fio
 lib iop/fio_codes
 data
 sttl Machine Dependent Rouintes
 pag
 name mach
 global clkinz,mchint,endinit

*
* clkinz
*
* Initialize system clock.
*

clkinz ldb #0 clear out lbolt counter
 stb lbolt
 lda stup set auto update counter
 sta tmtuct
 lda #1 reset the clock
 sta CLKSEL
 lda #%10000000 set reset bit
 sta CLOCK save in control
 clr CLKSEL reset control
 ldd #HZTIM set gimix clock rate
 std CLKLAT set latch count
 lda #CLKCON get control byte
 sta CLOCK send to clock
 lda #1 select timer 1
 sta CLKSEL
 clr CLOCK reset clocks
 rts return


*
* mchint
*
* Initialize all hardware junk and try to find
* the existing acias.  The ones that aren't there
* need to have their mask bits cleared.
*

mchint clr strm # terminals calculated here!
 lda cputyp get cpu info from UniBUG
 bita #%01000000 is it 50 HZ?
 beq mchin1
 sta sphz set 50 HZ flag
mchin1
 ldb #$D0 Clear out any WD Floppy Interrupts
 stb DMFCOM
mchin2 ldx #inttab point to int table
 ldb ,x+ pick up number of devices
 pshs b
mchin3 ldd indev,x check device # for TTYxx
 cmpd #$0100
 bhs 0f jump if not TTYxx
 inc strm
0 lda intype,x get interrupt device type
 cmpa #0 ACIA?
 bne 00f no
*
* Initialize ACIA port
*
 ldu instat,x get ACIA address
 lda #$43 set up reset code
 sta 0,u reset acia
 pshs d delay
 puls d
 lda 0,u check response
 beq 0f
 anda #%11110011
 beq 0f
 clr inmask,x zero int mask
0 cmpu scon was this the system console
 bne 1f no - continue
 pshs x save registers
 jsr syscon yes - go reinitialize console
 puls x restore registers
1 lbra 90f continue initialization
*
* Initialize PIA port
*
00 cmpa #1 PIA?
 bne 00f no
 ldu instat,x pick up PIA address
 leau -1,u point at PIA
 lda #$2e set up pia
 sta 1,u
 pshs d delay
 puls d
 lda 0,u
 pshs d
 puls d
 tst 1,u
 bpl 0f
 clr inmask,x zero int mask
0 lbra 90f continue initialization
*
* Initialize Intelligent I/O Processor
*
00 cmpa #2 IOP device?
 lbne 00f no
 inc strm -- Counts for 3 terminals!
 inc strm
 ldu instat,x get FIO base address
 ldy intbrg,x
* -- Master reset of IOP
 sta FIOint,u
 jsr fio_delay pause for at least 9 usec
 lda FIOctl,u
 clra
 sta FIOctl,u
 lda #1
 sta FIOctl,u
 clra
 sta FIOctl,u
 lda #ctl_reg0 init control register 0
 ldb #$96
 jsr fio_put
 lda #ist_reg0
 ldb #FIO_EMI enable message interrupts
 jsr fio_put
 lda #ctl_reg3
 ldb #$40 direction=out, control=port 1
 jsr fio_put
 lda #ctl_reg2
 ldb #1 enable port 2
 jsr fio_put
 lda #pat_mat make sure FIO functions
 ldb #$55
 jsr fio_put
 jsr fio_get
 cmpb #$55 see if match
 bne 10f
 ldy #2000
05 lda #ist_reg0
 jsr fio_get
 bitb #$20 mailbox full?
 bne 20f
 ldd #500
07 subd #1
 bne 07b
 leay -1,y
 cmpy #0
 bne 05b
10
 if 0
 pshs d,x,u
 ldx #msg00
 jsr Pdata
 ldd 4,s
 jsr Phex2
 puls d,x,u
 endif
 bra 25f
20 jsr fio_rcv fetch message
 cmpb #R_RESET system up message?
 beq 30f yes - OK
 if 0
 pshs d,x,u
 ldx #msg01
 jsr Pdata
 ldd 4,s
 jsr Phex2
 ldx #msg02
 jsr Pdata
 lda 1,s
 jsr Phex
 puls d,x,u
 endif
* IOP not there or didn't respond!
25 clr inmask,x ignore interrupts from this IOP
 clr inmask+INTSIZ,x
 clr inmask+(2*INTSIZ),x
 bra 50f
30 jsr fio_rcv get IOP ROM version
 pshs b save version code
 ldd indev,x system IOP?
 bne 35f no - continue
 pshs d,x save parameters
 jsr syscon yes - reopen tty00
 puls d,x
35 puls b restore IOP ROM version
 cmpb #$10 Major/Minor
 bhs 40f jump if OK
 pshs d,x
 ldx #badrom0
 jsr Pdata
 tfr d,u
 jsr Phex2
 ldx #badrom1
 jsr Pdata
 lda 1,s
 jsr Phex
 puls d,x
40 ldy intbrg,x get IOP control structure
 leau iop_Q,y initialize pointers
 stu iop_Qptr,y
 leau iop_Q+3,y
 stu iop_Qend,y
50 leax 2*INTSIZ,x IOP takes up three slots
 dec 0,s
 dec 0,s
00
90 leax INTSIZ,x bump to next entry
 dec 0,s end?
 lbne mchin3
 puls b reset stack
*-- Go determine type of winchester controller
 jsr win_test
*
 rts
 if 0
msg00 fcc $d,"IOP didn't respond - IOP = $",0
msg01 fcc $d,'Illegal response from IOP at $',0
msg02 fcc ', Code = $',0
msg11 fcc $d,'Syscon called',0
msg10 fcc $d,'System IOP',0
msg20 fcc $d,'Init IOP - Dev = ',0
msg21 fcc ', Addr = ',0
msg30 fcc $d,'Init Dev, Cnt = ',0
msg31 fcc ', Table = ',0
 endif
badrom0 fcc $d,'Bad IOP ROM at $',0
badrom1 fcc ', Version: ',0

*
*  End machine initialization
*
endinit rts
