Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mods to system for Sweet16 emulation in parallel #474

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ emulator/neo
emulator/src/core/hardware.o
emulator/src/core/sys_debugger.o
emulator/src/core/sys_processor.o
emulator/src/core/processors/*.o
examples/basic-examples
examples/samples.zip
firmware/build/
Expand Down
40 changes: 34 additions & 6 deletions basic/test.bsc
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
cls
mouse show
repeat
b = mouse(x,y,w)
print chr$(20);x,y,w;" "
until false
'
' Sweet 16 test program ; about 6.7 MIPS.
'
mem = alloc(512):reg = alloc(32):stack = alloc(16)
count = 50
print mem
for pass = 0 to 1
o = pass*2:p = mem
set 1,count
set 2,0
set 5,0
.l1
add 3
dcr 2
bnz l1
inr 5
dcr 1
bnz l1
rtn
next

reg[15] = mem:reg[12] = stack

t1 = time()
sweet reg
elapsed = time()-t1
print reg[5],reg[15]
if elapsed <> 0
print elapsed
elapsed = elapsed / 100 / 3 / 65535 / count
ips = 1 / elapsed
mips = ips / 1000000
print mips
endif
Binary file added documents/release/tank.odt
Binary file not shown.
3 changes: 2 additions & 1 deletion emulator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ COMMONSOURCES = $(wildcard $(IMPSRC)*.cpp)
COMMONOBJECTS = $(subst .cpp,.o,$(COMMONSOURCES))

SOURCES = src$(S)core$(S)sys_processor.o src$(S)core$(S)hardware.o src$(S)framework$(S)beeper.o \
src$(S)framework$(S)main.o src$(S)framework$(S)gfx.o src$(S)framework$(S)debugger.o \
src$(S)framework$(S)main.o src$(S)framework$(S)gfx.o src$(S)framework$(S)debugger.o \
src$(S)core$(S)sys_debugger.o \
src$(S)core$(S)processors$(S)6502.o \
$(COMMONOBJECTS)

CC = g++
Expand Down
1 change: 1 addition & 0 deletions emulator/cross-compile/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ COMMONSOURCES = $(wildcard $(IMPSRC)*.cpp)

SOURCES = $(FRASRC)main.cpp $(FRASRC)gfx.cpp $(FRASRC)debugger.cpp $(FRASRC)beeper.cpp \
$(EMUSRC)core$(S)sys_processor.cpp $(EMUSRC)core$(S)sys_debugger.cpp $(EMUSRC)core$(S)hardware.cpp \
$(EMUSRC)core$(S)processors$(S)6502.cpp \
$(COMMONSOURCES)

INCLUDES= -I ../include -I ../framework -I .. -I $(COMDIR)include
Expand Down
4 changes: 3 additions & 1 deletion emulator/include/sys_debug_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#define WIN_BACKCOLOUR 0x004

// *******************************************************************************************************************************
//
// These functions need to be implemented by the dependent debugger.
//
// *******************************************************************************************************************************

#define DEBUG_ARGUMENTS(ac,av) { CPUSaveArguments(ac,av); }
Expand All @@ -32,7 +34,7 @@
#define DEBUG_VDURENDER(x) DBGXRender(x,1) // Render the game display etc.

#define DEBUG_RESET() CPUReset() // Reset the CPU / Hardware.
#define DEBUG_HOMEPC() ((CPUGetStatus()->pc) & 0xFFFF) // Get PC Home Address (e.g. current PCTR value)
#define DEBUG_HOMEPC() (CPUGetPC() & 0xFFFF) // Get PC Home Address (e.g. current PCTR value)

#define DEBUG_SINGLESTEP() CPUExecuteInstruction() // Execute a single instruction, return 0 or Frame rate on frame end.
#define DEBUG_RUN(b1,b2) CPUExecute(b1,b2) // Run a frame or to breakpoint, returns -1 if breakpoint
Expand Down
53 changes: 46 additions & 7 deletions emulator/include/sys_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,66 @@ typedef unsigned int LONG32; // 32 bit type.

#define AKEY_BACKSPACE (0x5F) // Apple Backspace

// *******************************************************************************************************************************
//
// Timing
//
// *******************************************************************************************************************************

#define CYCLE_RATE (62*1024*1024/10) // Cycles per second (6.25Mhz)
#define FRAME_RATE (60) // Frames per second (60 same as DVI)
#define CYCLES_PER_FRAME (CYCLE_RATE / FRAME_RATE) // Cycles per frame

extern LONG32 cycles;

// *******************************************************************************************************************************
//
// Prototypes
//
// *******************************************************************************************************************************

void CPUReset(void);
BYTE8 CPUExecuteInstruction(void);
BYTE8 CPUWriteKeyboard(BYTE8 pattern);
BYTE8 CPUReadMemory(WORD16 address);
BYTE8 *CPUAccessMemory(void);
int CPUUseDebugKeys(void);

void CPUInterruptMaskable(void);
#define Read(a) _Read(a) // Basic Read
#define Write(a,d) _Write(a,d) // Basic Write
#define ReadWord(a) (Read(a) | ((Read((a)+1) << 8))) // Read 16 bit, Basic
#define Cycles(n) cycles += (n) // Bump Cycles
#define Fetch() _Read(pc++) // Fetch byte
#define FetchWord() { temp16 = Fetch();temp16 |= (Fetch() << 8); } // Fetch word

typedef struct __CPUSTATUS {
int a,x,y,sp,pc;
int carry,zero,sign,interruptDisable,decimal,brk,overflow,status;
int cycles;
} CPUSTATUS;
BYTE8 _Read(WORD16 address); // Need to be forward defined as
void _Write(WORD16 address,BYTE8 data); // used in support functions.
WORD16 CPUGetPC(void);

CPUSTATUS *CPUGetStatus(void);
BYTE8 CPUExecute(WORD16 breakPoint1,WORD16 breakPoint2);
WORD16 CPUGetStepOverBreakpoint(void);
void CPUWriteMemory(WORD16 address,BYTE8 data);
void CPUEndRun(void);
void CPULoadBinary(char *fileName);
void CPUExit(void);
void CPUSaveArguments(int argc,char *argv[]);

// *******************************************************************************************************************************
//
// 6502 Prototypes
//
// *******************************************************************************************************************************

typedef struct __CPUSTATUS65 {
int a,x,y,sp,pc;
int carry,zero,sign,interruptDisable,decimal,brk,overflow,status;
int cycles;
} CPUSTATUS65;

BYTE8 CPUExecute6502(void);
void CPUReset6502(void);
CPUSTATUS65 *CPUGetStatus65(void);
WORD16 CPUGetPC65(void);
int CPUGetStep65(BYTE8 opcode);

#endif
104 changes: 104 additions & 0 deletions emulator/src/core/processors/6502.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// *******************************************************************************************************************************
// *******************************************************************************************************************************
//
// Name: 6502.cpp
// Purpose: 6502 Processor Emulation.
// Created: 28th April 2024
// Author: Paul Robson (paul@robsons.org.uk)
//
// *******************************************************************************************************************************
// *******************************************************************************************************************************

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <cstdint>
#include "sys_processor.h"
#include "sys_debug_system.h"
#include "hardware.h"
#include "common.h"

// *******************************************************************************************************************************
//
// CPU / Memory
//
// *******************************************************************************************************************************

static BYTE8 a,x,y,s; // 6502 A,X,Y and Stack registers
static BYTE8 carryFlag,interruptDisableFlag,breakFlag, // Values representing status reg
decimalFlag,overflowFlag,sValue,zValue;
static WORD16 pc;

// *******************************************************************************************************************************
//
// Generated support functions
//
// *******************************************************************************************************************************

#include "6502/__6502support.h"

// *******************************************************************************************************************************
//
// Reset 6502
//
// *******************************************************************************************************************************

void CPUReset6502(void) {
resetProcessor(); // Reset CPU
}

// *******************************************************************************************************************************
//
// Execute a single 6502 instruction
//
// *******************************************************************************************************************************

BYTE8 CPUExecute6502(void) {
BYTE8 opcode = Fetch(); // Fetch opcode.
BYTE8 forceSync = 0;
switch(opcode) { // Execute it.
#include "6502/__6502opcodes.h"

case 0xF3: // $F3 forces sync in emulator. Not needed in real hardware.
forceSync = 1;break;
}
return forceSync;
}

// *******************************************************************************************************************************
//
// Get 6502 PC
//
// *******************************************************************************************************************************

WORD16 CPUGetPC65(void) {
return pc;
}

// *******************************************************************************************************************************
//
// Retrieve a snapshot of the processor
//
// *******************************************************************************************************************************

static CPUSTATUS65 st; // Status area

CPUSTATUS65 *CPUGetStatus65(void) {
st.a = a;st.x = x;st.y = y;st.sp = s;st.pc = pc;
st.carry = carryFlag;st.interruptDisable = interruptDisableFlag;st.zero = (zValue == 0);
st.decimal = decimalFlag;st.brk = breakFlag;st.overflow = overflowFlag;
st.sign = (sValue & 0x80) != 0;st.status = constructFlagRegister();
st.cycles = cycles;
return &st;
}

// *******************************************************************************************************************************
//
// Handle skipping subroutine calls
//
// *******************************************************************************************************************************

int CPUGetStep65(BYTE8 opcode) {
return (opcode == 0x20) ? 3 : 0;
}
22 changes: 19 additions & 3 deletions emulator/src/core/sys_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ int DBGXDasm(int addr, char* buffer) {
return p;
}

// Return the number of bytes (1, 2 or 3) occupied by the instruction at addr.
// *******************************************************************************************************************************
//
// Return the number of bytes (1, 2 or 3) occupied by the instruction at addr.
//
// *******************************************************************************************************************************

int DBGXInstructionSize(int addr) {
int opcode = CPUReadMemory(addr);
const char *at = strchr(_mnemonics[opcode],'@');
Expand All @@ -119,7 +124,12 @@ int DBGXInstructionSize(int addr) {
return 1; // It's a bare opcode.
}

// Dump out nbytes of memory to a string buffer.
// *******************************************************************************************************************************
//
// Dump out nbytes of memory to a string buffer.
//
// *******************************************************************************************************************************

// Buffer must have room for at least 3 bytes per memory location,
// plus an extra byte for null-termination.
// e.g for nbytes=3: "XX XX XX \0"
Expand All @@ -132,10 +142,16 @@ void DBGXDumpMem(int addr, int nbytes, char* buffer) {
}


// *******************************************************************************************************************************
//
// Render debug information and/or display
//
// *******************************************************************************************************************************

void DBGXRender(int *address,int showDisplay) {
int n = 0;
char buffer[32];
CPUSTATUS *s = CPUGetStatus();
CPUSTATUS65 *s = CPUGetStatus65();

if (showDisplay == 0) {
GFXSetCharacterSize(36,24);
Expand Down
Loading
Loading