59Fermer61
GodzilLe 06/05/2008 à 16:28
Vu qu'on est dans le concours de celui qui a la plus grosse wink


/**
 *  CoreCPU - The Quick6502 Project
 *  corecpu.c
 *
 *  Created by Manoel Trapier on 24/02/08
 *  Copyright 2008 986 Corp. All rights reserved.
 *
 *  $LastChangedDate$
 *  $Author$
 *  $HeadURL$
 *  $Revision$
 *
 */

/* Depending on the OS, one of these provide the malloc function */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

{snap}

/**
 * Initialise the CPU
 *
 * Inputs:
 *
 * - CPU Init structure:
 *  - Memory Read function pointer
 *  - Memory Write function pointer
 *  - Fast memory read function pointer (for opcodes read)
 *  - Fast page 0 function Read/Write
 *  - Fast page 1 function Read/Write
 *
 * Output:
 *
 * (void *): An opaque pointer to the internal structure of the CPU.
 *           NULL if an error occured !
 */
quick6502_cpu *quick6502_init(quick6502_cpuconfig *config)
{
   quick6502_cpu *cpu;

   /* Alloc structure */
   cpu = (quick6502_cpu *) malloc (sizeof (quick6502_cpu));
   if (!cpu)
      return NULL;

   /* Initialise other variables */
   cpu->running = 0; /* CPU is currently NOT running */

   cpu->cycle_done = 0;
   
   /* Initialise registers */
   cpu->reg_A = 0;
   cpu->reg_X = 0;
   cpu->reg_Y = 0;
   cpu->reg_S = 0xFF;
   
   cpu->reg_P = Q6502_D_FLAG | Q6502_I_FLAG;
   
   if (config->memory_read != NULL)
      cpu->memory_read                 = config->memory_read;
   else
      goto init_error;

   if (config->memory_write != NULL)
      cpu->memory_write                = config->memory_write;
   else
      goto init_error;

   if (config->memory_opcode_read != NULL)
      cpu->memory_opcode_read          = config->memory_opcode_read;
   else
      cpu->memory_opcode_read          = config->memory_read;


   if (config->memory_page0_read != NULL)
      cpu->memory_page0_read           = config->memory_page0_read;
   else
      cpu->memory_page0_read           = config->memory_read;

   if (config->memory_page0_write != NULL)
      cpu->memory_page0_write          = config->memory_page0_write;
   else
      cpu->memory_page0_write          = config->memory_write;

   if (config->memory_stack_read != NULL)
      cpu->memory_stack_read           = config->memory_stack_read;
   else
      cpu->memory_stack_read           = config->memory_read;

   if (config->memory_stack_write != NULL)
      cpu->memory_stack_write          = config->memory_stack_write;
   else
      cpu->memory_stack_write          = config->memory_write;
   
   return cpu;

init_error:
   if (cpu)
      free (cpu);

   return NULL;
}

{snap}

/**
 * Run cpu for at least X cycles 
 *
 * Output:
 *
 * int: (Number of cycle really done) - (Number of cycle asked)
 */
int quick6502_run(quick6502_cpu *cpu, int cycles)
{
   cpu->running = !0;

   while(cpu->cycle_done < cycles)
   {
      quick6502_exec_one(cpu);
   }
   cpu->cycle_done -= cycles;

   cpu->running = 0;

   return cycles + cpu->cycle_done;
}