added i2c controller functions, added IDE interface functions, fiexd IDE schematics

This commit is contained in:
Dennis Gunia
2023-01-16 19:30:31 +01:00
parent 265342e7f7
commit 96c4dca4d5
34 changed files with 18931 additions and 3700 deletions

View File

@@ -0,0 +1,243 @@
;----------------------------------------------------------------
;BIOS Driver for IDE Interface 82C55
;by Dennis Gunia (01/2023)
;----------------------------------------------------------------
;================================================================
; I/O registers
;================================================================
CS_PIA_PA .EQU 0x30 ; D0-7
CS_PIA_PB .EQU 0x31 ; D8-15
CS_PIA_PC .EQU 0x32 ; Controll Lines
CS_PIA_CR .EQU 0x33
;================================================================
; I/O pins
;================================================================
IDE_WR .EQU 00100000b
IDE_RD .EQU 01000000b
IDE_RST .EQU 10000000b
;================================================================
; IDE registers
;================================================================
IDE_REG_DATA .EQU 01000b ;data I/O register (16-bits)
IDE_REG_ERROR .EQU 01001b ;error information register when read; write precompensation register when written.
IDE_REG_SECTOR .EQU 01010b ;Sector counter register
IDE_REG_SSECTOR .EQU 01011b ;Start sector register
IDE_REG_LCYL .EQU 01100b ;Low byte of the cylinder number
IDE_REG_HCYL .EQU 01101b ;High two bits of the cylinder number
IDE_REG_HEAD .EQU 01110b ;Head and device select register
IDE_REG_CMDSTS .EQU 01111b ;command/status register
IDE_REG_ALTSTS .EQU 10110b ;Alternate Status/Digital Output
IDE_REG_DRVADDR .EQU 10111b ;Drive Address
;================================================================
; I/O access functions
;================================================================
;------------------------------------------------------------------------------
; ide_reset
;
; resets drives on bus
;------------------------------------------------------------------------------
ide_reset:
LD A, 10000000b ;CommandByte-A, Mode 0, PA Out, PC Out, PB Out
OUT (CS_PIA_CR), A ;Set Data direction to out
LD A, IDE_RST
OUT (CS_PIA_PC), A ;Reset IDE Device
NOP
XOR A
OUT (CS_PIA_PC), A ;end device reset
RET
;------------------------------------------------------------------------------
; ide_regwrite_8
;
; Sends data to the IDE device
; A contains DATA
; B contains register number
;------------------------------------------------------------------------------
ide_regwrite_8:
PUSH AF ;store date to stack
; Prepare PIA Data Direction
LD A, 10000000b ;CommandByte-A, Mode 0, PA Out, PC Out, PB Out
OUT (CS_PIA_CR), A ;Set Data direction to out
; Write Data out
POP AF
OUT (CS_PIA_PA), A ;Write Data to bit 0-7
;Prepare Address
LD A, B ;Load register address
AND 00011111b ;Mask unused bits
OUT (CS_PIA_PC), A ;Write Data to bit controll lines
OR IDE_WR ;Set Write bit
OUT (CS_PIA_PC), A ;Set write signal
NOP ;delay to wait for processing
LD A, B ;Load register address
AND 00011111b ;Mask unused bits
OUT (CS_PIA_PC), A ;disable write signal
NOP
XOR A ;clear register A
OUT (CS_PIA_PC), A ;clear controll lines
RET
;------------------------------------------------------------------------------
; ide_regread_8
;
; Sends data to the IDE device
; B contains register number
; A returns data
;------------------------------------------------------------------------------
ide_regread_8:
LD A, 10010010b ;CommandByte-A, Mode 0, PA IN, PC Out, PB IN
OUT (CS_PIA_CR), A ;Set Data direction to in
;Prepare Address
LD A, B ;Load register address
AND 00011111b ;Mask unused bits
OUT (CS_PIA_PC), A ;Write Data to bit controll lines
OR IDE_RD ;Set Write bit
OUT (CS_PIA_PC), A ;Write Data to bit controll lines
NOP ;delay to wait for processing
PUSH AF
POP AF
PUSH AF
POP AF
PUSH AF
POP AF
PUSH AF
POP AF
IN A,(CS_PIA_PA) ;read data from ide device to b (because a is used later)
PUSH AF
XOR A ;clear register A
OUT (CS_PIA_PC), A ;clear controll lines
POP AF ;put data in accumulator
RET
;------------------------------------------------------------------------------
; ide_readsector_256
;
; Reads IDE Data
; HL contains destination address
;------------------------------------------------------------------------------
ide_readsector_256:
LD C,255 ;Setup counter for 256 words
ide_readsector_256_waitloop:
LD B, IDE_REG_CMDSTS
CALL ide_regread_8
BIT 0,a ;Error Bit set.
JP NZ, ide_printerror
BIT 3,a ;DRQ Bit set. If set, disk has data
JR Z, ide_readsector_256_waitloop ;If not set, wait
LD A, 10010010b ;CommandByte-A, Mode 0, PA IN, PC Out, PB IN
OUT (CS_PIA_CR), A ;Set Data direction to IN
LD A, IDE_REG_DATA ;CS0 and A=0 -> I/O register
OUT (CS_PIA_PC), A ;set register
OR IDE_RD ;Set Read bit
OUT (CS_PIA_PC), A ;Write Read to bit controll lines
NOP
NOP
NOP
IN A,(CS_PIA_PB) ;Load 16-Bit data to buffer
LD (HL), A
INC HL
IN A,(CS_PIA_PA)
LD (HL), A
INC HL
LD A,C
OR A
JP Z,ide_readsector_256_done
DEC C
JR ide_readsector_256_waitloop
ide_readsector_256_done:
RET
ide_readsector_256_inv:
LD C,255 ;Setup counter for 256 words
ide_readsector_256_inv_waitloop:
LD B, IDE_REG_CMDSTS
CALL ide_regread_8
BIT 0,a ;Error Bit set.
JP NZ, ide_printerror
BIT 3,a ;DRQ Bit set. If set, disk has data
JR Z, ide_readsector_256_inv_waitloop ;If not set, wait
LD A, 10010010b ;CommandByte-A, Mode 0, PA IN, PC Out, PB IN
OUT (CS_PIA_CR), A ;Set Data direction to IN
LD A, IDE_REG_DATA ;CS0 and A=0 -> I/O register
OUT (CS_PIA_PC), A ;set register
OR IDE_RD ;Set Read bit
OUT (CS_PIA_PC), A ;Write Read to bit controll lines
NOP
NOP
NOP
IN A,(CS_PIA_PA) ;Load 16-Bit data to buffer
LD (HL), A
INC HL
IN A,(CS_PIA_PB)
LD (HL), A
INC HL
LD A,C
OR A
JP Z,ide_readsector_256_done
DEC C
JR ide_readsector_256_inv_waitloop
;------------------------------------------------------------------------------
; ide_writesector_256
;
; Writes 512 bytes (256 words) of IDE Data
; HL contains data start address
;------------------------------------------------------------------------------
ide_writesector_256:
RET ;NOT IMPLEMENTED
;================================================================
; utility functions
;================================================================
;------------------------------------------------------------------------------
; ide_printerror
;
; prints IDE error to console
;------------------------------------------------------------------------------
ide_printerror:
LD HL, [str_error_start]
CALL print_str
LD B, IDE_REG_CMDSTS
CALL ide_regread_8
CALL print_a_hex
LD HL, [str_error_start1]
CALL print_str
LD A,(MEM_IDE_DEVICE)
CALL print_a_hex
LD HL, [str_error_start2]
CALL print_str
LD B, IDE_REG_ERROR
CALL ide_regread_8
CALL print_a_hex
LD A,10
CALL print_char
LD A,13
CALL print_char
RET
str_error_start:
db 13,10,"Disk I/O error. Status: 0x",0
str_error_start1:
db " Dev: 0x",0
str_error_start2:
db " Err: 0x",0

View File

@@ -0,0 +1,93 @@
;----------------------------------------------------------------
;BIOS Driver for IDE Access
;by Dennis Gunia (01/2023)
;----------------------------------------------------------------
;================================================================
; IDE commands
;================================================================
IDE_CMD_IDENT .EQU 0xEC ;Identify drive.
IDE_CMD_READSEC .EQU 0x20 ;Read sectors.
;================================================================
; IDE Variables
;================================================================
MEM_IDE_BASE .EQU 0x5000
MEM_IDE_DEVICE .EQU MEM_IDE_BASE ;1Byte: Device ID for IDE-Port, Controller and Master/Slave
MEM_IDE_STATUS .EQU MEM_IDE_BASE + 1 ;1Byte: 0x00 if status is okay
MEM_IDE_BUFFER .EQU MEM_IDE_BASE + 2 ;512Byte: buffer for read/write data
;================================================================
; IDE funtions
;================================================================
;------------------------------------------------------------------------------
; ideif_init_drive
;
; initializes drive
;------------------------------------------------------------------------------
ideif_init_drive:
xor a
ld (MEM_IDE_DEVICE),A ;Set device to 0
ld (MEM_IDE_STATUS),A ;Set status to 0 (OK)
di ;disable interrupt
call ide_reset ;Reset drives on bus
ld hl, [str_dev_waitready]
call print_str ;print waiting message
ld DE, 0x1FFF ;preload timeout counter
ideif_init_drive_loop1:
ld b, IDE_REG_CMDSTS
call ide_regread_8 ;read drive status register
OR A
JR Z, ideif_init_drive_nodrv ;no drive found
BIT 6,A ;Wait for device ready
JR NZ, ideif_init_drive_detected
DEC DE ; decrement timeout
LD A,D
OR E
JR Z, ideif_init_drive_nodrv
JR ideif_init_drive_loop1
ideif_init_drive_nodrv:
ld hl, [str_dev_notfound]
call print_str
RET
ideif_init_drive_detected:
ld hl, [str_dev_ready]
call print_str
LD B, IDE_REG_CMDSTS ;Get drive identification
LD A, IDE_CMD_IDENT
call ide_regwrite_8 ;Write command to drive
LD HL,MEM_IDE_BUFFER ;set read/write buffer start address
call ide_readsector_256 ;read 256 words from device
LD HL,MEM_IDE_BUFFER + 20 ;print device serial
LD B, 20
CALL print_str_fixed
ld hl, [str_dev_ready2]
call print_str
LD HL,MEM_IDE_BUFFER + 54 ;print device name
LD B, 40
CALL print_str_fixed
LD A,10 ;New line
CALL print_char
LD A,13
CALL print_char
RET
;================================================================
; IDE strings
;===============================================================
str_dev_waitready:
db 13,10,"Seek HDD ... ",0
str_dev_ready:
db "Device Found!",13,10,"Serial: ",0
str_dev_ready2:
db " Name: ",0
str_dev_notfound:
db "no drive detected",13,10,0

View File

@@ -0,0 +1,90 @@
;----------------------------------------------------------------
;HEX and ASCII dump function
;by Dennis Gunia (01/2023)
;----------------------------------------------------------------
;------------------------------------------------------------------------------
; dump_pretty
;
; Dumps memory content
; B contains amount of rows
; HL contains start address
; Destroys BC, HL
;------------------------------------------------------------------------------
dump_pretty:
PUSH HL
LD HL,[STR_PD_HEADER] ;Print header
CALL print_str
POP HL
dump_pretty_row:
LD A,B ;Check row counter
OR A
JP Z, dump_pretty_end ;If counter is 0, exit
DEC B ;Decrement row counter by 1
LD C, 16 ;Load column counter
LD A, H ;Print base address
CALL print_a_hex
LD A, L
CALL print_a_hex
LD A, ' '
CALL print_char
dump_pretty_col: ;Loop for column
LD A,(HL) ;Load byte to disply
CALL print_a_hex
LD A, ' '
CALL print_char
INC HL
DEC C ;Decrement column counter
JR NZ, dump_pretty_col ;Loop if not 0
dump_pretty_ascii:
PUSH BC
PUSH HL
LD B,0
LD C,16
SBC HL,BC ;Reset HL by column count
dump_pretty_ascii_loop:
LD A,(HL)
INC HL
CP 32
JP C, dump_pretty_ascii_none ;if less than 32, it is not a char
CP 127
JP NC, dump_pretty_ascii_none ;if greater or equal than 128, it is not a char
call print_char
jr dump_pretty_ascii_cont
dump_pretty_ascii_none:
LD A,'.'
call print_char
dump_pretty_ascii_cont:
DEC C
JP NZ, dump_pretty_ascii_loop
POP HL
POP BC
dump_pretty_nextrow:
LD A,10 ;New line
CALL print_char
LD A,13
CALL print_char
JR dump_pretty_row ;Else next line
dump_pretty_end:
RET
STR_PD_HEADER:
db 13,10,'BASE 0 1 2 3 4 5 6 7 8 9 A B C D E F ASCII',13,10,0
;------------------------------------------------------------------------------
; print_str_fixed
;
; Prints string with fixed length
; B contains length
; HL contains start address
;------------------------------------------------------------------------------
print_str_fixed:
LD A,(HL)
INC HL
CALL print_char
DJNZ print_str_fixed
RET