Skip to content

Commit 52c0c5f

Browse files
committed
Use firmware XSTACK
1 parent e8eeca1 commit 52c0c5f

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

src/chips/ria816.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#undef CHIPS_IMPL
44

55
#include "ria816.h"
6+
#include "sys/mem.h"
67

78
#include <string.h>
89
#include <stdlib.h>
@@ -26,7 +27,6 @@ void ria816_init(ria816_t* c, const ria816_desc_t* desc) {
2627

2728
c->ticks_per_ms = desc->tick_hz * RIA816_FIXEDPOINT_SCALE / 1000000;
2829

29-
rb_init(&c->api_stack);
3030
c->api_cb = desc->api_cb;
3131
c->user_data = desc->user_data;
3232

@@ -40,7 +40,6 @@ void ria816_reset(ria816_t* c) {
4040
c->us = 0;
4141
rb_init(&c->uart_rx);
4242
rb_init(&c->uart_tx);
43-
rb_init(&c->api_stack);
4443
m6526_reset(&c->cia);
4544
}
4645

@@ -108,7 +107,10 @@ uint8_t ria816_reg_read(ria816_t* c, uint8_t addr) {
108107
case RIA816_IRQ_ENABLE: data = c->irq_enable; break;
109108
case RIA816_IRQ_STATUS: data = ~(c->int_status); break;
110109

111-
case RIA816_API_STACK: rb_get(&c->api_stack, &data); break;
110+
case RIA816_API_STACK:
111+
data = xstack[xstack_ptr];
112+
if (xstack_ptr < XSTACK_SIZE) ++xstack_ptr;
113+
break;
112114

113115
default: data = c->reg[addr];
114116
}
@@ -122,7 +124,9 @@ void ria816_reg_write(ria816_t* c, uint8_t addr, uint8_t data) {
122124
case RIA816_IRQ_STATUS: break;
123125
case RIA816_IRQ_ENABLE: c->irq_enable = (data && 0x01); break;
124126

125-
case RIA816_API_STACK: rb_put(&c->api_stack, data); break;
127+
case RIA816_API_STACK:
128+
if (xstack_ptr) xstack[--xstack_ptr] = data;
129+
break;
126130
case RIA816_API_OP_RET:
127131
if (c->api_cb) c->api_cb(data, c->user_data);
128132
break;

src/chips/ria816.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ typedef struct {
190190
int ticks_per_ms;
191191
int ticks_counter;
192192
uint64_t pins;
193-
ring_buffer_t api_stack;
194193
// API callback
195194
ria816_api_call_t api_cb;
196195
// optional user-data for the API callback

src/systems/x65.c

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -576,47 +576,38 @@ bool x65_load_snapshot(x65_t* sys, uint32_t version, x65_t* src) {
576576
#include "sys/cpu.h"
577577
#include "term/font.h"
578578

579-
volatile uint8_t __regs[0x40];
579+
volatile uint8_t regs[0x40];
580580
uint8_t xstack[XSTACK_SIZE + 1];
581581
size_t volatile xstack_ptr;
582582

583-
static bool api_stack_pop_uint16(x65_t* sys, uint16_t* value) {
584-
return rb_get(&sys->ria.api_stack, &((uint8_t*)value)[0]) && rb_get(&sys->ria.api_stack, &((uint8_t*)value)[1]);
585-
}
586-
static bool api_stack_push_uint16(x65_t* sys, uint16_t value) {
587-
return rb_put(&sys->ria.api_stack, ((uint8_t*)&value)[1]) && rb_put(&sys->ria.api_stack, ((uint8_t*)&value)[0]);
588-
}
589-
590583
void _x65_api_call(uint8_t data, void* user_data) {
591584
x65_t* sys = (x65_t*)user_data;
592585

593586
// sync RIA regs
594-
memcpy(__regs, sys->ria.reg, sizeof(__regs));
587+
memcpy(regs, sys->ria.reg, sizeof(regs));
595588

596589
switch (data) {
597590
case API_OP_ZXSTACK: {
598-
rb_init(&sys->ria.api_stack);
591+
xstack_ptr = XSTACK_SIZE;
592+
api_return_ax(0);
599593
} break;
600594
case API_OP_PHI2: {
601-
api_stack_push_uint16(sys, CPU_PHI2_DEFAULT);
595+
const uint16_t phi2 = CPU_PHI2_DEFAULT;
596+
api_push_uint16(&phi2);
597+
api_return_ax(0);
602598
} break;
603599
case API_OP_OEM_GET_CHARGEN: {
604-
uint8_t value;
605-
if (!rb_get(&sys->ria.api_stack, &value)) break;
606-
uint32_t mem_addr = (uint32_t)value;
607-
if (!rb_get(&sys->ria.api_stack, &value)) break;
608-
mem_addr |= (uint32_t)value << 8;
609-
if (!rb_get(&sys->ria.api_stack, &value)) break;
610-
mem_addr |= (uint32_t)value << 16;
611-
if (!rb_get(&sys->ria.api_stack, &value)) break;
612-
uint16_t code_page = (uint16_t)value;
613-
if (!rb_get(&sys->ria.api_stack, &value)) break;
614-
code_page |= (uint16_t)value << 8;
615-
616-
// copy chargen to memory
617-
printf("Loading font page %02X to $%06X\n", code_page, mem_addr);
618-
for (size_t i = 0; i < 256 * 8; ++i) {
619-
mem_ram_write(sys, mem_addr++, font_get_byte(i, code_page));
600+
uint16_t chargen_cp;
601+
uint32_t chargen_addr;
602+
if (!api_pop_uint16(&chargen_cp) || !api_pop_uint16(&((uint16_t*)(&chargen_addr))[0])
603+
|| !api_pop_uint8(&((uint8_t*)(&chargen_addr))[2]))
604+
api_return_errno(API_EINVAL);
605+
else {
606+
// blit chargen to memory
607+
for (size_t i = 0; i < 256 * 8; ++i) {
608+
mem_ram_write(sys, chargen_addr++, font_get_byte(i, chargen_cp));
609+
}
610+
api_return_ax(0);
620611
}
621612
} break;
622613
case API_OP_HALT: // STOP CPU
@@ -626,7 +617,7 @@ void _x65_api_call(uint8_t data, void* user_data) {
626617
}
627618

628619
// sync RIA regs back
629-
memcpy(sys->ria.reg, __regs, sizeof(__regs));
620+
memcpy(sys->ria.reg, regs, sizeof(regs));
630621
}
631622

632623
#define RP6502_CODE_PAGE 0

0 commit comments

Comments
 (0)