<commit_before>//
//  main.c
//  easy6502-xcode
//
//  Created by <NAME> on H.25/06/02.
//  Copyright (c) 平成25年 Hello-Channel, LLC. All rights reserved.
//

#include "cpu.h"
#include "mem.h"

#include "video.h"
#include "state.h"

/*
 Notes:
  
 Memory locations $200 to $5ff map to the screen pixels. Different values will
 draw different colour pixels. The colours are:
 */

static int faulted;

void v6502_fault(const char *e) {
	faulted++;
}


static FILE *logFile;
static unsigned long cycle;

static void logCpuStatus(v6502_cpu *cpu) {	
	fprintf(logFile, "Cycle #%lu\n", cycle);
	
	fprintf(logFile, "Status Register: %c%c%c%c%c%c%c%c\n",
			cpu->sr & v6502_cpu_status_negative ? 'N' : '-',
			cpu->sr & v6502_cpu_status_overflow ? 'V' : '-',
			cpu->sr & v6502_cpu_status_ignored ? 'X' : '-',
			cpu->sr & v6502_cpu_status_break ? 'B' : '-',
			cpu->sr & v6502_cpu_status_decimal ? 'D' : '-',
			cpu->sr & v6502_cpu_status_interrupt ? 'I' : '-',
			cpu->sr & v6502_cpu_status_zero ? 'Z' : '-',
			cpu->sr & v6502_cpu_status_carry ? 'C' : '-');
	fprintf(logFile, "CPU %p: pc = 0x%04x, ac = 0x%02x, x = 0x%02x, y = 0x%02x, sr = 0x%02x, sp = 0x%02x\nMEM %p: memsize = %hu (0x%04x)\n\n", cpu, cpu->pc, cpu->ac, cpu->x, cpu->y, cpu->sr, cpu->sp, cpu->memory, cpu->memory->size, cpu->memory->size);
}

void broken() {
	mvaddnstr(2, 2, "BREAK", 5);
	while (getch() != KEY_ENTER) {}
}

void loadProgram(v6502_memory *mem, const char *fname) {
	FILE *f = fopen(fname, "r");
	uint8_t byte;
	uint8_t offset = 0;
	
	while (fread(&byte, 1, 1, f)) {
		mem->bytes[0x600 + (offset++)] = byte;
	}
	
	fprintf(logFile, "Loaded ROM of %d bytes\n\n", offset);
	
	fclose(f);
}

int main(int argc, const char * argv[])
{
	v6502_cpu *cpu = v6502_createCPU();
	cpu->memory = v6502_createMemory(2048);
	WINDOW *scr = initVideo();
	initState();
	
	if (!logFile) {
		logFile = fopen("easy6502.log", "w");
	}

	// Load program code into memory
	loadProgram(cpu->memory, "easy_test.o");
	
	v6502_reset(cpu);
	while (!faulted) {
		// Update keypress byte, hold for clock cycle, and refresh random byte
		stateCycle(cpu->memory);
		<commit_msg>Moved video comment to video.c<commit_after>//
//  main.c
//  easy6502-xcode
//
//  Created by <NAME> on H.25/06/02.
//  Copyright (c) 平成25年 Hello-Channel, LLC. All rights reserved.
//

#include "cpu.h"
#include "mem.h"

#include "video.h"
#include "state.h"

static int faulted;

void v6502_fault(const char *e) {
	faulted++;
}


static FILE *logFile;
static unsigned long cycle;

static void logCpuStatus(v6502_cpu *cpu) {	
	fprintf(logFile, "Cycle #%lu\n", cycle);
	
	fprintf(logFile, "Status Register: %c%c%c%c%c%c%c%c\n",
			cpu->sr & v6502_cpu_status_negative ? 'N' : '-',
			cpu->sr & v6502_cpu_status_overflow ? 'V' : '-',
			cpu->sr & v6502_cpu_status_ignored ? 'X' : '-',
			cpu->sr & v6502_cpu_status_break ? 'B' : '-',
			cpu->sr & v6502_cpu_status_decimal ? 'D' : '-',
			cpu->sr & v6502_cpu_status_interrupt ? 'I' : '-',
			cpu->sr & v6502_cpu_status_zero ? 'Z' : '-',
			cpu->sr & v6502_cpu_status_carry ? 'C' : '-');
	fprintf(logFile, "CPU %p: pc = 0x%04x, ac = 0x%02x, x = 0x%02x, y = 0x%02x, sr = 0x%02x, sp = 0x%02x\nMEM %p: memsize = %hu (0x%04x)\n\n", cpu, cpu->pc, cpu->ac, cpu->x, cpu->y, cpu->sr, cpu->sp, cpu->memory, cpu->memory->size, cpu->memory->size);
}

void broken() {
	mvaddnstr(2, 2, "BREAK", 5);
	while (getch() != KEY_ENTER) {}
}

void loadProgram(v6502_memory *mem, const char *fname) {
	FILE *f = fopen(fname, "r");
	uint8_t byte;
	uint8_t offset = 0;
	
	while (fread(&byte, 1, 1, f)) {
		mem->bytes[0x600 + (offset++)] = byte;
	}
	
	fprintf(logFile, "Loaded ROM of %d bytes\n\n", offset);
	
	fclose(f);
}

int main(int argc, const char * argv[])
{
	v6502_cpu *cpu = v6502_createCPU();
	cpu->memory = v6502_createMemory(2048);
	WINDOW *scr = initVideo();
	initState();
	
	if (!logFile) {
		logFile = fopen("easy6502.log", "w");
	}

	// Load program code into memory
	loadProgram(cpu->memory, "/Users/kojiro/Code/v6502/easy6502/easy_test.o");
	
	v6502_reset(cpu);
	while (!faulted) {
		// Update keypress byte, hold for clock cycle, and refresh random byte
		stateCycle(cpu->memory);
		
{'fraction_non_alphanumeric': 0.190050712388312, 'fraction_numerical': 0.057474040086935525, 'mean_word_length': 3.7337142857142855, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6387639', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1738, 'n_words': 397}