TITLE DIAGNOSTIC - COPYRIGHT (C) 1984 Commodore Business Machines -
NAME DIAG
page 66,132


PUBLIC CHECKPOINT,CHECK_DIAG,DIAGNOSTIC
PUBLIC FIND_PARITY
;
; PARITY LOGIC I/O :
;
NMI_REG   EQU  0A0H			; NMI ENABLE LATCH	W/O
PORT_B	  EQU	61H			; CLEAR PARITY CHECKERS R/W
PORT_C	  EQU	62H			; READ PARITY BITS	R/O
;
PARITY_OFF   EQU  30H			; RESET PARITY CHECKERS PORT_B
PARITY_CHECK EQU 0C0H			; PARITY FAULT BITS	PORT_C

EXTRN	  INTERNAL_PRINTER:ABS		; PORT OF BUILT-IN PRINTER
DISK_BOOT EQU 1 			; BIT #0 OF EQUIPMENT FLAG INDICATES
					; TO HALT ON ERROR (IF 0)
DIAG_ROM  SEGMENT   AT 0C000H		; SEGMENT OF DIAGNOSTIC ROM
CHECKWORD1     LABEL	 WORD		; IF VALID ROM , CONTAINS AA55
	  ORG	    5			; DIAGNOSTIC MARKER
CHECKWORD2     LABEL	 WORD		; IF DIAG ROM, CONTAINS 6A65H
	  ORG	    7			; **** REV 1C ****
DIAG_ENTRY     LABEL	 FAR		; ENTRY IS HERE
DIAG_ROM  ENDS

BIOS_DATA SEGMENT AT 40H
EXTRN	  EQUIP_FLAG:WORD		; COPY OF DIP SWITCHES
EXTRN	  DIAG_CODE:BYTE		; ERROR CODE
BIOS_DATA ENDS

BIOS_CODE SEGMENT PARA COMMON 
ASSUME CS:BIOS_CODE

	       ORG 0F4CEH

EXTRN	  DDS:NEAR			; SET DS TO BIOS_DATA
					; DESTROYS .AX
EXTRN	  PRINT_BYTE:NEAR		; PRINT HEX BYTE IN .DH
EXTRN	  PRINT_WORD:NEAR		; PRINT HEX WORD IN .DX
EXTRN	  OUT_STRING:NEAR		; PRINT STRING POINTED TO BY .SI
EXTRN	  POWER_UP:NEAR 		; RESTARTS SYSTEM
;*********************************************************************
; Installs check code in .AL on internal printer port.
; If any test following that call failes, this code will remain there
; and indicate error reason
; DESTROYS .DX
;*********************************************************************
CHECKPOINT     PROC NEAR		; INSTALL CHECKPOINT CODE ON
ASSUME DS:BIOS_DATA
     PUSH DS				; INTERNAL PRINTER PORT
     CALL DDS
     MOV  DIAG_CODE,AL
     MOV  DX,INTERNAL_PRINTER
     OUT  DX,AL
     POP  DS				; SAVES ALL REGS
     RET
CHECKPOINT     ENDP
;*************************************************************************
; CHECK_DIAG
; ----------
; THIS SUBROUTINE WILL CHECK THE STATUS OF DIP SWITCH #1, BOOT/LOOP
; IF SWITCH IS OFF (==1) IT WILL RETURN TO CALLING PROGRAM
; IF SWITCH INDICATES LOOP IT WILL FALL INTO DIAGNOSTIC ENTRY (SEE BELOW)
; AND HALT THE SYSTEM TO MAKE SHURE THE ERROR CODE IS DISPLAYED
; ON THE DIAGS ERROR DISPLAY (PRINTER PORT)
;**************************************************************************
DIAGMSG   LABEL     NEAR
     DB   "Diag: ",0

CHECK_DIAG     PROC NEAR			  ; DESTROYS .AX
     ASSUME DS:BIOS_DATA
     PUSH      DS				  ; SAVE DS
     CALL      DDS				  ; MAP TO DATA SEGMENT
     TEST      WORD PTR EQUIP_FLAG,DISK_BOOT	  ; BOOT FROM DISK DESIRED ?
     JNZ       DIAG_RET 			  ; IF NZ, RETURN	  

     MOV  SI,OFFSET DIAGMSG			  ; PRINT "DIAG: "
     CALL OUT_STRING
     MOV  DH,DIAG_CODE				  ; GET LAST ERROR CODE
     CALL PRINT_BYTE				  ; PRINT ERROR #
     MOV  DH,DIAG_CODE				  ; FALL INTO DIAGNOSTIC ROM

CHECK_DIAG     ENDP
;**************************************************************************
; DIAGNOSTIC
; ----------
; THIS ENTRY IS ENTERED VIA JMP WHENEVER A FATAL SYSTEM ERROR OCCURED
; DURING POWRUP SEQUENCE.
; THIS RUOTINE WILL LOOK FOR A DIAGNOSTIC ROM AT C000:0 AND ENTER IT
; @ OFFSET 7 IF PRESENT. HERE SOME MORE ERROR ANALYSIS CAN BE DONE
; THE SECTION THAT JUMPED HERE HAS ITS ERROR CODE IN .DH	     
;      
; IF NO ROM IS FOUND, THE MACHINE RUNS IN AN OPEN LOOP.
;**************************************************************************
DIAGNOSTIC PROC NEAR

     MOV  AX,DIAG_ROM			 ; CS OF DIAG ROM
     MOV  DS,AX 			 ; POINT THERE

ASSUME DS:DIAG_ROM

     CMP  WORD PTR CHECKWORD1,0AA55H	 ; VALID ROM ?
     JNE  DIAGHLT			 ; IF NE, NOT PRESENT - HALT THE SYSTEM
     CMP  WORD PTR CHECKWORD2,06A65H	 ; IS IT THE DIAG ROM ?
     JNE  DIAGHLT			 ; IF NE, NOT PRESENT - HALT THE SYSTEM
     CALL DIAG_ROM:DIAG_ENTRY		 ; ..JUMP TO OFFBOARD MFG TESTS
					 ; THIS CALL IS FAR
DIAG_RET:
     POP  DS				 ; IF RETURNING, RECOVER DS
     RET				 ; TO ALLOW FOR RETURN IF DESIRED
					 ; (NEAR RETURN)
DIAGHLT:
     CLI
     JMP   POWER_UP			 ; HALT SYSTEM, NO DIAG ROM FOUND.

DIAGNOSTIC ENDP   

FIND_PARITY PROC NEAR

     SUB    AL,AL
     OUT    NMI_REG,AL			; DISABLE NMI's
     IN     AL,PORT_B			; CLEAR PARITY LATCHES
     OR     AL,PARITY_OFF
     OUT    PORT_B,AL
     AND    AL, NOT PARITY_OFF
     OUT    PORT_B,AL
     CALL   DDS
     SUB    AX,AX
PLOOP1:
     MOV    DS,AX			; CHECK 64K
PLOOP:
     LODSB				; MEMORY READ
     IN     AL,PORT_C			; PARITY TOGGLED ?
     AND    AL,PARITY_CHECK
     JNE    P_FOUND			; YES, IF NE
     LOOP   PLOOP			; ELSE CONTINUE
     MOV    AX,DS			; SI,CX SHOULD BE 0 HERE
     ADD    AX,1000H			; NEXT 64K BLOCK
     CMP    AX,0A000H			; 640K DONE ?
     JNE    PLOOP1			; IF NE, CONTINUE
     RET				; ELSE DONE   

P_FOUND:
     MOV   DX,DS			; SEGMENT
     CALL  PRINT_WORD			; 1ST 4 OF 8 DIGITS
     MOV   DX,SI			; OFFSET		
     JMP   PRINT_WORD			; RETURN THERE
FIND_PARITY ENDP

SIXTEEN LABEL WORD
     DW    16
BIOS_CODE  ENDS
     END  
