AppleIISd/Software/src/Flasher.c

134 lines
2.7 KiB
C
Raw Normal View History

2019-02-25 23:42:47 +00:00
#include "AppleIISd.h"
2019-02-24 10:24:04 +00:00
#include <stdio.h>
#include <errno.h>
#include <conio.h>
#include <apple2enh.h>
2019-02-25 23:42:47 +00:00
typedef enum
{
STATE_0 = 0x7C, // pipe
STATE_1 = 0x2F, // slash
STATE_2 = 0x2D, // hyphen
STATE_3 = 0x5C, // backslash
STATE_LAST // don't use
} STATE_CURSOR_T;
2019-03-04 21:00:36 +00:00
void writeChip(const uint8* pSource, uint8* pDest, uint16 length);
void printStatus(uint8 percentage);
2019-02-25 23:42:47 +00:00
// Binary can't be larger than 2k
2019-03-04 21:00:36 +00:00
uint8 buffer[2048] = { 0 };
2019-02-25 23:42:47 +00:00
2019-02-24 10:24:04 +00:00
int main()
{
FILE* pFile;
char slotNum;
2019-02-25 23:42:47 +00:00
APPLE_II_SD_T* pAIISD = (APPLE_II_SD_T*)SLOT_IO_START;
2019-03-04 21:00:36 +00:00
uint8* pSlotRom = SLOT_ROM_START;
uint8* pExtRom = EXT_ROM_START;
2019-02-25 23:42:47 +00:00
2019-02-24 10:24:04 +00:00
videomode(VIDEOMODE_80COL);
2019-02-25 23:42:47 +00:00
clrscr();
2019-02-24 10:24:04 +00:00
cprintf("AppleIISd firmware flasher\r");
cprintf("(c) 2019 Florian Reitz\r\r");
// ask for slot
cursor(1); // enable blinking cursor
cprintf("Slot number (1-7): ");
slotNum = cgetc();
cursor(0); // disable blinking cursor
// check if slot is valid
if((slotNum < 1) || (slotNum > 7))
{
cprintf("Invalid slot number!");
return 1; // failure
}
2019-03-04 21:00:36 +00:00
((uint8*)pAIISD) += slotNum << 4;
2019-02-25 23:42:47 +00:00
pSlotRom += slotNum << 8;
2019-02-24 10:24:04 +00:00
// open file
pFile = fopen("AppleIISd.bin", "rb");
if(pFile)
{
// read buffer
2019-03-04 21:00:36 +00:00
uint16 fileSize = fread(buffer, 1, sizeof(buffer), pFile);
fclose(pFile);
pFile = NULL;
2019-02-24 10:24:04 +00:00
2019-03-04 21:00:36 +00:00
if(fileSize == 2048)
{
2019-02-24 10:24:04 +00:00
// enable write
2019-02-25 23:42:47 +00:00
pAIISD->status.pgmen = 1;
2019-02-24 10:24:04 +00:00
// clear 0xCFFF
2019-02-25 23:42:47 +00:00
*CFFF = 0;
2019-02-24 10:24:04 +00:00
// write to SLOTROM
2019-02-25 23:42:47 +00:00
cprintf("\r\rFlashing SLOTROM: ");
writeChip(buffer, pSlotRom, 256);
2019-02-24 10:24:04 +00:00
// write to EXTROM
2019-02-25 23:42:47 +00:00
cprintf("\r\rFlashing EXTROM: ");
writeChip(buffer + 256, pExtRom, fileSize - 256);
// zero rest of chip
if(fileSize < 2048)
2019-03-04 21:00:36 +00:00
}
else
2019-02-25 23:42:47 +00:00
{
2019-03-04 21:00:36 +00:00
cprintf("\r\nWrong file size: %d\r\n", fileSize);
return 1;
2019-02-25 23:42:47 +00:00
}
2019-02-24 10:24:04 +00:00
}
else
{
cprintf("Can't open binary file: %d\r", errno);
return 1;
}
return 0; // success
}
2019-03-04 21:00:36 +00:00
void writeChip(const uint8* pSource, uint8* pDest, uint16 length)
2019-02-25 23:42:47 +00:00
{
2019-03-04 21:00:36 +00:00
uint32 i;
2019-02-25 23:42:47 +00:00
for(i=0; i<length; i++)
{
if(pSource)
{
*pDest = pSource[i];
}
else
{
// erase if no source
*pDest = 0;
}
printStatus(i * 100 / length);
pDest++;
}
}
2019-03-04 21:00:36 +00:00
void printStatus(uint8 percentage)
2019-02-25 23:42:47 +00:00
{
static STATE_CURSOR_T state = STATE_0;
2019-03-04 21:00:36 +00:00
uint8 wait = 0;
2019-02-25 23:42:47 +00:00
2019-03-04 21:00:36 +00:00
uint8 x = wherex();
2019-02-25 23:42:47 +00:00
cprintf("% 2hhu %c", percentage, (char)state);
gotox(x);
2019-02-24 10:24:04 +00:00
2019-02-25 23:42:47 +00:00
state++;
if(state == STATE_LAST)
{
state = STATE_0;
}
}