From 08eea8dcd1ed35e1a35291959b4a59b99ecbe23a Mon Sep 17 00:00:00 2001 From: meepingsnesroms Date: Mon, 30 Apr 2018 11:21:22 -0700 Subject: [PATCH] Alot, ... FEATURE_RAM_HUGE is now fully hooked up. Fix port d edge triggered interrupts when PLL is off. Add unfinished SPI1 support. Variable alignment cleanups. Add more timer registers. RTC is now set on emulatorInit(), but still not called on emulatorReset() to preserve RTC through resets --- src/ads7846.c | 4 +- src/ads7846.h | 4 +- src/emulator.c | 32 +++++++++++++++ src/hardwareRegisters.c | 65 +++++++++++++----------------- src/hardwareRegisters.h | 4 ++ src/hardwareRegistersAccessors.c.h | 58 ++++++++++++++++++++++++-- unimplementedFeatures.txt | 5 ++- 7 files changed, 127 insertions(+), 45 deletions(-) diff --git a/src/ads7846.c b/src/ads7846.c index 04e9602..d6d10e6 100644 --- a/src/ads7846.c +++ b/src/ads7846.c @@ -3,8 +3,8 @@ #include "emulator.h" -uint8_t ads7846InputBitsLeft; -uint8_t ads7846ControlByte; +uint8_t ads7846InputBitsLeft; +uint8_t ads7846ControlByte; uint16_t ads7846OutputValue; diff --git a/src/ads7846.h b/src/ads7846.h index 0cd0e27..561cc7e 100644 --- a/src/ads7846.h +++ b/src/ads7846.h @@ -2,8 +2,8 @@ #include -extern uint8_t ads7846InputBitsLeft; -extern uint8_t ads7846ControlByte; +extern uint8_t ads7846InputBitsLeft; +extern uint8_t ads7846ControlByte; extern uint16_t ads7846OutputValue; void ads7846SendBit(bool bit); diff --git a/src/emulator.c b/src/emulator.c index 096ade6..e8cf2a0 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -264,6 +264,7 @@ uint32_t emulatorInit(uint8_t* palmRomDump, uint8_t* palmBootDump, uint32_t spec //config palmClockMultiplier = (specialFeatures & FEATURE_FAST_CPU) ? 2.0 : 1.0;//overclock palmSpecialFeatures = specialFeatures; + setRtc(0, 0, 0, 0); //start running m68k_pulse_reset(); @@ -334,6 +335,9 @@ uint32_t emulatorGetStateSize(){ size += sizeof(int32_t);//pllWakeWait size += sizeof(uint32_t);//clk32Counter size += sizeof(uint16_t) * 2;//timerStatusReadAcknowledge + size += sizeof(uint16_t) * 8;//RX 8 * 16 SPI1 FIFO + size += sizeof(uint16_t) * 8;//TX 8 * 16 SPI1 FIFO + size += sizeof(uint8_t) * 2;//spi1(R/T)xPosition size += sizeof(uint8_t) * 2;//ads7846 size += sizeof(uint16_t);//ads7846 size += sizeof(uint8_t) * 7;//palmMisc @@ -442,6 +446,20 @@ void emulatorSaveState(uint8_t* data){ writeStateValueUint16(data + offset, timerStatusReadAcknowledge[1]); offset += sizeof(uint16_t); + //SPI1 + for(uint8_t fifoPosition = 0; fifoPosition < 8; fifoPosition++){ + writeStateValueUint16(data + offset, spi1RxFifo[fifoPosition]); + offset += sizeof(uint16_t); + } + for(uint8_t fifoPosition = 0; fifoPosition < 8; fifoPosition++){ + writeStateValueUint16(data + offset, spi1TxFifo[fifoPosition]); + offset += sizeof(uint16_t); + } + writeStateValueUint8(data + offset, spi1RxPosition); + offset += sizeof(uint8_t); + writeStateValueUint8(data + offset, spi1TxPosition); + offset += sizeof(uint8_t); + //ADS7846 writeStateValueUint8(data + offset, ads7846InputBitsLeft); offset += sizeof(uint8_t); @@ -567,6 +585,20 @@ void emulatorLoadState(uint8_t* data){ timerStatusReadAcknowledge[1] = readStateValueUint16(data + offset); offset += sizeof(uint16_t); + //SPI1 + for(uint8_t fifoPosition = 0; fifoPosition < 8; fifoPosition++){ + spi1RxFifo[fifoPosition] = readStateValueUint16(data + offset); + offset += sizeof(uint16_t); + } + for(uint8_t fifoPosition = 0; fifoPosition < 8; fifoPosition++){ + spi1TxFifo[fifoPosition] = readStateValueUint16(data + offset); + offset += sizeof(uint16_t); + } + spi1RxPosition = readStateValueUint8(data + offset); + offset += sizeof(uint8_t); + spi1TxPosition = readStateValueUint8(data + offset); + offset += sizeof(uint8_t); + //ADS7846 ads7846InputBitsLeft = readStateValueUint8(data + offset); offset += sizeof(uint8_t); diff --git a/src/hardwareRegisters.c b/src/hardwareRegisters.c index 9bc806d..f04feee 100644 --- a/src/hardwareRegisters.c +++ b/src/hardwareRegisters.c @@ -17,6 +17,11 @@ int32_t pllWakeWait; uint32_t clk32Counter; double timerCycleCounter[2]; uint16_t timerStatusReadAcknowledge[2]; +uint16_t spi1RxFifo[8]; +uint16_t spi1TxFifo[8]; +uint8_t spi1RxPosition; +uint8_t spi1TxPosition; +//bool spi1RxFifoOverflow;//may not be needed, just dont send any data unless FIFO has a slot bool pllIsOn(); @@ -138,59 +143,49 @@ static inline void pllWakeCpuIfOff(){ static void checkInterrupts(){ uint32_t activeInterrupts = registerArrayRead32(ISR); uint16_t interruptLevelControlRegister = registerArrayRead16(ILCR); - uint16_t portDEdgeSelect = registerArrayRead16(PDIRQEG); uint8_t intLevel = 0; - bool reenablePllIfOff = false; if(activeInterrupts & INT_EMIQ){ //EMIQ - Emulator IRQ, has nothing to do with emulation, used for debugging on a dev board intLevel = 7; - reenablePllIfOff = true; } if(activeInterrupts & INT_SPI1){ uint8_t spi1IrqLevel = interruptLevelControlRegister >> 12; if(intLevel < spi1IrqLevel) intLevel = spi1IrqLevel; - reenablePllIfOff = true; } if(activeInterrupts & INT_IRQ5){ if(intLevel < 5) intLevel = 5; - reenablePllIfOff = true; } if(activeInterrupts & INT_IRQ3){ if(intLevel < 3) intLevel = 3; - reenablePllIfOff = true; } if(activeInterrupts & INT_IRQ2){ if(intLevel < 2) intLevel = 2; - reenablePllIfOff = true; } if(activeInterrupts & INT_IRQ1){ if(intLevel < 1) intLevel = 1; - reenablePllIfOff = true; } if(activeInterrupts & INT_PWM2){ uint8_t pwm2IrqLevel = (interruptLevelControlRegister >> 4) & 0x0007; if(intLevel < pwm2IrqLevel) intLevel = pwm2IrqLevel; - reenablePllIfOff = true; } if(activeInterrupts & INT_UART2){ uint8_t uart2IrqLevel = (interruptLevelControlRegister >> 8) & 0x0007; if(intLevel < uart2IrqLevel) intLevel = uart2IrqLevel; - reenablePllIfOff = true; } if(activeInterrupts & INT_TMR2){ @@ -198,58 +193,47 @@ static void checkInterrupts(){ uint8_t timer2IrqLevel = interruptLevelControlRegister & 0x0007; if(intLevel < timer2IrqLevel) intLevel = timer2IrqLevel; - reenablePllIfOff = true; } if(activeInterrupts & (INT_TMR1 | INT_PWM1 | INT_IRQ6)){ //All Fixed Level 6 Interrupts if(intLevel < 6) intLevel = 6; - reenablePllIfOff = true; } if(activeInterrupts & (INT_SPI2 | INT_UART1 | INT_WDT | INT_RTC | INT_KB | INT_RTI)){ //All Fixed Level 4 Interrupts if(intLevel < 4) intLevel = 4; - reenablePllIfOff = true; } if(activeInterrupts & INT_INT0){ //INTx, only reenable the PLL if interrupt is set to level sensitive if(intLevel < 4) intLevel = 4; - if(!(portDEdgeSelect & 0x01)) - reenablePllIfOff = true; } if(activeInterrupts & INT_INT1){ //INTx, only reenable the PLL if interrupt is set to level sensitive if(intLevel < 4) intLevel = 4; - if(!(portDEdgeSelect & 0x02)) - reenablePllIfOff = true; } if(activeInterrupts & INT_INT2){ //INTx, only reenable the PLL if interrupt is set to level sensitive if(intLevel < 4) intLevel = 4; - if(!(portDEdgeSelect & 0x04)) - reenablePllIfOff = true; } if(activeInterrupts & INT_INT3){ //INTx, only reenable the PLL if interrupt is set to level sensitive if(intLevel < 4) intLevel = 4; - if(!(portDEdgeSelect & 0x08)) - reenablePllIfOff = true; } - if(reenablePllIfOff) - pllWakeCpuIfOff(); + //some interrupts should probably be auto cleared after being run once, RTI, RTC, WATCHDOG and SPI1/2 seem like they should be cleared this way + pllWakeCpuIfOff(); m68k_set_irq(intLevel);//should be called even if intLevel is 0, that is how the interrupt state gets cleared } @@ -259,26 +243,31 @@ static void checkPortDInterrupts(){ uint8_t portDIntEnable = registerArrayRead8(PDIRQEN); uint8_t portDKeyboardEnable = registerArrayRead8(PDKBEN); uint8_t portDIrqPins = ~registerArrayRead8(PDSEL); + uint16_t portDEdgeSelect = registerArrayRead16(PDIRQEG); uint16_t interruptControlRegister = registerArrayRead16(ICR); if(portDIntEnable & portDValue & ~portDDir & 0x01){ //int 0, polarity set with PDPOL - setIprIsrBit(INT_INT0); + if(!(portDEdgeSelect & 0x01) || pllIsOn()) + setIprIsrBit(INT_INT0); } if(portDIntEnable & portDValue & ~portDDir & 0x02){ //int 1, polarity set with PDPOL - setIprIsrBit(INT_INT1); + if(!(portDEdgeSelect & 0x02) || pllIsOn()) + setIprIsrBit(INT_INT1); } if(portDIntEnable & portDValue & ~portDDir & 0x04){ //int 2, polarity set with PDPOL - setIprIsrBit(INT_INT2); + if(!(portDEdgeSelect & 0x04) || pllIsOn()) + setIprIsrBit(INT_INT2); } if(portDIntEnable & portDValue & ~portDDir & 0x08){ //int 3, polarity set with PDPOL - setIprIsrBit(INT_INT3); + if(!(portDEdgeSelect & 0x08) || pllIsOn()) + setIprIsrBit(INT_INT3); } if(portDIrqPins & ~portDDir & 0x10 && (bool)(portDValue & 0x10) == (bool)(interruptControlRegister & 0x8000)){ @@ -474,6 +463,8 @@ unsigned int getHwRegister16(unsigned int address){ case TCMP2: case TPRER1: case TPRER2: + case TCTL1: + case TCTL2: case SPICONT2: case SPIDATA2: //simple read, no actions needed @@ -706,19 +697,11 @@ void setHwRegister16(unsigned int address, unsigned int value){ break; case TSTAT1: - //preserve any unread bits - registerArrayWrite16(TSTAT1, (value & timerStatusReadAcknowledge[0]) | (registerArrayRead16(TSTAT1) & ~timerStatusReadAcknowledge[0])); - timerStatusReadAcknowledge[0] = 0x0000;//clear acknowledged reads - if(!(registerArrayRead16(TSTAT1) & 0x0001)) - clearIprIsrBit(INT_TMR1); + setTstat1(value); break; case TSTAT2: - //preserve any unread bits - registerArrayWrite16(TSTAT2, (value & timerStatusReadAcknowledge[1]) | (registerArrayRead16(TSTAT2) & ~timerStatusReadAcknowledge[1])); - timerStatusReadAcknowledge[1] = 0x0000;//clear acknowledged reads - if(!(registerArrayRead16(TSTAT2) & 0x0001)) - clearIprIsrBit(INT_TMR2); + setTstat2(value); break; case WATCHDOG: @@ -841,6 +824,9 @@ void setHwRegister16(unsigned int address, unsigned int value){ setSpiCont2(value); break; + case SPISPC: + //SPI1 timing, unemulated for now + case TCMP1: case TCMP2: case TPRER1: @@ -916,6 +902,11 @@ void resetHwRegisters(){ timerCycleCounter[1] = 0.0; timerStatusReadAcknowledge[0] = 0x0000; timerStatusReadAcknowledge[1] = 0x0000; + memset(spi1RxFifo, 0x00, 8 * sizeof(uint16_t)); + memset(spi1TxFifo, 0x00, 8 * sizeof(uint16_t)); + spi1RxPosition = 0; + spi1TxPosition = 0; + for(uint8_t chip = CHIP_BEGIN; chip < CHIP_END; chip++){ chips[chip].enable = false; chips[chip].start = 0x00000000; diff --git a/src/hardwareRegisters.h b/src/hardwareRegisters.h index e269f07..34a7d8b 100644 --- a/src/hardwareRegisters.h +++ b/src/hardwareRegisters.h @@ -60,6 +60,10 @@ extern int32_t pllWakeWait; extern uint32_t clk32Counter; extern double timerCycleCounter[]; extern uint16_t timerStatusReadAcknowledge[]; +extern uint16_t spi1RxFifo[8]; +extern uint16_t spi1TxFifo[8]; +extern uint8_t spi1RxPosition; +extern uint8_t spi1TxPosition; //memory accessors unsigned int getHwRegister8(unsigned int address); diff --git a/src/hardwareRegistersAccessors.c.h b/src/hardwareRegistersAccessors.c.h index c805b05..5665e08 100644 --- a/src/hardwareRegistersAccessors.c.h +++ b/src/hardwareRegistersAccessors.c.h @@ -71,10 +71,15 @@ static inline void setCsd(uint16_t value){ chips[CHIP_D_RAM].enable = value & 0x0001; chips[CHIP_D_RAM].readOnly = value & 0x8000; - if(csControl1 & 0x0040 && value & 0x0200) - chips[CHIP_D_RAM].size = 0x800000/*8mb*/ << (value >> 1 & 0x0001); - else + if(csControl1 & 0x0040 && value & 0x0200){ + if(palmSpecialFeatures && FEATURE_RAM_HUGE) + chips[CHIP_D_RAM].size = 0x4000000/*64mb*/ << (value >> 1 & 0x0001); + else + chips[CHIP_D_RAM].size = 0x800000/*8mb*/ << (value >> 1 & 0x0001); + } + else{ chips[CHIP_D_RAM].size = 0x8000/*32kb*/ << (value >> 1 & 0x0007); + } //attributes chips[CHIP_D_RAM].supervisorOnlyProtectedMemory = value & 0x4000; @@ -209,6 +214,26 @@ static inline void setIlcr(uint16_t value){ newIlcr |= oldIlcr & 0x0007; } +static inline void setSpiCont1(uint16_t value){ + //unsure if ENABLE can be set at the exact moment of write or must be set before write, currently allow both + //important bits are ENABLE, XCH, IRQ, IRQEN and BITCOUNT + //uint16_t oldSpiCont1 = registerArrayRead16(SPICONT1); + if(value & 0x0400 && value & 0x0200 && value & 0x0100){ + //master mode, enabled and exchange set + uint8_t bitCount = (value & 0x000F) + 1; + + //dont know what to transfer here yet + + //debugLog("SPI2 transfer, ENABLE:%s, XCH:%s, IRQ:%s, IRQEN:%s, BITCOUNT:%d\n", boolString(value & 0x0200), boolString(value & 0x0100), boolString(value & 0x0080), boolString(value & 0x0400), (value & 0x000F) + 1); + //debugLog("SPI2 transfer, shifted in:0x%04X, shifted out:0x%04X\n", spi2Data << (16 - bitCount) >> bitCount, oldSpiCont2 >> (16 - bitCount)); + + //unset XCH, transfers are instant since timing is not emulated + value &= 0xFEFF; + } + + registerArrayWrite16(SPICONT1, value); +} + static inline void setSpiCont2(uint16_t value){ //unsure if ENABLE can be set at the exact moment of write or must be set before write, currently allow both //important bits are ENABLE, XCH, IRQ, IRQEN and BITCOUNT @@ -242,6 +267,28 @@ static inline void setSpiCont2(uint16_t value){ registerArrayWrite16(SPICONT2, value & 0xE3FF); } +static inline void setTstat1(uint16_t value){ + uint16_t oldTstat1 = registerArrayRead16(TSTAT1); + //preserve any unread bits + registerArrayWrite16(TSTAT1, (value & timerStatusReadAcknowledge[0]) | (oldTstat1 & ~timerStatusReadAcknowledge[0])); + if(!(value & 0x0001) && (oldTstat1 & timerStatusReadAcknowledge[0] & 0x0001)){ + clearIprIsrBit(INT_TMR1); + checkInterrupts(); + } + timerStatusReadAcknowledge[0] = 0x0000;//clear acknowledged reads +} + +static inline void setTstat2(uint16_t value){ + uint16_t oldTstat2 = registerArrayRead16(TSTAT2); + //preserve any unread bits + registerArrayWrite16(TSTAT2, (value & timerStatusReadAcknowledge[1]) | (oldTstat2 & ~timerStatusReadAcknowledge[1])); + if(!(value & 0x0001) && (oldTstat2 & timerStatusReadAcknowledge[1] & 0x0001)){ + clearIprIsrBit(INT_TMR2); + checkInterrupts(); + } + timerStatusReadAcknowledge[1] = 0x0000;//clear acknowledged reads +} + //register getters static inline uint8_t getPortDValue(){ uint8_t requestedRow = registerArrayRead8(PKDIR) & registerArrayRead8(PKDATA);//keys are requested on port k and read on port d @@ -292,3 +339,8 @@ static inline uint8_t getPortKValue(){ return portKValue; } + +static inline uint16_t getSpiTest(){ + //SSTATUS is unemulated because the datasheet has no descrption of how it works + return spi1RxPosition << 4 | spi1TxPosition; +} diff --git a/unimplementedFeatures.txt b/unimplementedFeatures.txt index f0c10a2..f123cd5 100644 --- a/unimplementedFeatures.txt +++ b/unimplementedFeatures.txt @@ -12,7 +12,7 @@ Timer capture events, I don't think there possible though with the Palms hardwar Peripheral Control Register, audio ppm mixing selection and timer cascade mode All edge triggered interrupts(currently always level triggered) Port G SPI stuff -Setting the battery dead bit(PDDATA 0x80) if the emulated battery percent gets too low +Setting the battery dead bit(PDDATA 0x80) if the emulated battery percent gets too low(don't know how low this is) DRAMC write enable All of DRAMMC pen input @@ -21,6 +21,9 @@ interrupt control register (ICR), POL5 and edge trigger selects ADC7846 temperature sensors ADC7846 dock sensor some undocumented I/O might be on port e and port j +if a timer triggers more than once per CLK32 it will lose any triggers after the first and get out of sync +SPI1, probably the sdcard since sdcards use SPI for data transfer +SPITEST SSTATUS is undocumented and therefore unemulated Debug tools: missing icons