diff --git a/debug.asm b/debug.asm index 14ee1fc..386b098 100644 --- a/debug.asm +++ b/debug.asm @@ -49,7 +49,9 @@ Drive64Write: sw t3, PI_RD_LEN(t5) // "read" from DRAM to cart // PI_WAIT() // if we always wait before doing operations, this shouldn't be necessary -Drive64WriteDirect: // TODO: rewrite so this takes a0, a1 instead of a2, a3 +Drive64WriteDirect: // TODO: rewrite so this takes a0,a1 instead of a2,a3 + lli v0, 0 + lui at, 0x0100 // set printf channel or a3, a3, at lli t1, 0x08 // WRITE mode @@ -61,14 +63,17 @@ Drive64WriteDirect: // TODO: rewrite so this takes a0, a1 instead of a2, a3 lui t9, K_BASE lw t9, K_CI_BASE(t9) - CI_USB_WRITE_WAIT() // clobbers t0, requires t9 + CI_USB_WRITE_WAIT(0x10000) // clobbers t0,v0, requires t9 + bnez v0, Drive64WriteExit + nop + sw a2, CI_USB_PARAM_RESULT_0(t9) PI_WAIT() // yes, these waits seem to be necessary sw a3, CI_USB_PARAM_RESULT_1(t9) PI_WAIT() sw t1, CI_USB_COMMAND_STATUS(t9) -// if we always wait before doing operations, this shouldn't be necessary: -// CI_USB_WRITE_WAIT() + + CI_USB_WRITE_WAIT(0x10000) // clobbers t0,v0, requires t9 Drive64WriteExit: jr ra @@ -102,7 +107,7 @@ DumpAndWrite: bnez v0, DumpAndWriteExit lui t0, K_BASE // delay slot - lw t1, K_64DRIVE_MAGIC(t0) + lw t1, K_CONSOLE_AVAILABLE(t0) beqz t1, DumpAndWriteExit ori a0, s0, r0 // delay slot diff --git a/inc/64drive.inc b/inc/64drive.inc index fbaa859..9f165b9 100644 --- a/inc/64drive.inc +++ b/inc/64drive.inc @@ -42,11 +42,30 @@ macro CI_WAIT() { nop } -macro CI_USB_WRITE_WAIT() { +macro CI_USB_WRITE_WAIT(timeout) { PI_WAIT() -- - lw t0, CI_USB_COMMAND_STATUS(t9) - srl t0, t0, 4 // shift out ARM status, leaving WRITE status - bnez t0,- - nop + + if {timeout} > 0 { + li v0, {timeout} + - + lw t0, CI_USB_COMMAND_STATUS(t9) + beqz v0,+ // die + srl t0, t0, 4 // shift out ARM status, leaving WRITE status + bnez t0,- + subiu v0, v0, 1 + b ++ // exit normally + or v0, r0, r0 + + + li t0, 0xF + sw t0, CI_USB_COMMAND_STATUS(t9) // force disarm + lli v0, 0xDEAD // return error code + + + + } else { + - + lw t0, CI_USB_COMMAND_STATUS(t9) + srl t0, t0, 4 // shift out ARM status, leaving WRITE status + bnez t0,- + nop + } } diff --git a/inc/main.inc b/inc/main.inc index 4620e36..3acc208 100644 --- a/inc/main.inc +++ b/inc/main.inc @@ -21,6 +21,7 @@ constant K_BADVADDR(0x0618) constant K_64DRIVE_MAGIC(0x0700) constant K_CI_BASE(0x0704) +constant K_CONSOLE_AVAILABLE(0x0708) constant K_STACK(0x0C00 - 0x10) constant K_XXD(0x0C00) // size: 0x400 (any larger and you overwrite kernel code) diff --git a/kernel.asm b/kernel.asm index 3f34ee0..c46dc9d 100644 --- a/kernel.asm +++ b/kernel.asm @@ -82,6 +82,17 @@ Drive64Confirmed: sw t1, CI_COMMAND(t9) CI_WAIT() // clobbers t0, requires t9 +Drive64CheckConsole: + // NOTE: we only check at boot, so disconnecting the console + // while running will cause a ton of lag (timeouts) until reset. + sw r0, K_CONSOLE_AVAILABLE(k0) + la a2, KConsoleConfirmed + jal Drive64WriteDirect + lli a3, KConsoleConfirmedX - KConsoleConfirmed + lli t0, 1 + beqzl v0, Drive64Done + sw t0, K_CONSOLE_AVAILABLE(k0) + Drive64Done: // delay to empty pipeline? @@ -258,7 +269,7 @@ if K_DEBUG { IHMain: // free to modify any GPR from here to IHExit macro KDumpString(str) { - lw t1, K_64DRIVE_MAGIC(k0) + lw t1, K_CONSOLE_AVAILABLE(k0) beqz t1,+ la a2, {str} jal Drive64WriteDirect @@ -372,18 +383,24 @@ ReturnFromInterrupt: include "debug.asm" -align(4) +align(8) KString0: db " ~~ Interrupt Handled ~~", 10, 0 -align(4) +align(8) KString1: db " Interrupt States:", 10, 0 -align(4) +align(8) KNewline: db 10, 0, 0, 0 dw 0, 0, 0 +align(8) +KConsoleConfirmed: + db "USB debug console detected", 10, 0 +align(16) +KConsoleConfirmedX: + align(4) nops((K_BASE << 16) + 0x10000)