Skip to content

Commit

Permalink
Now peripherals work in either 1P or 2P ports
Browse files Browse the repository at this point in the history
  • Loading branch information
sik committed Jul 7, 2017
1 parent 38dd979 commit 5684bc0
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 49 deletions.
Binary file modified indigo.bin
Binary file not shown.
4 changes: 3 additions & 1 deletion source/kernel/buildme.68k
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
include "kernel/video.68k"
include "kernel/text.68k"
include "kernel/random.68k"
include "kernel/keyboard.68k"
include "kernel/devices.68k"
include "kernel/joypad.68k"
include "kernel/mouse.68k"
include "kernel/keyboard.68k"
include "kernel/sound.68k"
include "kernel/debug.68k"

Expand Down
99 changes: 99 additions & 0 deletions source/kernel/devices.68k
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
;****************************************************************************
; DEV_*
; List of known devices.
;****************************************************************************

rsreset
DEV_NONE: rs.b 1 ; No device present
DEV_MOUSE: rs.b 1 ; Sega/Mega mouse
DEV_KEYBOARD: rs.b 1 ; Saturn keyboard

KNOWN_DEVICES: rs.b 0 ; Number of known devices

;****************************************************************************
; InitDevices
; Initializes the I/O ports.
;----------------------------------------------------------------------------
; breaks: all
;****************************************************************************

InitDevices:
moveq #$40, d7 ; Some default value for the ports
lea ($A10000), a6
move.b d7, $09(a6)
move.b d7, $0B(a6)
move.b d7, $03(a6)
move.b d7, $05(a6)

clr.b (Device1P) ; No device detected yet
clr.b (Device2P)

rts ; End of subroutine

;****************************************************************************
; UpdateDevice
; Updates whatever device is present in the specified port.
;----------------------------------------------------------------------------
; input d7.w .... Which port
;----------------------------------------------------------------------------
; breaks: d5-d7, a4-a6
;****************************************************************************

UpdateDevice:
lea (Device1P), a5 ; Get device type address
lea (a5,d7.w), a5

add.w d7, d7 ; Get port address
lea ($A10003), a6
lea (a6,d7.w), a6

move.b (a5), d6 ; Invoke the relevant handler
and.w #$7F, d6
lsl.w #2, d6
lea @UpdateHandlers(pc), a4
move.l a5, -(sp)
move.l a6, -(sp)
jsr (a4,d6.w)
move.l (sp)+, a6
move.l (sp)+, a5

tst.w d7 ; Is this device in that port?
beq.s @Failed

;----------------------------------------------------------------------------

@Success:
ori.b #$80, (a5) ; Mark device as present
rts ; End of subroutine

;----------------------------------------------------------------------------

@Failed:
move.b (a5), d7 ; Try next device type next time
and.b #$7F, d7 ; Device is marked as not present
addq.b #1, d7
cmp.b #KNOWN_DEVICES, d7
blo.s @DontLoop
moveq #1, d7
@DontLoop:
move.b d7, (a5)

lsl.w #2, d7 ; Jump to the initialization for the
lea @InitHandlers(pc), a5 ; next device to try
jmp (a5,d7.w)

;----------------------------------------------------------------------------

@UpdateHandlers:
bra.w @Dummy ; DEV_NONE
bra.w UpdateMouse ; DEV_MOUSE
bra.w UpdateKeyboard ; DEV_KEYBOARD

@InitHandlers:
bra.w @Dummy ; DEV_NONE
bra.w InitMouse ; DEV_MOUSE
bra.w InitKeyboard ; DEV_KEYBOARD

@Dummy:
moveq #0, d7
rts
Empty file added source/kernel/joypad.68k
Empty file.
64 changes: 46 additions & 18 deletions source/kernel/keyboard.68k
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
;****************************************************************************
; InitKeyboard
; Initializes the keyboard.
; Initializes a port for a keyboard.
;----------------------------------------------------------------------------
; breaks: all
; input a6.l .... Pointer to port
;----------------------------------------------------------------------------
; breaks: d5-d7, a4-a6
;****************************************************************************

InitKeyboard:
lea ($A10003), a6 ; Set up hardware
move.b #$60, 6(a6)
move.b #$60, 6(a6) ; Set up hardware
move.b #$60, (a6)

moveq #0, d7 ; Reset key buffer
lea (KeyBuf), a6
move.l d7, (a6)+
move.l d7, (a6)+
move.l d7, (a6)+
move.l d7, (a6)+
move.w d7, (a6)+

clr.b (KeyMods) ; Reset modifiers
clr.b (KeyLeds)

rts ; End of subroutine

;****************************************************************************
; UpdateKeyboard
; Checks for keyboard input and sends new events as needed.
;----------------------------------------------------------------------------
; input a6.l .... Pointer to port
;----------------------------------------------------------------------------
; output d7.w ... 1 on success, 0 on failure
;----------------------------------------------------------------------------
; breaks: d5-d7, a4-a6
;****************************************************************************

UpdateKeyboard:
lea ($A10003), a6 ; Keyboard port
lea -4(sp), sp ; Input buffer
lea (sp), a5

Expand Down Expand Up @@ -100,10 +93,16 @@ UpdateKeyboard:
and.b #$70, d7
move.b d7, (KeyLeds)

@NoKeyboard:
lea 4(sp), sp ; Done with buffer
moveq #1, d7 ; Keyboard is present

@End:
move.b #$60, (a6) ; Let keyboard idle
lea 4(sp), sp ; Done with buffer
rts ; End of subroutine

@NoKeyboard:
moveq #0, d7 ; No keyboard available
bra.s @End ; Go clean up

;----------------------------------------------------------------------------

Expand Down Expand Up @@ -155,3 +154,32 @@ ReadKeyboardByte:
@Timeout:
moveq #-1, d7 ; An error has happened
rts ; End of subroutine

;****************************************************************************
; CleanUpKeyboard
; Cleans up the keyboard state if the keyboard is missing.
;----------------------------------------------------------------------------
; breaks: d5-d7, a4-a6
;****************************************************************************

CleanUpKeyboard:
move.b #$80|DEV_KEYBOARD, d7 ; Need to reset the keyboard state?
cmp.b (Device1P), d7
beq.s @Present
cmp.b (Device2P), d7
beq.s @Present

@Missing:
moveq #0, d7 ; Reset key buffer
lea (KeyBuf), a6
move.l d7, (a6)+
move.l d7, (a6)+
move.l d7, (a6)+
move.l d7, (a6)+
move.w d7, (a6)+

clr.b (KeyMods) ; Reset modifiers
clr.b (KeyLeds)

@Present:
rts ; End of subroutine
17 changes: 12 additions & 5 deletions source/kernel/main.68k
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ EntryPoint:
@ProbeRamEnd:
move.b d0, (RamSize)

bsr InitMouse ; Set up system
bsr InitKeyboard
bsr InitDevices ; Set up system
bsr InitSound
bsr InitVideo
bsr InitCursor
bsr InitHandles

bsr SetMouseDefaults ; Default settings

move.w #$8174, ($C00004) ; Turn on screen
move.w #$2000, sr ; Enable interrupts

Expand Down Expand Up @@ -102,11 +103,17 @@ Interrupt:
clr.b (ResetCount)
@ResetHandled:

bsr UpdateMouse ; Handle the cursor
bsr UpdateCursor
moveq #0, d7 ; Read peripherals
bsr UpdateDevice
moveq #1, d7
bsr UpdateDevice

bsr CleanUpMouse ; Clean up hardware state if needed
bsr CleanUpKeyboard

bsr UpdateCursor ; Handle the cursor
bsr RedrawCursor

bsr UpdateKeyboard ; Handle typing
bsr UpdateBeep ; Update the beeper

st.b (NewFrame) ; Mark that a new frame started
Expand Down
88 changes: 63 additions & 25 deletions source/kernel/mouse.68k
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
;****************************************************************************
; InitMouse
; Initializes the mouse.
; Initializes a port for a mouse.
;----------------------------------------------------------------------------
; breaks: all
; input a6.l .... Pointer to port
;----------------------------------------------------------------------------
; breaks: d5-d7, a4-a6
;****************************************************************************

InitMouse:
move.b #$60, ($A1000B) ; Set up hardware
move.b #$60, ($A10005)

clr.b (HasMouse) ; No mouse detected yet
clr.w (MouseX)
clr.w (MouseY)
clr.b (MouseButtons)

move.b ($A10001), d0 ; Take a rough guess as to whether
and.b #$C0, d0 ; it's a Mega or Sega mouse based
cmp.b #$80, d0 ; on the region
sne.b d0
and.b #1, d0

clr.b (MouseSwap) ; Default mouse settings
move.b d0, (MouseType)
move.b #32, (MouseSpeed)
move.b #$60, 6(a6) ; Set up port
move.b #$60, (a6)

rts ; End of subroutine

;****************************************************************************
; UpdateMouse
; Reads the mouse and updates its status.
;----------------------------------------------------------------------------
; input a6.l .... Pointer to port
;----------------------------------------------------------------------------
; output d7.w ... 1 on success, 0 on failure
;----------------------------------------------------------------------------
; breaks: d5-d7, a4-a6
;****************************************************************************

UpdateMouse:
lea -10(sp), sp ; Make room for the data
lea ($A10005), a6 ; Mouse port

move.b (a6), d7 ; Check if mouse is there
and.b #$0F, d7
Expand Down Expand Up @@ -68,7 +58,7 @@ UpdateMouse:
cmp.l #$0B0F0F00, d7
bne @Error

st.b (HasMouse) ; Mouse available!
; st.b (HasMouse) ; Mouse available!

moveq #0, d7 ; Get motion
move.b 5(sp), d7
Expand Down Expand Up @@ -123,25 +113,73 @@ UpdateMouse:
move.b d7, (MouseButtons) ; Store button status

lea 10(sp), sp ; Get rid of the buffer
moveq #1, d7 ; Mouse is present
rts ; End of subroutine

;----------------------------------------------------------------------------

@Error:
move.b #$60, (a6) ; Leave mouse alone

clr.b (HasMouse) ; No mouse present!
clr.w (MouseX) ; Reset mouse status just in case
clr.w (MouseY)

lea 10(sp), sp ; Get rid of the buffer
moveq #0, d7 ; Mouse not available
rts ; End of subroutine

;----------------------------------------------------------------------------

@Pattern:
dc.w $2010,$0000,$2010,$0000,$2010,$0000,$2010,$0000,$2010

;****************************************************************************
; CleanUpMouse
; Cleans up the mouse state if the mouse is missing.
;----------------------------------------------------------------------------
; breaks: d5-d7, a4-a6
;****************************************************************************

CleanUpMouse:
move.b #$80|DEV_MOUSE, d7 ; Check if a mouse was detected
cmp.b (Device1P), d7
beq.s @Present
cmp.b (Device2P), d7
beq.s @Present

;----------------------------------------------------------------------------

@Missing:
clr.b (HasMouse) ; No mouse detected yet
clr.w (MouseX)
clr.w (MouseY)
clr.b (MouseButtons)

rts ; End of subroutine

;----------------------------------------------------------------------------

@Present:
st.b (HasMouse) ; Mark mouse as available
rts ; End of subroutine

;****************************************************************************
; SetMouseDefaults
; Sets the default settings for the mouse.
;----------------------------------------------------------------------------
; breaks: all
;****************************************************************************

SetMouseDefaults:
move.b ($A10001), d7 ; Take a rough guess as to whether
and.b #$C0, d7 ; it's a Mega or Sega mouse based
cmp.b #$80, d7 ; on the region
sne.b d7
and.b #1, d7

clr.b (MouseSwap) ; Default mouse settings
move.b d7, (MouseType)
move.b #32, (MouseSpeed)

rts ; End of subroutine

;****************************************************************************
; GetMouseType
; Retrieves the current mouse type.
Expand Down
3 changes: 3 additions & 0 deletions source/kernel/variables.68k
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

rsset $FFC000

Device1P: rs.b 1 ; Device in port 1
Device2P: rs.b 1 ; Device in port 2

MouseX: rs.w 1 ; Mouse X motion
MouseY: rs.w 1 ; Mouse Y motion
MouseButtons: rs.b 1 ; Status of each mouse button
Expand Down

0 comments on commit 5684bc0

Please sign in to comment.