6502 emulator for Commodore 64 ROMs, serial terminal edition for MBED. Recommend terminal echo on, line edit on, caps lock, 115200bps, implicit carriage return on newline, currently non-buffered so don't paste lots of stuff

More details at:

[https://github.com/davervw] [https://techwithdave.davevw.com/2020/03/simple-emu-c64.html]

Revision:
4:0461c100cbbb
Parent:
3:f978776f810c
Child:
6:0df1257f8cc1
--- a/emuc64.cpp	Fri Apr 10 04:32:54 2020 +0000
+++ b/emuc64.cpp	Sun Apr 12 19:26:44 2020 +0000
@@ -11,7 +11,6 @@
 // davevw.com
 //
 // Permission is hereby granted, free of charge, to any person obtaining a copy
-//
 // of this software and associated documentation files (the "Software"), to deal
 // in the Software without restriction, including without limitation the rights
 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -64,7 +63,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 
 #include <mbed.h>
-#include <LocalFileSystem.h>
+//#include <LocalFileSystem.h>
 #include "emu6502.h"
 #include "cbmconsole.h"
 
@@ -75,8 +74,7 @@
 char* StartupPRG = 0;
 
 // locals
-static int startup_state = 0;
-
+//static int startup_state = 0;
 //LocalFileSystem local("local");
 
 //static void File_ReadAllBytes(byte* bytes, unsigned int size, const char* filename)
@@ -92,44 +90,44 @@
 //	close(file);
 //}
 
-// returns true if BASIC
-static bool LoadPRG(const char* filename)
-{
-	bool result;
-	byte lo, hi;
-	int file;
-	ushort loadaddr;
-	
-	file = open(filename, O_RDONLY);
-	if (file < 0
-		|| read(file, &lo, 1) != 1
-		|| read(file, &hi, 1) != 1
-		)
-	{
-		pc.printf("file ""%s"", errno=%d\n", filename, errno);
-		exit(1);
-	}
-	if (lo == 1)
-	{
-		loadaddr = (ushort)(GetMemory(43) | (GetMemory(44) << 8));
-		result = true;
-	}
-	else
-	{
-		loadaddr = (ushort)(lo | (hi << 8));
-		result = false;
-	}
-	while (true)
-	{
-		byte value;
-		if (read(file, &value, 1) == 1)
-			SetMemory(loadaddr++, value);
-		else
-			break;
-	}
-	close(file);
-	return result;
-}
+//// returns true if BASIC
+//static bool LoadPRG(const char* filename)
+//{
+//	bool result;
+//	byte lo, hi;
+//	int file;
+//	ushort loadaddr;
+//	
+//	file = open(filename, O_RDONLY);
+//	if (file < 0
+//		|| read(file, &lo, 1) != 1
+//		|| read(file, &hi, 1) != 1
+//		)
+//	{
+//		pc.printf("file ""%s"", errno=%d\n", filename, errno);
+//		exit(1);
+//	}
+//	if (lo == 1)
+//	{
+//		loadaddr = (ushort)(GetMemory(43) | (GetMemory(44) << 8));
+//		result = true;
+//	}
+//	else
+//	{
+//		loadaddr = (ushort)(lo | (hi << 8));
+//		result = false;
+//	}
+//	while (true)
+//	{
+//		byte value;
+//		if (read(file, &value, 1) == 1)
+//			SetMemory(loadaddr++, value);
+//		else
+//			break;
+//	}
+//	close(file);
+//	return result;
+//}
 
 bool ExecutePatch(void)
 {
@@ -154,75 +152,75 @@
 
 		return true; // overriden, and PC changed, so caller should reloop before execution to allow breakpoint/trace/ExecutePatch/etc.
 	}
-	else if (PC == 0xA474) // READY
-	{
-		if (StartupPRG != 0 && strlen(StartupPRG) > 0) // User requested program be loaded at startup
-		{
-			const char* filename = StartupPRG;
-			StartupPRG = 0;
-
-			if (LoadPRG(filename))
-			{
-				//UNNEW that I used in late 1980s, should work well for loang a program too, probably gleaned from BASIC ROM
-				//ldy #0
-				//lda #1
-				//sta(43),y
-				//iny
-				//sta(43),y
-				//jsr $a533 ; LINKPRG
-				//clc
-				//lda $22
-				//adc #2
-				//sta 45
-				//lda $23
-				//adc #0
-				//sta 46
-				//lda #0
-				//jsr $a65e ; CLEAR/CLR
-				//jmp $a474 ; READY
-
-				// This part shouldn't be necessary as we have loaded, not recovering from NEW, bytes should still be there
-				ushort addr = (ushort)(GetMemory(43) | (GetMemory(44) << 8));
-				SetMemory(addr, 1);
-				SetMemory((ushort)(addr + 1), 1);
-
-				// JSR equivalent
-				ushort retaddr = (ushort)(PC - 1);
-				Push(HI(retaddr));
-				Push(LO(retaddr));
-				PC = 0xA533; // LINKPRG
-
-				startup_state = 1; // should be able to regain control when returns...
-
-				return true; // overriden, and PC changed, so caller should reloop before execution to allow breakpoint/trace/ExecutePatch/etc.
-			}
-		}
-		else if (startup_state == 1)
-		{
-			ushort addr = (ushort)(GetMemory(0x22) | (GetMemory(0x23) << 8) + 2);
-			SetMemory(45, (byte)addr);
-			SetMemory(46, (byte)(addr >> 8));
-
-			// JSR equivalent
-			ushort retaddr = (ushort)(PC - 1);
-			Push(HI(retaddr));
-			Push(LO(retaddr));
-			PC = 0xA65E; // CLEAR/CLR
-			A = 0;
-
-			startup_state = 2; // should be able to regain control when returns...
-
-			return true; // overriden, and PC changed, so caller should reloop before execution to allow breakpoint/trace/ExecutePatch/etc.
-		}
-		else if (startup_state == 2)
-		{
-			CBM_Console_Push("RUN\r");
-			PC = 0xA47B; // skip READY message, but still set direct mode, and continue to MAIN
-			C = false;
-			startup_state = 0;
-			return true; // overriden, and PC changed, so caller should reloop before execution to allow breakpoint/trace/ExecutePatch/etc.
-		}
-	}
+//	else if (PC == 0xA474) // READY
+//	{
+//		if (StartupPRG != 0 && strlen(StartupPRG) > 0) // User requested program be loaded at startup
+//		{
+//			const char* filename = StartupPRG;
+//			StartupPRG = 0;
+//
+//			if (LoadPRG(filename))
+//			{
+//				//UNNEW that I used in late 1980s, should work well for loang a program too, probably gleaned from BASIC ROM
+//				//ldy #0
+//				//lda #1
+//				//sta(43),y
+//				//iny
+//				//sta(43),y
+//				//jsr $a533 ; LINKPRG
+//				//clc
+//				//lda $22
+//				//adc #2
+//				//sta 45
+//				//lda $23
+//				//adc #0
+//				//sta 46
+//				//lda #0
+//				//jsr $a65e ; CLEAR/CLR
+//				//jmp $a474 ; READY
+//
+//				// This part shouldn't be necessary as we have loaded, not recovering from NEW, bytes should still be there
+//				ushort addr = (ushort)(GetMemory(43) | (GetMemory(44) << 8));
+//				SetMemory(addr, 1);
+//				SetMemory((ushort)(addr + 1), 1);
+//
+//				// JSR equivalent
+//				ushort retaddr = (ushort)(PC - 1);
+//				Push(HI(retaddr));
+//				Push(LO(retaddr));
+//				PC = 0xA533; // LINKPRG
+//
+//				startup_state = 1; // should be able to regain control when returns...
+//
+//				return true; // overriden, and PC changed, so caller should reloop before execution to allow breakpoint/trace/ExecutePatch/etc.
+//			}
+//		}
+//		else if (startup_state == 1)
+//		{
+//			ushort addr = (ushort)(GetMemory(0x22) | (GetMemory(0x23) << 8) + 2);
+//			SetMemory(45, (byte)addr);
+//			SetMemory(46, (byte)(addr >> 8));
+//
+//			// JSR equivalent
+//			ushort retaddr = (ushort)(PC - 1);
+//			Push(HI(retaddr));
+//			Push(LO(retaddr));
+//			PC = 0xA65E; // CLEAR/CLR
+//			A = 0;
+//
+//			startup_state = 2; // should be able to regain control when returns...
+//
+//			return true; // overriden, and PC changed, so caller should reloop before execution to allow breakpoint/trace/ExecutePatch/etc.
+//		}
+//		else if (startup_state == 2)
+//		{
+//			CBM_Console_Push("RUN\r");
+//			PC = 0xA47B; // skip READY message, but still set direct mode, and continue to MAIN
+//			C = false;
+//			startup_state = 0;
+//			return true; // overriden, and PC changed, so caller should reloop before execution to allow breakpoint/trace/ExecutePatch/etc.
+//		}
+//	}
 	return false; // execute normally
 }