1 // PC Engine CPU emulator for use with HES music files
3 // Game_Music_Emu 0.5.2
7 #include "blargg_common.h"
9 typedef blargg_long hes_time_t
; // clock cycle count
10 typedef unsigned hes_addr_t
; // 16-bit address
14 enum { future_hes_time
= LONG_MAX
/ 2 + 1 };
15 enum { page_size
= 0x2000 };
16 enum { page_shift
= 13 };
17 enum { page_count
= 8 };
19 // Attempt to execute instruction here results in CPU advancing time to
20 // lesser of irq_time() and end_time() (or end_time() if IRQs are
22 enum { idle_addr
= 0x1FFF };
24 // Can read this many bytes past end of a page
25 enum { cpu_padding
= 8 };
26 enum { irq_inhibit
= 0x04 };
31 uint8_t const* code_map
[page_count
+ 1];
52 struct state_t
* state
; // points to state_ or a local copy within run()
53 struct state_t state_
;
55 // page mapping registers
56 uint8_t mmr
[page_count
+ 1];
57 uint8_t ram
[page_size
];
61 void Cpu_init( struct Hes_Cpu
* this );
64 void Cpu_reset( struct Hes_Cpu
* this );
66 // Set end_time and run CPU from current time. Returns true if any illegal
67 // instructions were encountered.
68 bool Cpu_run( struct Hes_Emu
* this, hes_time_t end_time
) ICODE_ATTR
;
70 void Cpu_set_mmr( struct Hes_Emu
* this, int reg
, int bank
) ICODE_ATTR
;
72 // Time of ning of next instruction to be executed
73 static inline hes_time_t
Cpu_time( struct Hes_Cpu
* this )
75 return this->state
->time
+ this->state
->base
;
78 static inline uint8_t const* Cpu_get_code( struct Hes_Cpu
* this, hes_addr_t addr
)
80 return this->state
->code_map
[addr
>> page_shift
] + addr
81 #if !defined (BLARGG_NONPORTABLE)
82 % (unsigned) page_size
87 static inline int Cpu_update_end_time( struct Hes_Cpu
* this, uint8_t reg_status
, hes_time_t t
, hes_time_t irq
)
89 if ( irq
< t
&& !(reg_status
& irq_inhibit
) ) t
= irq
;
90 int delta
= this->state
->base
- t
;
91 this->state
->base
= t
;