Skip to content

Commit

Permalink
SD card is always treated as 2 words now, start adding SPI1
Browse files Browse the repository at this point in the history
  • Loading branch information
meepingsnesroms committed Aug 10, 2018
1 parent 5ba0af7 commit 037cca5
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 39 deletions.
4 changes: 2 additions & 2 deletions qtBuildSystem/Mu/Mu.pro
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ SOURCES += \
src/emulator.c \
src/hardwareRegisters.c \
src/memoryAccess.c \
src/sdcard.c \
src/sdCard.c \
src/sed1376.c \
src/silkscreen.c \
src/ads7846.c \
Expand All @@ -85,7 +85,7 @@ HEADERS += \
src/hardwareRegisters.h \
src/memoryAccess.h \
src/portability.h \
src/sdcard.h \
src/sdCard.h \
src/sed1376.h \
src/silkscreen.h \
mainwindow.h \
Expand Down
22 changes: 11 additions & 11 deletions src/emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "memoryAccess.h"
#include "sed1376.h"
#include "ads7846.h"
#include "sdcard.h"
#include "sdCard.h"
#include "silkscreen.h"
#include "portability.h"
#include "debug/sandbox.h"
Expand All @@ -35,7 +35,7 @@ uint8_t* palmRam;
uint8_t* palmRom;
uint8_t palmReg[REG_SIZE];
input_t palmInput;
sdcard_t palmSdCard;
sd_card_t palmSdCard;
misc_hw_t palmMisc;
uint16_t palmFramebuffer[160 * (160 + 60)];//really 160*160, the extra pixels are the silkscreened digitizer area
uint16_t* palmExtendedFramebuffer;
Expand All @@ -46,8 +46,8 @@ double palmClockMultiplier;//used by the emulator to overclock the emulated P


uint64_t (*emulatorGetSysTime)();
uint64_t* (*emulatorGetSdCardStateChunkList)(uint64_t sessionId, uint64_t stateId);//returns the bps chunkIds for a stateId in the order they need to be applied
void (*emulatorSetSdCardStateChunkList)(uint64_t sessionId, uint64_t stateId, uint64_t* data);//sets the bps chunkIds for a stateId in the order they need to be applied
uint64_t* (*emulatorGetSdCardStateChunkList)(uint64_t sessionId, uint64_t stateId);//returns the BPS chunkIds for a stateId in the order they need to be applied
void (*emulatorSetSdCardStateChunkList)(uint64_t sessionId, uint64_t stateId, uint64_t* data);//sets the BPS chunkIds for a stateId in the order they need to be applied
buffer_t (*emulatorGetSdCardChunk)(uint64_t sessionId, uint64_t chunkId);
void (*emulatorSetSdCardChunk)(uint64_t sessionId, uint64_t chunkId, buffer_t chunk);

Expand Down Expand Up @@ -146,7 +146,7 @@ void emulatorExit(){
}

void emulatorReset(){
//reset doesnt clear RAM or sdcard, all programs are stored in RAM or on sdcard
//reset doesnt clear RAM or SD card, all programs are stored in RAM or on SD card
debugLog("Reset triggered, PC:0x%08X\n", m68k_get_reg(NULL, M68K_REG_PPC));
resetHwRegisters();
resetAddressSpace();//address space must be reset after hardware registers because it is dependant on them
Expand All @@ -168,7 +168,7 @@ uint32_t emulatorSetNewSdCard(uint64_t size, uint8_t type){
return EMU_ERROR_INVALID_PARAMETER;

if(type != CARD_NONE){
palmSdCard.sessionId = emulatorGetSysTime();//completely new sdcard, reset delta state chain
palmSdCard.sessionId = emulatorGetSysTime();//completely new SD card, reset delta state chain
palmSdCard.stateId = 0x0000000000000000;//set when saving state
palmSdCard.size = size;
palmSdCard.type = type;
Expand All @@ -195,7 +195,7 @@ uint32_t emulatorSetSdCardFromImage(buffer_t image, uint8_t type){

sdCardSetFromImage(image);

palmSdCard.sessionId = emulatorGetSysTime();//completely new sdcard, reset delta state chain
palmSdCard.sessionId = emulatorGetSysTime();//completely new SD card, reset delta state chain
palmSdCard.stateId = 0x0000000000000000;//set when saving state
palmSdCard.size = image.size;
palmSdCard.type = type;
Expand Down Expand Up @@ -308,8 +308,8 @@ bool emulatorSaveState(buffer_t buffer){
memcpy(buffer.data + offset, sed1376Framebuffer, SED1376_FB_SIZE);
offset += SED1376_FB_SIZE;

//sdcard
//update sdcard struct and save sdcard data
//SD card
//update SD card struct and save SD card data
if(allSdCardCallbacksPresent() && palmSdCard.type != CARD_NONE){
palmSdCard.stateId = emulatorGetSysTime();
sdCardSaveState(palmSdCard.sessionId, palmSdCard.stateId);
Expand Down Expand Up @@ -458,7 +458,7 @@ bool emulatorLoadState(buffer_t buffer){
offset += SED1376_FB_SIZE;
sed1376RefreshLut();

//sdcard
//SD card
palmSdCard.sessionId = readStateValueUint64(buffer.data + offset);
offset += sizeof(uint64_t);
palmSdCard.stateId = readStateValueUint64(buffer.data + offset);
Expand All @@ -469,7 +469,7 @@ bool emulatorLoadState(buffer_t buffer){
offset += sizeof(uint8_t);
palmSdCard.inserted = readStateValueBool(buffer.data + offset);
offset += sizeof(uint8_t);
//update sdcard data from sdcard struct
//update SD card data from SD card struct
if(allSdCardCallbacksPresent() && palmSdCard.type != CARD_NONE)
sdCardLoadState(palmSdCard.sessionId, palmSdCard.stateId);

Expand Down
22 changes: 11 additions & 11 deletions src/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ enum{
EMU_ERROR_INVALID_PARAMETER
};

//sdcard types
//SD card types
enum{
CARD_NONE = 0,
CARD_SD,
Expand Down Expand Up @@ -98,18 +98,18 @@ typedef struct{
}input_t;

typedef struct{
//the sdcard is stored as a directory structure with an /(SESSIONID)/sdcard(STATEID).info and /(SESSIONID)/sdcard(CHUNKID).bps
//each emulator instance has its own sdcard SESSIONID directory if it has an sdcard
//sdcard(STATEID).info specifys the bps files to use
//every savestate a new bps file is created with the changes made to the sdcard since the last savestate
//the first bps is a patch over a buffer of sdcard size filled with 0x00
//the SD card is stored as a directory structure with an /(SESSIONID)/sdCard(STATEID).info and /(SESSIONID)/sdCard(CHUNKID).bps
//each emulator instance has its own SD card SESSIONID directory if it has an SD card
//sdCard(STATEID).info specifys the BPS files to use
//every savestate a new BPS file is created with the changes made to the SD card since the last savestate
//the first BPS is a patch over a buffer of SD card size filled with 0x00
//the frontend provides file access but the emulator does all the patching
uint64_t sessionId;//64 bit system time when emulator starts
uint64_t stateId;//64 bit system time when the savestate was taken
uint64_t size;
uint8_t type;
bool inserted;
}sdcard_t;
}sd_card_t;

typedef struct{
bool powerButtonLed;
Expand All @@ -135,7 +135,7 @@ extern uint8_t* palmRam;
extern uint8_t* palmRom;
extern uint8_t palmReg[];
extern input_t palmInput;
extern sdcard_t palmSdCard;
extern sd_card_t palmSdCard;
extern misc_hw_t palmMisc;
extern uint16_t palmFramebuffer[];
extern uint16_t* palmExtendedFramebuffer;
Expand All @@ -146,8 +146,8 @@ extern double palmClockMultiplier;

//callbacks
extern uint64_t (*emulatorGetSysTime)();
extern uint64_t* (*emulatorGetSdCardStateChunkList)(uint64_t sessionId, uint64_t stateId);//returns the bps chunkIds for a stateId in the order they need to be applied
extern void (*emulatorSetSdCardStateChunkList)(uint64_t sessionId, uint64_t stateId, uint64_t* data);//sets the bps chunkIds for a stateId in the order they need to be applied
extern uint64_t* (*emulatorGetSdCardStateChunkList)(uint64_t sessionId, uint64_t stateId);//returns the BPS chunkIds for a stateId in the order they need to be applied
extern void (*emulatorSetSdCardStateChunkList)(uint64_t sessionId, uint64_t stateId, uint64_t* data);//sets the BPS chunkIds for a stateId in the order they need to be applied
extern buffer_t (*emulatorGetSdCardChunk)(uint64_t sessionId, uint64_t chunkId);
extern void (*emulatorSetSdCardChunk)(uint64_t sessionId, uint64_t chunkId, buffer_t chunk);

Expand All @@ -157,7 +157,7 @@ void emulatorExit();
void emulatorReset();
void emulatorSetRtc(uint16_t days, uint8_t hours, uint8_t minutes, uint8_t seconds);
uint32_t emulatorSetNewSdCard(uint64_t size, uint8_t type);
buffer_t emulatorGetSdCardImage();//this is a direct pointer to the sdcard data, do not free it
buffer_t emulatorGetSdCardImage();//this is a direct pointer to the SD card data, do not free it
uint32_t emulatorSetSdCardFromImage(buffer_t image, uint8_t type);
uint64_t emulatorGetStateSize();
bool emulatorSaveState(buffer_t buffer);//true = success
Expand Down
6 changes: 6 additions & 0 deletions src/hardwareRegisters.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "68328Functions.h"
#include "portability.h"
#include "ads7846.h"
#include "sdCard.h"
#include "m68k/m68k.h"
#include "debug/sandbox.h"

Expand Down Expand Up @@ -507,6 +508,7 @@ uint16_t getHwRegister16(uint32_t address){
case TPRER2:
case TCTL1:
case TCTL2:
case SPICONT1:
case SPICONT2:
case SPIDATA2:
//simple read, no actions needed
Expand Down Expand Up @@ -919,6 +921,10 @@ void setHwRegister16(uint32_t address, uint16_t value){
}
break;

case SPICONT1:
setSpiCont1(value);
break;

case SPICONT2:
setSpiCont2(value);
break;
Expand Down
36 changes: 24 additions & 12 deletions src/hardwareRegistersAccessors.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,27 +236,39 @@ static inline void setIlcr(uint16_t value){
registerArrayWrite16(ILCR, newIlcr);
}

/*
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
//only master mode is implemented!!!
uint16_t oldSpiCont1 = registerArrayRead16(SPICONT1);

//do a transfer
if(value & oldSpiCont1 & 0x0200 && value & 0x0100){
//enabled and exchange set
uint8_t bitCount = (value & 0x000F) + 1;
uint16_t startBit = 1 << (bitCount - 1);
uint16_t currentTxFifoEntry = spi1TxFifo[0];
uint16_t newRxFifoEntry = 0;

//dont know what to transfer here yet
for(uint8_t bits = 0; bits < bitCount; bits++){
newRxFifoEntry |= sdCardExchangeBit(currentTxFifoEntry & startBit);
newRxFifoEntry <<= 1;
currentTxFifoEntry <<= 1;
}

//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));
//add received data to RX FIFO
if(spi1RxPosition < 8){
//not full add entry
spi1RxPosition = newRxFifoEntry;
spi1RxPosition++;
}

//unset XCH, transfers are instant since timing is not emulated
value &= 0xFEFF;
//remove used TX FIFO entry
for(uint8_t count = 0; count < 7; count++)
spi1TxFifo[count] = spi1TxFifo[count + 1];
spi1TxPosition--;
}

registerArrayWrite16(SPICONT1, value);
}
*/

static inline void setSpiCont2(uint16_t value){
//the ENABLE bit must be set before the transfer and in the transfer command
Expand Down
2 changes: 1 addition & 1 deletion src/makefile.all
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ EMU_SOURCES_C := $(EMU_PATH)/emulator.c \
$(EMU_PATH)/hardwareRegisters.c \
$(EMU_PATH)/sed1376.c \
$(EMU_PATH)/ads7846.c \
$(EMU_PATH)/sdcard.c \
$(EMU_PATH)/sdCard.c \
$(EMU_PATH)/silkscreen.c \
$(EMU_PATH)/68328Functions.c \
$(EMU_PATH)/m68k/m68kops.c \
Expand Down
10 changes: 8 additions & 2 deletions src/sdcard.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

Expand Down Expand Up @@ -101,7 +102,7 @@ uint32_t sdCardSetFromImage(buffer_t image){
}

void sdCardSaveState(uint64_t sessionId, uint64_t stateId){
//make single bps if sdcard data has changed
//make single BPS if SD card data has changed
if(memcmp(sdCardOldData, sdCardData, sdCardSize) != 0){
struct mem source;
struct mem target;
Expand Down Expand Up @@ -140,7 +141,7 @@ void sdCardSaveState(uint64_t sessionId, uint64_t stateId){
}

void sdCardLoadState(uint64_t sessionId, uint64_t stateId){
//rebuild sdcard from bps chain
//rebuild SD card from BPS chain
free(sdCardChunks);
sdCardChunks = emulatorGetSdCardStateChunkList(sessionId, stateId);
updateChunkBufferLength();
Expand All @@ -167,3 +168,8 @@ void sdCardLoadState(uint64_t sessionId, uint64_t stateId){

memcpy(sdCardOldData, sdCardData, sdCardSize);
}

bool sdCardExchangeBit(bool bit){
//not done
return true;//eek
}
3 changes: 3 additions & 0 deletions src/sdcard.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <stdint.h>
#include <stdbool.h>

#include "emulator.h"

Expand All @@ -11,3 +12,5 @@ buffer_t sdCardGetImage();
uint32_t sdCardSetFromImage(buffer_t image);
void sdCardSaveState(uint64_t sessionId, uint64_t stateId);
void sdCardLoadState(uint64_t sessionId, uint64_t stateId);

bool sdCardExchangeBit(bool bit);
2 changes: 2 additions & 0 deletions unimplementedHardware.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ port d IRQ* bits edge triggered interrupt mode is not emulated
edgeTriggeredInterruptLastValue may need to be locked when PLL is off
port d data register seems to have its data bits cleared when an edge triggered interrupt is cleared(according to MC68VZ328UM.pdf Page 10-15)
Dont know if it possible to have the backlight on and the display off at the same time(theres no reason for it but it may be possible)
SPI1 slave mode, should never be used

Debug tools:
missing home screen icons
Expand All @@ -38,6 +39,7 @@ USB chip may be swapped in to address space with a GPIO and reconfiguring CSC ch
EMUCS memory range is unemulated
the second line on all the chip selects is not properly emulated, they should be empty and trigger a bus error(except CSD1, it seems to follow different rules) but currently mirror the first half
the CSC address space is never accessed, this may be because the DRAM controller takes it over
unknown if DRAM bit expanded address space is used for CSC when its not an extension of CSD
there is conflicting information on wether the DRAM bit effects CSC, needs a test made
chipselect mirroring is wrong(CS*0 and CS*1 are considered continuous when mapped with more address space than memory, mirroring is currently [CS*0, CS*1, repeat alternating till the end of address space] it should be [CS*0, repeat CS*0 until half way through CS* address space, CS*1, repeat CS*1 until end of CS* address space])
CSC0 and CSA1 ranges overlap, something specifically stated as what not to do(the CSA1 pin is disabled though)
Expand Down

0 comments on commit 037cca5

Please sign in to comment.