2019-07-30 08:05:21 +00:00
//
// main.c
// 6502
//
// Created by Tamas Rudnai on 7/14/19.
// Copyright © 2019 GameAlloy. All rights reserved.
//
# ifndef __APPLE2_MMIO_H__
# define __APPLE2_MMIO_H__
# include "common.h"
# include "6502.h"
2020-01-28 06:54:03 +00:00
typedef union {
struct {
uint8_t latch ;
uint8_t shift ;
} ;
struct {
uint16_t lower15 : 15 ;
uint16_t valid : 1 ;
} ;
uint16_t shift16 ;
} WOZread_t ;
2019-07-30 08:05:21 +00:00
2019-11-28 04:27:32 +00:00
uint8_t Apple2_64K_RAM [ 64 * KB ] = { 0 } ;
uint8_t * RAM = Apple2_64K_RAM ;
2020-01-28 06:54:03 +00:00
WOZread_t WOZread = { 0 } ;
2019-11-28 04:27:32 +00:00
enum slot {
SLOT0 = 0x00 ,
SLOT1 = 0x10 ,
SLOT2 = 0x20 ,
SLOT3 = 0x30 ,
SLOT4 = 0x40 ,
SLOT5 = 0x50 ,
SLOT6 = 0x60 ,
SLOT7 = 0x70 ,
2019-07-30 08:05:21 +00:00
} ;
2019-11-28 04:27:32 +00:00
enum mmio {
io_KBD = 0xC000 ,
io_KBDSTRB = 0xC010 ,
io_DISK_PHASE0_OFF = 0xC080 ,
io_DISK_PHASE0_ON = 0xC081 ,
io_DISK_PHASE1_OFF = 0xC082 ,
io_DISK_PHASE1_ON = 0xC083 ,
io_DISK_PHASE2_OFF = 0xC084 ,
io_DISK_PHASE2_ON = 0xC085 ,
io_DISK_PHASE3_OFF = 0xC086 ,
io_DISK_PHASE3_ON = 0xC087 ,
io_DISK_POWER_OFF = 0xC088 ,
io_DISK_POWER_ON = 0xC089 ,
io_DISK_SELECT_1 = 0xC08A ,
io_DISK_SELECT_2 = 0xC08B ,
io_DISK_READ = 0xC08C ,
io_DISK_WRITE = 0xC08D ,
io_DISK_CLEAR = 0xC08E ,
io_DISK_SHIFT = 0xC08F ,
} ;
2019-07-30 08:05:21 +00:00
# define PAGESIZE 256
# define PAGES 16
2019-09-09 07:27:31 +00:00
//uint8_t ram_0[PAGESIZE];
//uint8_t ram_1[PAGESIZE];
//uint8_t ram_2[PAGESIZE];
//uint8_t ram_3[PAGESIZE];
//uint8_t ram_4[PAGESIZE];
//uint8_t ram_5[PAGESIZE];
//uint8_t ram_6[PAGESIZE];
//uint8_t ram_7[PAGESIZE];
//uint8_t ram_8[PAGESIZE];
//uint8_t ram_9[PAGESIZE];
//uint8_t ram_A[PAGESIZE];
//uint8_t ram_B[PAGESIZE];
//uint8_t aui_C[PAGESIZE];
//uint8_t rom_D[PAGESIZE];
//uint8_t rom_E[PAGESIZE];
//uint8_t rom_F[PAGESIZE];
//
//uint8_t * ram[PAGES] = {
// ram_0,
// ram_1,
// ram_2,
// ram_3,
// ram_4,
// ram_5,
// ram_6,
// ram_7,
// ram_8,
// ram_9,
// ram_A,
// ram_B,
// aui_C,
// rom_D,
// rom_E,
// rom_F,
//};
2019-07-30 08:05:21 +00:00
//uint8_t ( * mmio_read [ 64 * KB ] )( uint16_t addr );
2019-09-07 23:18:05 +00:00
typedef union address16_u {
uint16_t addr ;
struct {
uint8_t offs ;
uint8_t page ;
} ;
} address16_t ;
2019-07-30 08:05:21 +00:00
2019-09-07 23:18:05 +00:00
2019-11-28 04:27:32 +00:00
# define CASE_DISKII(x) \
case io_DISK_PHASE0_OFF + SLOT # # x : \
printf ( " io_DISK_PHASE0_OFF (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_PHASE0_ON + SLOT # # x : \
printf ( " io_DISK_PHASE0_ON (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_PHASE1_OFF + SLOT # # x : \
printf ( " io_DISK_PHASE1_OFF (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_PHASE1_ON + SLOT # # x : \
printf ( " io_DISK_PHASE1_ON (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_PHASE2_OFF + SLOT # # x : \
printf ( " io_DISK_PHASE2_OFF (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_PHASE2_ON + SLOT # # x : \
printf ( " io_DISK_PHASE2_ON (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_PHASE3_OFF + SLOT # # x : \
printf ( " io_DISK_PHASE3_OFF (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_PHASE3_ON + SLOT # # x : \
printf ( " io_DISK_PHASE3_ON (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_POWER_OFF + SLOT # # x : \
printf ( " io_DISK_POWER_OFF (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_POWER_ON + SLOT # # x : \
printf ( " io_DISK_POWER_ON (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_SELECT_1 + SLOT # # x : \
printf ( " io_DISK_SELECT_1 (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_SELECT_2 + SLOT # # x : \
printf ( " io_DISK_SELECT_2 (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_READ + SLOT # # x : \
printf ( " io_DISK_READ (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_WRITE + SLOT # # x : \
printf ( " io_DISK_WRITE (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_CLEAR + SLOT # # x : \
printf ( " io_DISK_CLEAR (S%u) \n " , x ) ; \
return 0 ; \
case io_DISK_SHIFT + SLOT # # x : \
printf ( " io_DISK_SHIFT (S%u) \n " , x ) ; \
return 0 ;
2020-01-28 22:00:04 +00:00
static const int minDiskTrackNum = 0 ;
static const int maxDiskTrackNum = 39 ;
static const int minDiskPhaseStates = 8 ; // 4 quarters * 2 because of two neighbouring magnets can be activated at the same time which gets you a half quarter movement
2020-01-28 06:54:03 +00:00
static const int minDiskPhaseNum = 0 ;
2020-01-28 22:00:04 +00:00
static const int maxDiskPhaseNum = minDiskPhaseStates * maxDiskTrackNum ;
2019-11-28 04:27:32 +00:00
struct phase_t {
2020-01-28 06:54:03 +00:00
uint8_t lastMagnet : 4 ;
uint8_t magnet : 4 ;
int count ;
} phase = { 0 , 0 , 0 } ;
//static const int8_t phaseTransition[4][4] = {
// { 0, -1, 0, +1 },
// { +1, 0, -1, 0 },
// { 0, +1, 0, -1 },
// { -1, 0, +1, 0 },
//};
//static const int phaseTransition[16][16] = {
//// 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 0000
// { 0, 0, -2, -1, 0, 0, 0, 0, +2, +1, 0, 0, 0, 0, 0, 0 }, // 0001
// { 0, +2, 0, +1, -2, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 0010
// { 0, +1, -1, 0, 0, 0, -2, 0, 0, +2, 0, 0, 0, 0, 0, 0 }, // 0011
// { 0, 0, +2, 0, 0, 0, +1, 0, -2, 0, 0, 0, -1, 0, 0, 0 }, // 0100
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 0101
// { 0, 0, +1, +2, -1, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 0 }, // 0110
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 0111
// { 0, -2, 0, 0, +2, 0, 0, 0, 0, -1, 0, 0, +1, 0, 0, 0 }, // 1000
// { 0, -1, 0, -2, 0, 0, 0, 0, +1, 0, 0, 0, +2, 0, 0, 0 }, // 1001
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 1010
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 1011
// { 0, 0, 0, 0, +1, 0, +2, 0, -1, -2, 0, 0, 0, 0, 0, 0 }, // 1100
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 1101
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 1110
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 1111
//};
// Magnet States --> Stepper Motor Position
//
// N
// 0001
// NW | NE
// 1001 | 0011
// |
// W 1000 ------- o ------- 0010 E
// |
// 1100 | 0110
// SW | SE
// 0100
// S
// motor position from the magnet state
// -1 means invalid, not supported
static const int magnet_to_Poistion [ 16 ] = {
// 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
- 1 , 0 , 2 , 1 , 4 , - 1 , 3 , - 1 , 6 , 7 , - 1 , - 1 , 5 , - 1 , - 1 , - 1
2019-11-28 04:27:32 +00:00
} ;
2020-01-28 06:54:03 +00:00
static const int position_to_direction [ 8 ] [ 8 ] = {
// N NE E SE S SW W NW
// 0 1 2 3 4 5 6 7
{ 0 , 1 , 2 , 3 , 0 , - 3 , - 2 , - 1 } , // 0 N
{ - 1 , 0 , 1 , 2 , 3 , 0 , - 3 , - 2 } , // 1 NE
{ - 2 , - 1 , 0 , 1 , 2 , 3 , 0 , - 3 } , // 2 E
{ - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 0 } , // 3 SE
{ 0 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 } , // 4 S
{ 3 , 0 , - 3 , - 2 , - 1 , 0 , 1 , 2 } , // 5 SW
{ 2 , 3 , 0 , - 3 , - 2 , - 1 , 0 , 1 } , // 6 W
{ 1 , 2 , 3 , 0 , - 3 , - 2 , - 1 , 0 } , // 7 NW
} ;
INLINE void diskII_phase ( ) {
2019-11-28 04:27:32 +00:00
2020-01-28 06:54:03 +00:00
int position = magnet_to_Poistion [ phase . magnet ] ;
if ( position > = 0 ) {
int lastPosition = phase . count & 7 ;
int direction = position_to_direction [ lastPosition ] [ position ] ;
phase . count + = direction ;
if ( phase . count < minDiskPhaseNum ) {
phase . count = minDiskPhaseNum ;
}
else if ( phase . count > maxDiskPhaseNum ) {
phase . count = maxDiskPhaseNum ;
}
printf ( " , p:%d d:%d l:%d: ph:%u trk:%u) " , position , direction , lastPosition , phase . count , woz_tmap . phase [ phase . count ] ) ;
}
printf ( " \n " ) ;
}
2019-11-28 04:27:32 +00:00
2019-09-15 11:02:22 +00:00
INLINE uint8_t ioRead ( uint16_t addr ) {
2019-11-28 04:27:32 +00:00
dbgPrintf ( " mmio read:%04X \n " , addr ) ;
2020-01-28 06:54:03 +00:00
uint8_t currentMagnet = 0 ;
2019-07-30 08:05:21 +00:00
switch ( addr ) {
2019-09-09 07:27:31 +00:00
case io_KBD :
2019-09-22 08:31:09 +00:00
// if ( RAM[io_KBD] > 0x7F ) printf("io_KBD:%04X\n", addr);
2019-09-11 07:11:45 +00:00
return RAM [ io_KBD ] ;
2019-09-22 08:31:09 +00:00
2019-09-09 07:27:31 +00:00
case io_KBDSTRB :
2019-09-11 07:11:45 +00:00
// TODO: This is very slow!
2019-09-22 08:31:09 +00:00
// printf("io_KBDSTRB\n");
2019-09-09 07:27:31 +00:00
return RAM [ io_KBD ] & = 0x7F ;
2019-11-28 04:27:32 +00:00
// CASE_DISKII(6)
// TODO: Make code "card insertable to slot" / aka slot independent and dynamically add/remove
case io_DISK_PHASE0_OFF + SLOT6 :
case io_DISK_PHASE1_OFF + SLOT6 :
case io_DISK_PHASE2_OFF + SLOT6 :
case io_DISK_PHASE3_OFF + SLOT6 :
2020-01-28 06:54:03 +00:00
currentMagnet = ( addr - io_DISK_PHASE0_OFF - SLOT6 ) / 2 ;
phase . magnet & = ~ ( 1 < < currentMagnet ) ;
printf ( " io_DISK_PHASE%u_OFF (S%u, ps:%X) " , currentMagnet , 6 , phase . magnet ) ;
diskII_phase ( ) ;
2019-11-28 04:27:32 +00:00
return 0 ;
2020-01-28 06:54:03 +00:00
2019-11-28 04:27:32 +00:00
case io_DISK_PHASE0_ON + SLOT6 :
case io_DISK_PHASE1_ON + SLOT6 :
case io_DISK_PHASE2_ON + SLOT6 :
case io_DISK_PHASE3_ON + SLOT6 : {
2020-01-28 06:54:03 +00:00
currentMagnet = ( addr - io_DISK_PHASE0_ON - SLOT6 ) / 2 ;
phase . magnet | = 1 < < currentMagnet ;
printf ( " io_DISK_PHASE%u_ON (S%u, ps:%X) " , currentMagnet , 6 , phase . magnet ) ;
diskII_phase ( ) ;
2019-11-28 04:27:32 +00:00
return 0 ;
}
2020-01-28 06:54:03 +00:00
2019-11-28 04:27:32 +00:00
case io_DISK_POWER_OFF + SLOT6 :
dbgPrintf2 ( " io_DISK_POWER_OFF (S%u) \n " , 6 ) ;
return 0 ;
2020-01-28 06:54:03 +00:00
2019-11-28 04:27:32 +00:00
case io_DISK_POWER_ON + SLOT6 :
dbgPrintf2 ( " io_DISK_POWER_ON (S%u) \n " , 6 ) ;
return 0 ;
2020-01-28 06:54:03 +00:00
2019-11-28 04:27:32 +00:00
case io_DISK_SELECT_1 + SLOT6 :
dbgPrintf2 ( " io_DISK_SELECT_1 (S%u) \n " , 6 ) ;
return 0 ;
2020-01-28 06:54:03 +00:00
2019-11-28 04:27:32 +00:00
case io_DISK_SELECT_2 + SLOT6 :
dbgPrintf2 ( " io_DISK_SELECT_2 (S%u) \n " , 6 ) ;
return 0 ;
2020-01-28 06:54:03 +00:00
2019-11-28 04:27:32 +00:00
case io_DISK_READ + SLOT6 :
dbgPrintf ( " io_DISK_READ (S%u) \n " , 6 ) ;
2020-01-28 06:54:03 +00:00
int track = woz_tmap . phase [ phase . count ] ;
if ( outdev ) fprintf ( outdev , " track: %d (%d) " , track , phase . count ) ;
if ( track > = 40 ) {
printf ( " TRCK TOO HIGH! \n " ) ;
return rand ( ) ;
2019-11-28 04:27:32 +00:00
}
2020-01-28 06:54:03 +00:00
// to avoid infinite loop and to search for bit 7 high
for ( int i = 0 ; i < WOZ_TRACK_BYTE_COUNT * 8 ; i + + ) {
if ( + + bitOffset > = 8 ) {
bitOffset = 0 ;
if ( + + trackOffset > = WOZ_TRACK_BYTE_COUNT ) {
trackOffset = 0 ;
}
// printf("offs:%u\n", trackOffset);
WOZread . latch = woz_trks [ track ] . data [ trackOffset ] ;
}
WOZread . shift16 < < = 1 ;
if ( WOZread . valid ) {
uint8_t byte = WOZread . shift ;
// printf("%02X ", byte);
WOZread . shift = 0 ;
if ( outdev ) fprintf ( outdev , " byte: %02X \n " , byte ) ;
return byte ;
}
}
if ( outdev ) fprintf ( outdev , " TIME OUT! \n " ) ;
return rand ( ) ;
2019-11-28 04:27:32 +00:00
case io_DISK_WRITE + SLOT6 :
dbgPrintf2 ( " io_DISK_WRITE (S%u) \n " , 6 ) ;
return 0 ;
2020-01-28 06:54:03 +00:00
2019-11-28 04:27:32 +00:00
case io_DISK_CLEAR + SLOT6 :
dbgPrintf2 ( " io_DISK_CLEAR (S%u) \n " , 6 ) ;
return 0 ;
2020-01-28 06:54:03 +00:00
2019-11-28 04:27:32 +00:00
case io_DISK_SHIFT + SLOT6 :
dbgPrintf2 ( " io_DISK_SHIFT (S%u) \n " , 6 ) ;
return 0 ;
2019-07-30 08:05:21 +00:00
default :
2019-11-28 04:27:32 +00:00
return RAM [ addr ] ;
2019-07-30 08:05:21 +00:00
}
2019-11-28 04:27:32 +00:00
2019-07-30 08:05:21 +00:00
}
2019-09-22 08:31:09 +00:00
void kbdInput ( uint8_t code ) {
// printf("kbdInput: %02X ('%c')\n", code, isprint(code) ? code : ' ');
switch ( code ) {
// case '\n':
// code = 0x0D;
// break;
//
case 0x7F : // BackSlash
code = 0x08 ;
break ;
default :
break ;
}
code | = 0x80 ;
while ( RAM [ io_KBD ] > 0x7F ) {
usleep ( 10 ) ;
}
RAM [ io_KBD ] = code ;
}
INLINE void ioWrite ( uint16_t addr , uint8_t val ) {
2019-09-07 23:18:05 +00:00
// printf("mmio:%04X\n", addr);
switch ( addr ) {
2019-09-09 07:27:31 +00:00
case io_KBD :
2019-09-07 23:18:05 +00:00
return ;
default :
break ;
}
return ;
}
2019-07-30 08:05:21 +00:00
/**
Naive implementation of RAM read from address
* */
2019-09-07 23:18:05 +00:00
2019-09-15 11:02:22 +00:00
INLINE uint8_t memread_zp ( uint8_t addr ) {
2019-09-07 23:18:05 +00:00
return RAM [ addr ] ;
}
2019-11-28 04:27:32 +00:00
/**
Naive implementation of RAM read from address
* */
INLINE uint8_t memread8 ( uint16_t addr ) {
// if ( addr == 0xD2AD ) {
// dbgPrintf("OUT OF MEMORY!\n");
// }
return RAM [ addr ] ;
}
/**
Naive implementation of RAM read from address
* */
INLINE uint16_t memread16 ( uint16_t addr ) {
return * ( uint16_t * ) ( & RAM [ addr ] ) ;
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t memread ( uint16_t addr ) {
2019-09-07 23:18:05 +00:00
// switch ( ((address16_t)addr).page ) {
// case 0xC0:
// case 0xC1:
// case 0xC2:
// case 0xC3:
// case 0xC4:
// case 0xC5:
// case 0xC6:
// case 0xC7:
// case 0xC8:
// case 0xC9:
// case 0xCA:
// case 0xCB:
// case 0xCC:
// case 0xCD:
// case 0xCE:
// case 0xCF:
// return ioRead(addr);
//
// defaut:
// break;
// }
2019-07-30 08:05:21 +00:00
2019-11-28 04:27:32 +00:00
if ( ( addr > = 0xC000 ) & & ( addr < 0xC0FF ) ) {
2019-09-22 08:31:09 +00:00
return ioRead ( addr ) ;
2019-07-30 08:05:21 +00:00
}
2019-11-28 04:27:32 +00:00
return memread8 ( addr ) ;
2019-07-30 08:05:21 +00:00
}
/**
Naive implementation of RAM read from address
* */
2019-09-15 11:02:22 +00:00
//INLINE uint16_t memioread16( uint16_t addr ) {
2019-07-30 08:05:21 +00:00
// return (uint16_t)mmio_read[ addr ](addr);
//}
2019-09-07 23:18:05 +00:00
/**
Naive implementation of RAM write to address
* */
static void memwrite_zp ( uint8_t addr , uint8_t byte ) {
RAM [ addr ] = byte ;
}
2019-07-30 08:05:21 +00:00
/**
Naive implementation of RAM write to address
* */
static void memwrite ( uint16_t addr , uint8_t byte ) {
2019-09-07 23:18:05 +00:00
// if ( addr >= 0xD000 ) {
// // ROM
// return;
// }
// if ( addr >= 0xC000 ) {
// return mmioWrite(addr);
// }
//
2019-07-30 08:05:21 +00:00
RAM [ addr ] = byte ;
}
2019-09-07 23:18:05 +00:00
2019-07-30 08:05:21 +00:00
/**
Fetching 1 byte from memory address pc ( program counter )
increase pc by one
* */
2019-09-15 11:02:22 +00:00
INLINE uint8_t fetch ( ) {
2019-11-28 04:27:32 +00:00
disHexB ( disassembly . pOpcode , RAM [ m6502 . PC ] ) ;
2019-09-15 11:02:22 +00:00
return memread ( m6502 . PC + + ) ;
2019-07-30 08:05:21 +00:00
}
/**
Fetching 2 bytes as a 16 bit number from memory address pc ( program counter )
increase pc by one
* */
2019-09-15 11:02:22 +00:00
INLINE uint16_t fetch16 ( ) {
uint16_t word = memread16 ( m6502 . PC ) ;
2020-01-28 06:54:03 +00:00
// disPrintf(disassembly.comment, "fetch16:%04X", word);
2019-09-15 11:02:22 +00:00
m6502 . PC + = 2 ;
2019-11-28 04:27:32 +00:00
disHexW ( disassembly . pOpcode , word ) ;
2019-07-30 08:05:21 +00:00
return word ;
}
2019-09-11 07:11:45 +00:00
/**
abs . . . . absolute OPC $ LLHH , X
operand is address ; effective address is address incremented by X with carry * *
* */
2019-09-15 11:02:22 +00:00
INLINE uint16_t addr_abs ( ) {
dbgPrintf ( " abs:%04X(%02X) " , * ( ( uint16_t * ) & RAM [ m6502 . PC ] ) , RAM [ * ( ( uint16_t * ) & RAM [ m6502 . PC ] ) ] ) ;
2019-11-28 04:27:32 +00:00
disPrintf ( disassembly . oper , " $%04X " , memread16 ( m6502 . PC ) )
2019-09-11 07:11:45 +00:00
return fetch16 ( ) ;
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t src_abs ( ) {
2019-09-11 07:11:45 +00:00
return memread ( addr_abs ( ) ) ;
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t * dest_abs ( ) {
2019-09-11 07:11:45 +00:00
return & RAM [ addr_abs ( ) ] ;
}
2019-09-15 11:02:22 +00:00
INLINE int8_t rel_addr ( ) {
2019-11-28 04:27:32 +00:00
disPrintf ( disassembly . oper , " $%04X " , m6502 . PC + 1 + ( int8_t ) memread8 ( m6502 . PC ) )
2019-09-11 07:11:45 +00:00
return fetch ( ) ;
}
2019-09-15 11:02:22 +00:00
INLINE uint16_t abs_addr ( ) {
2019-11-28 04:27:32 +00:00
disPrintf ( disassembly . oper , " $%04X " , memread16 ( m6502 . PC ) )
2019-09-11 07:11:45 +00:00
return fetch16 ( ) ;
}
2019-09-15 11:02:22 +00:00
INLINE uint16_t ind_addr ( ) {
2019-11-28 04:27:32 +00:00
disPrintf ( disassembly . oper , " ($%04X) " , memread16 ( m6502 . PC ) )
2020-01-28 06:54:03 +00:00
disPrintf ( disassembly . comment , " ind_addr:%04X " , memread16 ( memread16 ( m6502 . PC ) ) )
2019-09-11 07:11:45 +00:00
return memread16 ( fetch16 ( ) ) ;
2019-07-30 08:05:21 +00:00
}
/**
abs , X . . . . absolute , X - indexed OPC $ LLHH , X
operand is address ; effective address is address incremented by X with carry * *
* */
2019-09-15 11:02:22 +00:00
INLINE uint16_t addr_abs_X ( ) {
dbgPrintf ( " abs,X:%04X(%02X) " , * ( ( uint16_t * ) & RAM [ m6502 . PC ] ) + m6502 . X , RAM [ * ( ( uint16_t * ) & RAM [ m6502 . PC ] ) + m6502 . X ] ) ;
2020-01-28 06:54:03 +00:00
disPrintf ( disassembly . oper , " $%04X,X " , memread16 ( m6502 . PC ) ) ;
2019-07-30 08:05:21 +00:00
return fetch16 ( ) + m6502 . X ;
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t src_abs_X ( ) {
2019-11-28 04:27:32 +00:00
return memread ( addr_abs_X ( ) ) ;
2019-09-10 07:00:00 +00:00
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t * dest_abs_X ( ) {
2019-09-10 07:00:00 +00:00
return & RAM [ addr_abs_X ( ) ] ;
}
2019-07-30 08:05:21 +00:00
/**
abs , Y . . . . absolute , Y - indexed OPC $ LLHH , Y
operand is address ; effective address is address incremented by Y with carry * *
* */
2019-09-15 11:02:22 +00:00
INLINE uint16_t addr_abs_Y ( ) {
dbgPrintf ( " abs,Y:%04X(%02X) " , * ( ( uint16_t * ) & RAM [ m6502 . PC ] ) + m6502 . Y , RAM [ * ( ( uint16_t * ) & RAM [ m6502 . PC ] ) + m6502 . Y ] ) ;
2019-11-28 04:27:32 +00:00
disPrintf ( disassembly . oper , " $%04X,Y " , memread16 ( m6502 . PC ) )
return fetch16 ( ) + m6502 . Y ;
2019-07-30 08:05:21 +00:00
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t src_abs_Y ( ) {
2019-11-28 04:27:32 +00:00
return memread ( addr_abs_Y ( ) ) ;
2019-09-10 07:00:00 +00:00
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t * dest_abs_Y ( ) {
2019-09-10 07:00:00 +00:00
return & RAM [ addr_abs_Y ( ) ] ;
}
2019-07-30 08:05:21 +00:00
2019-09-15 11:02:22 +00:00
INLINE uint16_t imm ( ) {
2019-11-28 04:27:32 +00:00
disPrintf ( disassembly . oper , " #$%02X " , memread8 ( m6502 . PC ) )
2019-09-11 07:11:45 +00:00
return fetch ( ) ;
}
2019-07-30 08:05:21 +00:00
/**
zpg . . . . zeropage OPC $ LL
operand is zeropage address ( hi - byte is zero , address = $ 00LL )
* */
2019-09-15 11:02:22 +00:00
INLINE uint8_t addr_zp ( ) {
dbgPrintf ( " zp:%02X(%02X) " , RAM [ m6502 . PC ] , RAM [ RAM [ m6502 . PC ] ] ) ;
2019-11-28 04:27:32 +00:00
disPrintf ( disassembly . oper , " $%02X " , memread8 ( m6502 . PC ) )
2019-07-30 08:05:21 +00:00
return fetch ( ) ;
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t src_zp ( ) {
2019-09-10 07:00:00 +00:00
return memread_zp ( addr_zp ( ) ) ;
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t * dest_zp ( ) {
2019-09-10 07:00:00 +00:00
return & RAM [ addr_zp ( ) ] ;
}
2019-07-30 08:05:21 +00:00
2019-09-12 01:36:30 +00:00
/**
get a 16 bit address from the zp : zp + 1
* */
2019-09-15 11:02:22 +00:00
INLINE uint16_t addr_zp_ind ( uint8_t addr ) {
dbgPrintf ( " zpi:%02X:%04X(%02X) " , RAM [ m6502 . PC ] , * ( ( uint16_t * ) & RAM [ m6502 . PC ] ) , RAM [ * ( ( uint16_t * ) & RAM [ m6502 . PC ] ) ] ) ;
2020-01-28 06:54:03 +00:00
disPrintf ( disassembly . oper , " ($%02X) " , memread8 ( m6502 . PC ) ) ;
disPrintf ( disassembly . comment , " ind_addr:%04X " , memread16 ( memread8 ( m6502 . PC ) ) ) ;
2019-09-12 01:36:30 +00:00
return memread16 ( addr ) ;
}
/**
X , ind . . . . X - indexed , indirect OPC ( $ LL , X )
operand is zeropage address ;
effective address is word in ( LL + X , LL + X + 1 ) , inc . without carry : C . w ( $ 00LL + X )
* */
2019-09-15 11:02:22 +00:00
INLINE uint16_t addr_X_ind ( ) {
dbgPrintf ( " zpXi:%02X:%04X(%02X) " , RAM [ m6502 . PC ] , * ( ( uint16_t * ) & RAM [ m6502 . PC ] ) + m6502 . X , RAM [ * ( ( uint16_t * ) & RAM [ m6502 . PC ] ) + m6502 . X ] ) ;
2020-01-28 06:54:03 +00:00
disPrintf ( disassembly . oper , " ($%02X,X) " , memread8 ( m6502 . PC ) )
disPrintf ( disassembly . comment , " ind_addr:%04X " , memread16 ( memread8 ( m6502 . PC ) + m6502 . X ) ) ;
2019-11-28 04:27:32 +00:00
return memread16 ( fetch ( ) + m6502 . X ) ;
2019-09-12 01:36:30 +00:00
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t src_X_ind ( ) {
2019-11-28 04:27:32 +00:00
return memread ( addr_X_ind ( ) ) ;
2019-09-12 01:36:30 +00:00
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t * dest_X_ind ( ) {
2019-09-12 01:36:30 +00:00
return & RAM [ addr_X_ind ( ) ] ;
}
/**
ind , Y . . . . indirect , Y - indexed OPC ( $ LL ) , Y
operand is zeropage address ;
effective address is word in ( LL , LL + 1 ) incremented by Y with carry : C . w ( $ 00LL ) + Y
* */
2019-09-15 11:02:22 +00:00
INLINE uint16_t addr_ind_Y ( ) {
2019-09-12 01:36:30 +00:00
// uint8_t a = fetch();
// dbgPrintf("addr_ind_Y: %04X + %02X = %04X ", addr_zpg_ind( a ), m6502.Y, addr_zpg_ind( a ) + m6502.Y);
2020-01-28 06:54:03 +00:00
disPrintf ( disassembly . oper , " ($%02X),Y " , memread8 ( m6502 . PC ) )
disPrintf ( disassembly . comment , " ind_addr:%04X " , memread16 ( memread8 ( m6502 . PC ) ) + m6502 . Y ) ;
2019-11-28 04:27:32 +00:00
return memread16 ( fetch ( ) ) + m6502 . Y ;
2019-09-12 01:36:30 +00:00
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t src_ind_Y ( ) {
2019-11-28 04:27:32 +00:00
return memread ( addr_ind_Y ( ) ) ;
2019-09-12 01:36:30 +00:00
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t * dest_ind_Y ( ) {
2019-09-19 09:27:56 +00:00
uint16_t addr = addr_ind_Y ( ) ;
2019-11-28 04:27:32 +00:00
// if ( (addr >= 0xC000) && (addr <= 0xC0FF) ) {
// addr = 0xC111;
// }
2019-09-19 09:27:56 +00:00
// return & RAM[ addr_abs_Y() ];
return & RAM [ addr ] ;
// return & RAM[ addr_ind_Y() ];
2019-09-12 01:36:30 +00:00
}
2019-07-30 08:05:21 +00:00
/**
zpg , X . . . . zeropage , X - indexed OPC $ LL , X
operand is zeropage address ;
effective address is address incremented by X without carry * *
* */
2019-09-15 11:02:22 +00:00
INLINE uint8_t addr_zp_X ( ) {
2019-11-28 04:27:32 +00:00
disPrintf ( disassembly . oper , " $%02X,X " , memread8 ( m6502 . PC ) )
2019-09-22 08:31:09 +00:00
return fetch ( ) + m6502 . X ;
2019-07-30 08:05:21 +00:00
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t src_zp_X ( ) {
2019-09-11 07:11:45 +00:00
return memread_zp ( addr_zp_X ( ) ) ;
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t * dest_zp_X ( ) {
2019-09-11 07:11:45 +00:00
return & RAM [ addr_zp_X ( ) ] ;
}
2019-07-30 08:05:21 +00:00
/**
zpg , Y . . . . zeropage , Y - indexed OPC $ LL , Y
operand is zeropage address ;
effective address is address incremented by Y without carry * *
* */
2019-09-15 11:02:22 +00:00
INLINE uint8_t addr_zp_Y ( ) {
2019-11-28 04:27:32 +00:00
disPrintf ( disassembly . oper , " $%02X,Y " , memread8 ( m6502 . PC ) )
2019-09-22 08:31:09 +00:00
return fetch ( ) + m6502 . Y ;
2019-07-30 08:05:21 +00:00
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t src_zp_Y ( ) {
2019-09-11 07:11:45 +00:00
return memread_zp ( addr_zp_Y ( ) ) ;
}
2019-09-15 11:02:22 +00:00
INLINE uint8_t * dest_zp_Y ( ) {
2019-09-11 07:11:45 +00:00
return & RAM [ addr_zp_Y ( ) ] ;
}
2019-07-30 08:05:21 +00:00
# endif // __APPLE2_MMIO_H__