Skip to content

Commit

Permalink
Added checksum program, first draft
Browse files Browse the repository at this point in the history
Fixed VERA bugs
Optimized flow
Optimized string allocation
Tried to create some re-use functions
Optimized data segment allocation issues. Modified kickc to solve string consolidation issues over data segments.
  • Loading branch information
FlightControl-User committed Oct 18, 2023
1 parent cd426df commit 5854a79
Show file tree
Hide file tree
Showing 22 changed files with 14,826 additions and 8,331 deletions.
153 changes: 153 additions & 0 deletions src/cx16-checksum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/**
* @file cx16-checksum.c
*
* @author Sven Van de Velde from CX16 community (https://github.com/FlightControl-User) -- Creation of this program.
*
* @brief COMMANDER X16 UPDATE TOOL CHECKSUM VALIDATOR
*
* @version 3.0
* @date 2023-10-15
*
* @copyright Copyright (c) 2023
*
*/

// Ensures the proper character set is used for the COMMANDER X16.
#pragma encoding(screencode_mixed)

// Uses all parameters to be passed using zero pages (fast).
#pragma var_model(zp)


#include <6502.h>
#include <cx16.h>
#include <cx16-conio.h>
#include <kernal.h>
#include <printf.h>
#include <sprintf.h>
#include <stdio.h>
#include "cx16-vera.h"
#include "cx16-veralib.h"

__mem unsigned long rom_file_checksum = 0;
__mem unsigned long rom_file_size = 0;
__mem unsigned long rom_checksum = 0;




unsigned char* rom_file(unsigned char rom_chip) {
static char* file_rom_cx16 = "ROM.BIN";
static char* file_rom_card = "ROMn.BIN";
if(rom_chip) {
file_rom_card[3] = '0'+rom_chip;
return file_rom_card;
} else {
return file_rom_cx16;
}
return NULL;
}

void rom_calc_file_checksum(unsigned char rom_chip) {

// We start for ROM from 0x0:0x7800 !!!!
bram_bank_t rom_bram_bank = 0;
bram_ptr_t rom_bram_ptr = (bram_ptr_t)0x0400;
bank_set_bram(rom_bram_bank);

unsigned long rom_address = 0;

unsigned int rom_row_current = 0;
unsigned char rom_release;
unsigned char rom_github[6];

rom_file_size = 0;
rom_file_checksum = 0;

unsigned char* file = "ROM-R45.BIN";
printf("Opening %s from SD card ...\n", file);


FILE *fp = fopen(file, "r");
if (fp) {

while (rom_file_size < 0x800000) {

rom_bram_ptr = (char*)0x400;
unsigned int rom_package_read = fgets(rom_bram_ptr, 128, fp); // this will load b bytes from the rom.bin file or less if EOF is reached.
if (!rom_package_read) {
break;
}

for(unsigned int b=0; b<rom_package_read; b++) {
rom_file_checksum += rom_bram_ptr[b];
}
rom_file_size += rom_package_read;

}
fclose(fp);
}

cbm_k_clrchn();

return;
}

void rom_calc_rom_checksum() {

SEI();
unsigned char bank_brom = 0;
unsigned char* byte_brom = (char*)0xC000;
unsigned long rom_addr = 0;

while(rom_addr < rom_file_size) {

bank_set_brom(bank_brom);
rom_checksum += *byte_brom;
rom_addr++;
byte_brom++;

if(byte_brom == 0x0) {
byte_brom = (char*)0xC000;
bank_brom++;
}
}

bank_set_brom(4);
CLI();

return;
}

void main() {

cx16_k_screen_set_mode(0); // Default 80 columns mode.
screenlayer1(); // Reset the screen layer values for conio.
cx16_k_screen_set_charset(3, (char *)0); // Lower case characters.
vera_display_set_hstart(11); // Set border.
vera_display_set_hstop(147); // Set border.
vera_display_set_vstart(19); // Set border.
vera_display_set_vstop(219); // Set border.
vera_sprites_hide(); // Hide sprites.
vera_layer0_hide(); // Layer 0 deactivated.
vera_layer1_show(); // Layer 1 is the current text canvas.
textcolor(WHITE); // Default text color is white.
bgcolor(BLUE); // With a blue background.
clrscr();

printf("\n\n\n\nCommander X16 checksum calculator and validator of .BIN files.\n\n");

rom_calc_file_checksum(1);

printf("ROM-R45.BIN size : %x\n", rom_file_size);
printf("\nROM-R45.BIN checksum: %x\n", rom_file_checksum);

rom_calc_rom_checksum();

printf("ROM checksum: %x\n", rom_checksum);


bank_set_bram(0);
bank_set_brom(4);

}
1 change: 1 addition & 0 deletions src/cx16-checksum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void rom_calc_file_checksum(unsigned char rom_chip);
4 changes: 2 additions & 2 deletions src/cx16-defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@

#define __INTRO

// #define __VERA_CHIP_PROCESS
#define __VERA_CHIP_PROCESS
#define __SMC_CHIP_PROCESS
#define __ROM_CHIP_PROCESS


#define __VERA_JP1_DETECT
// #define __VERA_FLASH
#define __VERA_FLASH
#define __SMC_FLASH
#define __ROM_FLASH

Expand Down
15 changes: 8 additions & 7 deletions src/cx16-display-text.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*
*/

#pragma code_seg(CodeIntro)
#pragma data_seg(DataIntro)

const char display_intro_briefing_count = 15;
Expand Down Expand Up @@ -57,8 +56,8 @@ const char* display_into_colors_text[16] = {
"Errors can indicate J1 jumpers are not closed!"
};

//#pragma code_seg(CodeVera)
//#pragma data_seg(DataVera)
#pragma data_seg(DataVera)

const char display_jp1_spi_vera_count = 16;
const char* display_jp1_spi_vera_text[16] = {
"The following steps are IMPORTANT to update the VERA:",
Expand All @@ -79,8 +78,6 @@ const char* display_jp1_spi_vera_text[16] = {
"The program continues once the JP1 pins are opened/closed.",
};

#pragma code_seg(Code)
#pragma data_seg(Data)
const char display_no_valid_smc_bootloader_count = 9;
const char* display_no_valid_smc_bootloader_text[9] = {
"The SMC chip in your CX16 doesn't have a valid bootloader.",
Expand Down Expand Up @@ -118,8 +115,12 @@ const char* display_smc_unsupported_rom_text[7] = {
};


const char display_debriefing_count_smc = 14;
const char* display_debriefing_text_smc[14] = {
#pragma data_seg(Data)

const char* TEXT_PROGRESS_FLASHING = "Flashing ... (-) equal, (+) flashed, (!) error.";

const char display_debriefing_smc_count = 14;
const char* display_debriefing_smc_text[14] = {
"Your CX16 system has been successfully updated!",
"",
"DON'T DO ANYTHING UNTIL COUNTDOWN FINISHES!",
Expand Down
7 changes: 6 additions & 1 deletion src/cx16-display.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ void display_info_vera(unsigned char info_status, unsigned char* info_text) {
status_vera = info_status;
display_vera_led(status_color[info_status]);
gotoxy(INFO_X, INFO_Y+1);
printf("VERA %-9s SPI %0x %0x %0x ", status_text[info_status], spi_manufacturer, spi_memory_type, spi_memory_capacity);
printf("VERA %-9s W25Q16", status_text[info_status]);
if(info_text) {
gotoxy(INFO_X+64-28, INFO_Y+1);
printf("%-25s", info_text);
Expand Down Expand Up @@ -596,3 +596,8 @@ void display_action_text_flashed(unsigned long bytes, unsigned char* chip) {
sprintf(info_text, "Flashed %u bytes from RAM -> %s ... ", bytes, chip);
display_action_text(info_text);
}

unsigned char* get_info_text_flashing(unsigned long flash_bytes) {
sprintf(info_text, "%u bytes flashed", flash_bytes);
return info_text;
}
2 changes: 2 additions & 0 deletions src/cx16-display.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ void display_info_cx16_rom(unsigned char info_status, unsigned char* info_text);
void display_action_text_flashing(unsigned long bytes, unsigned char* chip, bram_bank_t bram_bank, bram_ptr_t bram_ptr, unsigned long address);
void display_action_text_flashed(unsigned long bytes, unsigned char* chip);
void display_action_text_reading(unsigned char* action, unsigned char* file, unsigned long bytes, unsigned long size, bram_bank_t bram_bank, bram_ptr_t bram_ptr);

unsigned char* get_info_text_flashing(unsigned long flash_bytes);
2 changes: 1 addition & 1 deletion src/cx16-globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
cx16_k_screen_mode_t screen_mode;

char file[32];
char info_text[256];
char info_text[80];


const char STATUS_NONE = 0;
Expand Down
8 changes: 7 additions & 1 deletion src/cx16-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include "cx16-init.h"

#pragma code_seg(CodeIntro)

void init() {
display_frame_init_64(); // ST1 | Reset canvas to 64 columns
display_frame_draw();
Expand All @@ -44,4 +46,8 @@ void init() {
display_info_rom(rom_chip, STATUS_NONE, NULL);
}

}
}

#pragma code_seg(Code)
#pragma data_seg(Data)

16 changes: 9 additions & 7 deletions src/cx16-rom.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "cx16-globals.h"
#include "cx16-utils.h"
#include "cx16-display.h"
#include "cx16-display-text.h"
#include "cx16-rom.h"

// Globals
Expand Down Expand Up @@ -513,7 +514,7 @@ unsigned long rom_verify(
unsigned int progress_row_current = 0;
unsigned long rom_different_bytes = 0;

display_info_rom(rom_chip, STATUS_COMPARING, "Comparing ...");
display_info_rom(rom_chip, STATUS_COMPARING, "");

gotoxy(x, y);

Expand Down Expand Up @@ -607,17 +608,16 @@ unsigned long rom_flash(
bank_set_bram(bram_bank_sector);

// Now we compare the RAM with the actual ROM contents.
display_action_progress("Flashing ... (-) equal, (+) flashed, (!) error.");
display_action_progress(TEXT_PROGRESS_FLASHING);

unsigned long rom_address_sector = rom_address_from_bank(rom_bank_start);
unsigned long rom_boundary = rom_address_sector + file_size;

unsigned int progress_row_current = 0;
unsigned long rom_flash_errors = 0;

display_info_rom(rom_chip, STATUS_FLASHING, "Flashing ...");

unsigned long flash_errors = 0;
unsigned long flash_bytes = 0;

while (rom_address_sector < rom_boundary) {

Expand All @@ -628,6 +628,7 @@ unsigned long rom_flash(
if (equal_bytes != ROM_SECTOR) {

unsigned int flash_errors_sector = 0;
unsigned int flash_bytes_sector = 0;
unsigned char retries = 0;

do {
Expand Down Expand Up @@ -667,6 +668,7 @@ unsigned long rom_flash(
}
ram_address += ROM_PROGRESS_CELL;
rom_address += ROM_PROGRESS_CELL;
flash_bytes_sector += ROM_PROGRESS_CELL;

x++; // This should never exceed the 64 char boundary.
}
Expand All @@ -676,6 +678,7 @@ unsigned long rom_flash(
} while (flash_errors_sector && retries <= 3);

flash_errors += flash_errors_sector;
flash_bytes += flash_bytes_sector;

} else {
cputsxy(x_sector, y_sector, "--------");
Expand All @@ -701,12 +704,11 @@ unsigned long rom_flash(
y_sector++;
}

sprintf(info_text, "%u flash errors ...", flash_errors);
display_info_rom(rom_chip, STATUS_FLASHING, info_text);
display_info_rom(rom_chip, STATUS_FLASHING, get_info_text_flashing(flash_bytes));
}

display_action_text_flashed(rom_address_sector, "ROM");
wait_moment(32);
wait_moment(16);

return flash_errors;
}
Expand Down
18 changes: 10 additions & 8 deletions src/cx16-smc.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ unsigned int smc_flash(unsigned int smc_bytes_total) {
textcolor(WHITE);
gotoxy(x, y);

unsigned int smc_bytes_flashed = 0;
unsigned int smc_flashed_bytes = 0;
unsigned int smc_attempts_total = 0;

while(smc_bytes_flashed < smc_bytes_total) {
while(smc_flashed_bytes < smc_bytes_total) {

unsigned char smc_attempts_flashed = 0;
unsigned char smc_package_committed = 0;
Expand All @@ -295,7 +295,7 @@ unsigned int smc_flash(unsigned int smc_bytes_total) {
unsigned char smc_bytes_checksum = 0;
unsigned int smc_package_flashed = 0;

display_action_text_flashing(8, "SMC", smc_bram_bank, smc_bram_ptr, smc_bytes_flashed);
display_action_text_flashing(8, "SMC", smc_bram_bank, smc_bram_ptr, smc_flashed_bytes);

while(smc_package_flashed < SMC_PROGRESS_CELL) {
unsigned char smc_byte_upload = *smc_bram_ptr;
Expand All @@ -319,7 +319,7 @@ unsigned int smc_flash(unsigned int smc_bytes_total) {

cputc('+');

smc_bytes_flashed += SMC_PROGRESS_CELL;
smc_flashed_bytes += SMC_PROGRESS_CELL;
smc_row_bytes += SMC_PROGRESS_CELL;
smc_attempts_total += smc_attempts_flashed;

Expand All @@ -330,15 +330,17 @@ unsigned int smc_flash(unsigned int smc_bytes_total) {
}
}
if(smc_attempts_flashed >= 10) {
sprintf(info_text, "There were too many attempts trying to flash the SMC at location %04x", smc_bytes_flashed);
sprintf(info_text, "There were too many attempts trying to flash the SMC at location %04x", smc_flashed_bytes);
display_action_text(info_text);
return (unsigned int)0xFFFF;
}
}

display_action_text_flashed(smc_bytes_flashed, "SMC");
display_info_smc(STATUS_FLASHING, get_info_text_flashing(smc_flashed_bytes));
}
display_action_text_flashed(smc_flashed_bytes, "SMC");
wait_moment(16);

return smc_bytes_flashed;
return smc_flashed_bytes;
}


Loading

0 comments on commit 5854a79

Please sign in to comment.