Tutorial 7
8255 Mode 2
Hardware:
D7-D0 D7- D0 P
A D7 – D0
A1
A1 PC7 OBF’
A0 A0 Floppy
8255 P
PC6 ACK’
Controller
C PC4 STB’
𝐼𝑂𝑅𝐷 PC5 IBUF’
𝑅𝐷
𝐼𝑂𝑊𝑅
𝑊𝑅
𝐶𝑆
A15-A2 Address
Decoder
Control Word
Port A Mode 2
Port B Don’t care (will configure as
output)
Port C Gets automatically configured as
control signals except PC0-PC2 (will
configure as output)
Control Word
Mode D6 D5 PA PCH D2 PB PCL
1 1 0 0 0 0 0 0
.MODEL TINY
.186
.DATA
MESSAGE DB 'Hello world' ; Message to write
MSG_LEN DB $-MESSAGE
BUFFER DB 11 DUP(0) ; Buffer for readback
ERROR_MSG DB 'Verification failed!$'
SUCCESS_MSG DB 'Verification successful!$'
.CODE
.STARTUP
;Port addresses
PORT_A EQU 50h ; Data port (bidirectional)
PORT_B EQU 51h ; Not used in this configuration
PORT_C EQU 52h ; Status/Control port
CMD_REG EQU 53h ; Command register
; Define FDC commands (ASCII)
CMD_RESET EQU 'X' ; Reset drive head
CMD_MOVE EQU 'M' ; Move head by one byte
CMD_WRITE EQU 'W' ; Write byte
CMD_READ EQU 'R' ; Read byte
; Configure 8255 in Mode 2 (bidirectional)
MOV AL, 0C0H ; Mode 2, Port A bidirectional
OUT CMD_REG, AL
; Reset the disk head to beginning
CALL RESET_DISK
; Write the message to disk
LEA SI, MESSAGE ; Point to message
MOV CX, MSG_LEN ; Length of message
CALL WRITE_TO_DISK
;Reset head again to read from beginning
CALL RESET_DISK
; Read back the data
LEA DI, BUFFER ; Point to buffer
MOV CX, MSG_LEN ; Length to read
CALL READ_FROM_DISK
; Verify the data
CALL VERIFY_DATA
.exit
; Subroutine to reset disk head
RESET_DISK PROC NEAR
MOV AL, CMD_RESET
OUT PORT_A, AL ; Send reset command
CALL WAIT_FOR_ACK ; Wait for acknowledgment
RET
RESET_DISK ENDP
; Subroutine to write data to disk Input: SI = pointer to data, CX = length
WRITE_TO_DISK PROC NEAR
WRITE_LOOP:
MOV AL, CMD_WRITE ; Write command
OUT PORT_A, AL
CALL WAIT_FOR_ACK
MOV AL, [SI] ; Get data byte
OUT PORT_A, AL ; Send data
CALL WAIT_FOR_ACK
MOV AL, CMD_MOVE ; Move head
OUT PORT_A, AL
CALL WAIT_FOR_ACK
INC SI ; Next byte
LOOP WRITE_LOOP
RET
WRITE_TO_DISK ENDP
; Subroutine to read data from disk; Input: DI = pointer to buffer, CX = length
READ_FROM_DISK PROC NEAR
READ_LOOP:
MOV AL, CMD_READ ; Read command
OUT PORT_A, AL
CALL WAIT_FOR_ACK
CALL WAIT_FOR_STRB
IN AL, PORT_A ; Read data byte
MOV [DI], AL ; Store in buffer
MOV AL, CMD_MOVE ; Move head
OUT PORT_A, AL
CALL WAIT_FOR_ACK
INC DI ; Next position
LOOP READ_LOOP
RET
READ_FROM_DISK ENDP
; Subroutine to wait for acknowledgment from FDC
WAIT_FOR_ACK PROC NEAR
PUSH AX
WAIT_LOOP1:
IN AL,PORT_C ; Read status
AND AL, 01000000b ; Check ACK’ signal
JNZ WAIT_LOOP1 ; Wait until FDC is ready
POP AX
RET
WAIT_FOR_ACK ENDP
; Subroutine to wait for strobe from FDC
WAIT_FOR_STRB PROC NEAR
PUSH AX
WAIT_LOOP2:
IN AL,PORT_C ; Read status
AND AL, 00010000b ; Check Stobe’ signal
JNZ WAIT_LOOP2 ; Wait until FDC is ready
POP AX
RET
VERIFY_DATA PROC NEAR
LEA SI, MESSAGE
LEA DI, BUFFER
MOV CX, MSG_LEN
VERIFY_LOOP:
MOV AL, [SI]
CMP AL, [DI]
JNE VERIFY_FAIL
INC SI
INC DI
LOOP VERIFY_LOOP
; Verification successful
LEA DX, SUCCESS_MSG
JMP DISPLAY_RESULT
VERIFY_FAIL:
LEA DX, ERROR_MSG
DISPLAY_RESULT:
MOV AH, 09h
INT 21h
RET
VERIFY_DATA ENDP
END