Skip to content

Commit

Permalink
Changed lib6502 to use scan lines and cycles-per-scanline as video fr…
Browse files Browse the repository at this point in the history
…ame timing
  • Loading branch information
robmcmullen committed Jul 13, 2019
1 parent d5715a0 commit ddf75b5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 15 deletions.
29 changes: 23 additions & 6 deletions lib6502/6502-emu_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "libdebugger.h"
#include "libudis.h"

uint16_t cycles_per_scan_line;
long cycles_per_frame;

int apple2_mode = 0;
Expand All @@ -28,7 +29,7 @@ void lib6502_init_debug_kernel() {
PC = 0xf000;
}

void lib6502_init_cpu(float frequency_mhz, float refresh_rate_hz) {
void lib6502_init_cpu(int scan_lines, int cycles_per) {
init_tables();

A = 0;
Expand All @@ -39,19 +40,30 @@ void lib6502_init_cpu(float frequency_mhz, float refresh_rate_hz) {
PC = 0xfffe;
memset(memory, 0, sizeof(memory));

cycles_per_frame = (long)((frequency_mhz * 1000000.0) / refresh_rate_hz);
cycles_per_scan_line = cycles_per;
cycles_per_frame = (long)(scan_lines * cycles_per_scan_line);

lib6502_init_debug_kernel();
}

void lib6502_clear_state_arrays(void *input, output_t *output) {
frame_status_t *status = &output->status;

status->frame_number = 0;
status->frame_status = 0;
status->cycles_since_power_on = 0;
status->instructions_since_power_on = 0;
status->cycles_user = 0;
status->instructions_user = 0;
status->current_instruction_in_frame = 0;
status->use_memory_access = 1;
status->brk_into_debugger = 1;

status->final_cycle_in_frame = cycles_per_frame - 1;

// Initialize frame cycle count at max so first frame cycle count will
// start at zero
status->current_cycle_in_frame = cycles_per_frame - 1;
}

void lib6502_configure_state_arrays(void *input, output_t *output) {
Expand Down Expand Up @@ -108,8 +120,9 @@ int lib6502_show_current_instruction(history_6502_t *entry)
int lib6502_step_cpu(frame_status_t *status, history_6502_t *entry, breakpoints_t *breakpoints)
{
int count, bpid;
uint8_t last_sp, opcode;
uint8_t last_sp, opcode, cycles;
intptr_t index;
uint16_t line;
history_breakpoint_t *b;

last_pc = PC;
Expand All @@ -118,8 +131,13 @@ int lib6502_step_cpu(frame_status_t *status, history_6502_t *entry, breakpoints_
opcode = memory[PC];
inst = instructions[opcode];
count = lengths[inst.mode];
if (entry) lib6502_show_current_instruction(entry);

if (entry) {
lib6502_show_current_instruction(entry);
line = status->current_cycle_in_frame / cycles_per_scan_line;
cycles = status->current_cycle_in_frame % cycles_per_scan_line;
entry->scan_line = line;
entry->clock_cycle_in_scan_line = line > 255 ? cycles | 0x80 : cycles;
}
bpid = libdebugger_check_breakpoints(breakpoints, status, &lib6502_register_callback, opcode == 0x4c);
if (bpid >= 0) {
status->frame_status = FRAME_BREAKPOINT;
Expand Down Expand Up @@ -379,7 +397,6 @@ int lib6502_next_frame(input_t *input, output_t *output, breakpoints_t *breakpoi
if (apple2_mode) {
memory[0xc000] = input->keychar;
}
status->final_cycle_in_frame = cycles_per_frame - 1;
bpid = libdebugger_calc_frame(&lib6502_calc_frame, memory, (frame_status_t *)output, breakpoints, history);
lib6502_get_current_state(output);
return bpid;
Expand Down
2 changes: 1 addition & 1 deletion lib6502/6502-emu_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extern long cycles_per_frame;

/* library functions defined in lib6502.c */

void lib6502_init_cpu(float frequency_mhz, float refresh_rate_hz);
void lib6502_init_cpu(int scan_lines, int cycles_per_scan_line);

void lib6502_get_current_state(output_t *output);

Expand Down
4 changes: 2 additions & 2 deletions lib6502/lib6502.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import numpy as np
cimport numpy as np

cdef extern:
int lib6502_init_cpu(float, float)
int lib6502_init_cpu(int, int)
int lib6502_clear_state_arrays(np.uint8_t *buf, np.uint8_t *buf)
int lib6502_configure_state_arrays(np.uint8_t *buf, np.uint8_t *buf)
int lib6502_next_frame(np.uint8_t *buf, np.uint8_t *buf, np.uint8_t *buf, np.uint8_t *buf)
Expand All @@ -12,7 +12,7 @@ cdef extern:
void lib6502_set_a2_emulation_mode(np.uint8_t value)

def start_emulator(args):
lib6502_init_cpu(1.023, 60.0) # apple 2 speed
lib6502_init_cpu(262, 65) # apple 2 speed

def clear_state_arrays(np.ndarray input not None, np.ndarray output not None):
cdef np.uint8_t[:] ibuf
Expand Down
8 changes: 7 additions & 1 deletion libdebugger/libdebugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,13 @@ int libdebugger_calc_frame(emu_frame_callback_ptr calc, uint8_t *memory, frame_s
default:
output->frame_number += 1;
output->current_instruction_in_frame = 0;
output->current_cycle_in_frame = 0;

// number of cycles may cross a frame boundary, so any extra cycles
// recorded in the last frame are skipped at the start of this frame
output->current_cycle_in_frame -= output->final_cycle_in_frame;
if (output->current_cycle_in_frame < 0) {
printf("warning: frame %d starting at negative cycle offset %d\n", output->frame_number, output->current_cycle_in_frame);
}
libdebugger_memory_access_start_frame(memory, output);
}
output->frame_status = FRAME_INCOMPLETE;
Expand Down
4 changes: 2 additions & 2 deletions libudis/libudis.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ typedef struct {
uint8_t after2;
uint8_t before3;
uint8_t after3;
uint8_t extra1;
uint8_t extra2;
uint8_t clock_cycle_in_scan_line;
uint8_t scan_line;
} history_6502_t; /* 24 bytes */

typedef struct {
Expand Down
4 changes: 2 additions & 2 deletions omnivore/disassembler/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
("after2", np.uint8),
("before3", np.uint8),
("after3", np.uint8),
("extra1", np.uint8),
("extra2", np.uint8),
("cycle_in_scan_line", np.uint8),
("scan_line", np.uint8),
])

HISTORY_ATARI800_DTYPE = np.dtype([
Expand Down
5 changes: 4 additions & 1 deletion omnivore/viewers/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ def get_row_label_text(self, start_line, num_lines, step=1):
if t == flags.DISASM_NEXT_INSTRUCTION:
h = h.view(dtype=dd.HISTORY_BREAKPOINT_DTYPE)
t = h['disassembler_type_cpu']
if t == flags.DISASM_ATARI800_HISTORY:
if t == flags.DISASM_ATARI800_HISTORY or t == flags.DISASM_6502_HISTORY:
# lib6502 uses same cycles/scanline info in history, named
# differently but having same meaning as antic_xpos and
# antic_ypos
h = h.view(dtype=dd.HISTORY_ATARI800_DTYPE)
x = h['antic_xpos']
y = h['antic_ypos']
Expand Down

0 comments on commit ddf75b5

Please sign in to comment.