/*
* emulates the DCPU16 system from http://0x10c.com/doc/dcpu-16.txt
+ * currently emulates '1.7' spec from http://pastebin.com/Q4JvQvnM
*
* I couldn't remember ever implementing an emulator before, so this
* happened. As such, consider this a toy in progress.
* !! v1.7 hardware interface needs to be finished
* !! v1.7 interrupts need to be finished
* change api to print into buffers rather than stdio
- * refactor opcode functiontables into switch statements
* let callbacks determine whether to override events, or just observe
* sort init callbacks by base addr, to call in-order
* make all callbacks register addr range of interest
return message;
}
+inline
+void dcpu16_cycle_inc(struct dcpu16 *vm, unsigned int n) {
+ size_t i;
+
+ while (n--) {
+ /* new cycle */
+ vm->cycle_ += 1;
+ TRACE("%s>> starting cycle %llu", vm->cycle_);
+
+ /* signal interested cycle hooks */
+ acct_event_(vm, DCPU16_ACCT_EV_CYCLE, vm->reg[DCPU16_REG_PC]);
+
+ /* signal attached hardware */
+ for (i = 0; i < vm->hw_table_entries_; i++) {
+ TRACE("%s>> notifying %s", __func__, vm->hw_table_[i].name_);
+ vm->hw_table_[i].cycle(vm, vm->hw_table_[i].data);
+ }
+ }
+}
+
/* value_decode_
* sets *v to be the address of the represented value
* value_is_a is 0 for b, 1 for a, alters behavior of some operands
&vm->reg_work_[1], &a, &ev_a_addr,\
&pc_adjust, &sp_adjust, &cycle_adjust);\
vm->reg[DCPU16_REG_SP] += sp_adjust;\
- vm->cycle += cycle_adjust;\
+ dcpu16_cycle_inc(vm, cycle_adjust);\
} while (0)
#define OP_NBI_ (void)val_b, (void)b, (void)ev_b_addr, (void)val_b_data
#define OP_BASIC_ value_decode_(vm, val_b, 0, val_b_data,\
vm->ram[ --vm->reg[DCPU16_REG_SP] ] = vm->reg[DCPU16_REG_PC];
vm->reg[DCPU16_REG_PC] = *a;
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
ACCT_W(vm->reg[DCPU16_REG_SP] + 1);
}
vm->reg[0] = *a;
}
- vm->cycle += 4;
+ dcpu16_cycle_inc(vm, 4);
}
OP_IMPL(nbi_iag) {
*a = vm->reg[DCPU16_REG_IA];
+ dcpu16_cycle_inc(vm, 1);
+
ACCT_W(ev_a_addr);
}
vm->reg[DCPU16_REG_IA] = *a;
+ dcpu16_cycle_inc(vm, 1);
+
ACCT_R(ev_a_addr);
}
vm->interrupts_deferred_ = 0;
vm->reg[DCPU16_REG_A] = vm->ram[vm->reg[DCPU16_REG_SP]++];
vm->reg[DCPU16_REG_PC] = vm->ram[vm->reg[DCPU16_REG_SP]++];
+
+ dcpu16_cycle_inc(vm, 3);
}
OP_IMPL(nbi_iaq) {
vm->interrupts_deferred_ = 0;
}
+ dcpu16_cycle_inc(vm, 2);
+
ACCT_R(ev_a_addr);
}
*a = vm->hw_table_entries_;
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
}
OP_IMPL(nbi_hwq) {
vm->reg[DCPU16_REG_X] = vm->hw_table_[*a].mfg_l;
vm->reg[DCPU16_REG_Y] = vm->hw_table_[*a].mfg_h;
- vm->cycle += 4;
+ dcpu16_cycle_inc(vm, 4);
}
OP_IMPL(nbi_hwi) {
return;
}
- vm->cycle += 4;
+ dcpu16_cycle_inc(vm, 4);
if (vm->hw_table_[*a].hwi)
vm->hw_table_[*a].hwi(vm, vm->hw_table_[*a].data);
else
vm->on_fire_ = 1;
WARN("system on fire");
- vm->cycle += 9;
+ dcpu16_cycle_inc(vm, 9);
}
static const struct opcode_entry opcode_nbi_entries[] = {
*/
*b = *a;
- vm->cycle += 1;
+ dcpu16_cycle_inc(vm, 1);
ACCT_W(ev_b_addr);
}
*b = acc;
vm->reg[DCPU16_REG_EX] = (acc > 0xffff);
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
ACCT_W(ev_b_addr);
}
*b = acc;
vm->reg[DCPU16_REG_EX] = (acc > 0xffff);
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
ACCT_W(ev_b_addr);
}
*b = acc;
vm->reg[DCPU16_REG_EX] = acc >> 16;
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
ACCT_W(ev_b_addr);
}
*b = acc;
vm->reg[DCPU16_REG_EX] = acc >> 16;
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
ACCT_W(ev_b_addr);
}
vm->reg[DCPU16_REG_EX] = (*b << 16) / *a;
}
- vm->cycle += 3;
+ dcpu16_cycle_inc(vm, 3);
ACCT_W(ev_b_addr);
}
vm->reg[DCPU16_REG_EX] = (short)(*b << 16) / (short)*a;
}
- vm->cycle += 3;
+ dcpu16_cycle_inc(vm, 3);
ACCT_W(ev_b_addr);
}
*b = *b % *a;
}
- vm->cycle += 3;
+ dcpu16_cycle_inc(vm, 3);
ACCT_W(ev_a_addr);
}
*b = (short)*b % (short)*a;
}
- vm->cycle += 3;
+ dcpu16_cycle_inc(vm, 3);
ACCT_W(ev_b_addr);
}
*b = *b & *a;
- vm->cycle += 1;
+ dcpu16_cycle_inc(vm, 1);
ACCT_W(ev_b_addr);
}
*b = *b | *a;
- vm->cycle += 1;
+ dcpu16_cycle_inc(vm, 1);
ACCT_W(ev_b_addr);
}
*b = *b ^ *a;
- vm->cycle += 1;
+ dcpu16_cycle_inc(vm, 1);
ACCT_W(ev_b_addr);
}
*b = acc & 0xffff;
vm->reg[DCPU16_REG_EX] = (*b << 16) >> *a;
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
WARN("IMPLEMENT");
*b = acc & 0xffff;
vm->reg[DCPU16_REG_EX] = (*b << 16) >> *a;
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
WARN("IMPLEMENT");
vm->reg[DCPU16_REG_EX] = acc >> 16;
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
ACCT_W(ev_b_addr);
}
/* */
} else {
vm->skip_ = 1;
- vm->cycle += 1;
+ dcpu16_cycle_inc(vm, 1);
}
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
}
OP_IMPL(ifc) {
/* */
} else {
vm->skip_ = 1;
- vm->cycle += 1;
+ dcpu16_cycle_inc(vm, 1);
}
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
}
OP_IMPL(ife) {
/* */
} else {
vm->skip_ = 1;
- vm->cycle += 1;
+ dcpu16_cycle_inc(vm, 1);
}
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
}
OP_IMPL(ifn) {
/* */
} else {
vm->skip_ = 1;
- vm->cycle++;
+ dcpu16_cycle_inc(vm, 1);
}
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
}
OP_IMPL(ifg) {
/* */
} else {
vm->skip_ = 1;
- vm->cycle++;
+ dcpu16_cycle_inc(vm, 1);
}
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
}
OP_IMPL(ifa) {
/* */
} else {
vm->skip_ = 1;
- vm->cycle += 1;
+ dcpu16_cycle_inc(vm, 1);
}
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
}
OP_IMPL(ifl) {
/* */
} else {
vm->skip_ = 1;
- vm->cycle++;
+ dcpu16_cycle_inc(vm, 1);
}
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
}
OP_IMPL(ifu) {
/* */
} else {
vm->skip_ = 1;
- vm->cycle += 1;
+ dcpu16_cycle_inc(vm, 1);
}
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
}
OP_IMPL(adx) {
else
vm->reg[DCPU16_REG_EX] = 0x0000;
- vm->cycle += 3;
+ dcpu16_cycle_inc(vm, 3);
ACCT_W(ev_b_addr);
}
else
vm->reg[DCPU16_REG_EX] = 0;
- vm->cycle += 3;
+ dcpu16_cycle_inc(vm, 3);
ACCT_W(ev_b_addr);
}
vm->reg[DCPU16_REG_I] += 1;
vm->reg[DCPU16_REG_J] += 1;
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
ACCT_W(ev_b_addr);
}
vm->reg[DCPU16_REG_I] -= 1;
vm->reg[DCPU16_REG_J] -= 1;
- vm->cycle += 2;
+ dcpu16_cycle_inc(vm, 2);
ACCT_W(ev_b_addr);
}
/* execute the next instruction */
void dcpu16_step(struct dcpu16 *vm) {
DCPU16_WORD opcode, b, a, instr_len, *b_data, *a_data;
- size_t i;
const struct opcode_entry *e;
if (!vm)
return;
- /* signal interested parties that a new cycle has ticked */
- TRACE("%s>> sending global cycle event", __func__);
- acct_event_(vm, DCPU16_ACCT_EV_CYCLE, vm->reg[DCPU16_REG_PC]);
-
- /* signal attached hardware */
- for (i = 0; i < vm->hw_table_entries_; i++) {
- if (vm->hw_table_[i].cycle) {
- TRACE("%s>> sending cycle to %s", __func__, vm->hw_table_[i].name_);
- vm->hw_table_[i].cycle(vm, vm->hw_table_[i].data);
- }
- }
-
instruction_decode_(vm->ram, vm->reg[DCPU16_REG_PC], &opcode, &b, &b_data, &a, &a_data, &instr_len);
/* consume what we decoded */
TRACE("++ SKIPPED %x words", instr_len);
if (opcode >= 0x10 && opcode <= 0x17) {
/* skipping a branch instruction? skip branch's skippable instruction as well */
- vm->cycle += 1;
+ dcpu16_cycle_inc(vm, 1);
} else {
vm->skip_ = 0;
}
DCPU16_WORD message;
message = interrupt_dequeue_(vm);
+ TRACE("%s>> %s interrupt IA:0x%04x message:0x%04x",
+ __func__,
+ vm->reg[DCPU16_REG_IA] ? "servicing" : "ignoring",
+ vm->reg[DCPU16_REG_IA],
+ message);
if (vm->reg[DCPU16_REG_IA]) {
- TRACE("%s>> servicing interrupt IA:0x%04x message:0x%04x \n", __func__, vm->reg[DCPU16_REG_IA], message);
/* then service the next interrupt */
vm->interrupts_deferred_ = 1;
vm->ram[--vm->reg[DCPU16_REG_SP]] = vm->reg[DCPU16_REG_PC];
vm->ram[--vm->reg[DCPU16_REG_SP]] = vm->reg[DCPU16_REG_A];
vm->reg[DCPU16_REG_PC] = vm->reg[DCPU16_REG_IA];
vm->reg[DCPU16_REG_A] = message;
- } else {
- TRACE("%s>> ignoring interrupt IA:0", __func__);
}
}
}
printf("\n");
printf("(0x%08llx) %2s:0x%04x %2s:0x%04x %2s:0x%04x %2s:0x%04x [%2s]:",
- vm->cycle,
+ vm->cycle_,
dcpu16_reg_names[DCPU16_REG_EX], vm->reg[DCPU16_REG_EX],
dcpu16_reg_names[DCPU16_REG_SP], vm->reg[DCPU16_REG_SP],
dcpu16_reg_names[DCPU16_REG_PC], vm->reg[DCPU16_REG_PC],
memset(vm->reg, 0, sizeof vm->reg);
memset(vm->ram, 0, sizeof vm->ram);
- vm->cycle = 0;
+ vm->cycle_ = 0;
acct_event_(vm, DCPU16_ACCT_EV_RESET, 0);
}
#include <stdio.h>
#include <string.h>
#include <errno.h>
+#include <sys/time.h>
#ifdef HAVE_LIBPNG
#include <setjmp.h>
.data = (struct lem1802_ *)NULL
};
+#define LEM1802_POWER_ON_CYCLES 100000 /* this should vary by, let us say, 10% */
+
#define PIX_X 160 /* pixels in display */
#define PIX_Y 128 /* pixels in display */
#define PIX_BORDER 16 /* border pixels from edge to first tile */
};
struct lem1802_ {
- long long cycle_activated; /* for tracking 'turn on delay' */
+ long long cycle_activated; /* running since */
+ long long cycles_until_active_; /* for tracking power-up delay */
DCPU16_WORD video_base;
DCPU16_WORD font_base;
unsigned int blink_tally_; /* tick */
unsigned int blink_state;
+ enum cycle_state_ {
+ CYCLE_IDLE,
+ CYCLE_COPY_TO_RAM,
+ } cycle_state_;
+ const DCPU16_WORD *cycle_state_copy_src_ptr_;
+ DCPU16_WORD cycle_state_copy_dst_addr_;
+ size_t cycle_state_copy_words_;
+
int (*render)(void *, struct pixel_ *, size_t, size_t);
void *renderer_data;
};
+static
+long long power_on_cycles_(void) {
+ struct tv;
+ long long r = 0;
+
+#if WANT_DELAY_START
+ gettimeofday(&tv, NULL);
+ r += LEM1802_POWER_ON_CYCLES - (LEM1802_POWER_ON_CYCLES / 10);
+ r += tv.tv_usec % (LEM1802_POWER_ON_CYCLES / 5);
+#endif
+
+ return r;
+}
+
static inline
void pixel_color_(struct pixel_ *pix, DCPU16_WORD color) {
unsigned char x;
TRACE("%s>> video_base:0x%04x", __func__, display->video_base);
#endif
+ if (display->cycles_until_active_) {
+ /* show cute power-up sequence.. */
+ memset(display->pixbuf, 0, PIX_X * PIX_Y * sizeof *display->pixbuf);
+ return;
+ }
+
if (display->video_base == 0) {
- /* disconnected, blank display. static might be fun, too */
+ /* disconnected, blank display */
memset(display->pixbuf, 0, PIX_X * PIX_Y * sizeof *display->pixbuf);
return;
}
(void)pixbuf;
/* derp */
- rfbMarkRectAsModified(s, 0, 0, x, y);
+ if (s)
+ rfbMarkRectAsModified(s, 0, 0, x, y);
TRACE("%s>>", __func__);
display->blink_tally_ = 0;
display->blink_state = 0;
+ display->cycle_state_ = 0;
+
#if DEBUG
vm->trace_cb_("%s>>", __func__);
#endif /* DEBUG */
(void)vm;
/*
- maybe just step through video memory (if set)
- one word per clock..? could just cheat and
- use accounting callbacks..
-
+ for more cycle-accuracy, could step through video memory, if set,
+ one word per clock..
for now just count cycles and issue a full refresh/render
every so often
*/
lem1802_pixbuf_refresh_full_(display, vm->ram);
display->render(display->renderer_data, display->pixbuf, PIX_X, PIX_Y);
}
+
+ switch (display->cycle_state_) {
+ case CYCLE_IDLE:
+ break;
+
+ case CYCLE_COPY_TO_RAM:
+ TRACE("%s>> copy_to_ram words:%zu src:%p dst_addr:0x%04x",
+ __func__,
+ display->cycle_state_copy_words_,
+ display->cycle_state_copy_src_ptr_,
+ display->cycle_state_copy_dst_addr_);
+ vm->ram[display->cycle_state_copy_dst_addr_] = *display->cycle_state_copy_src_ptr_;
+ display->cycle_state_copy_dst_addr_++;
+ display->cycle_state_copy_src_ptr_++;
+ display->cycle_state_copy_words_--;
+ if (display->cycle_state_copy_words_ == 0) {
+ display->cycle_state_ = CYCLE_IDLE;
+ }
+ break;
+ }
+
+ if (display->cycles_until_active_) {
+ display->cycles_until_active_--;
+ if (display->cycles_until_active_ == 0) {
+ TRACE("%s>> display now active", __func__);
+ }
+ }
}
static
struct lem1802_ *display = (struct lem1802_ *)data;
DCPU16_WORD reg_a = vm->reg[DCPU16_REG_A];
DCPU16_WORD reg_b = vm->reg[DCPU16_REG_B];
- size_t i;
TRACE("%s>> A:0x%04x B:0x%04x", __func__, reg_a, reg_b);
switch (reg_a) {
case 0: /* MEM_MAP_SCREEN */
if (display->cycle_activated == 0 && reg_b) {
- display->cycle_activated = vm->cycle;
+ display->cycle_activated = vm->cycle_;
+ display->cycles_until_active_ = power_on_cycles_();
}
display->video_base = reg_b;
if (reg_b == 0)
break;
case 4: /* MEM_DUMP_FONT */
- for (i = 0; i < 128 ; i++) {
- vm->ram[reg_b] = chargen_4x8_glyphs[reg_b][0] << 8;
- vm->ram[reg_b] |= chargen_4x8_glyphs[reg_b][1];
- reg_b += 1;
- vm->ram[reg_b] = chargen_4x8_glyphs[reg_b][2] << 8;
- vm->ram[reg_b] |= chargen_4x8_glyphs[reg_b][3];
- reg_b += 1;
- }
- vm->cycle += 256;
+ display->cycle_state_copy_src_ptr_ = (DCPU16_WORD *)chargen_4x8_glyphs;
+ display->cycle_state_copy_dst_addr_ = reg_b;
+ display->cycle_state_copy_words_ = 256;
+ display->cycle_state_ = CYCLE_COPY_TO_RAM;
+ dcpu16_cycle_inc(vm, 256);
break;
case 5: /* MEM_DUMP_PALETTE */
- for (i = 0; i < 16; i++) {
- vm->ram[reg_b] = palette_default_[i];
- reg_b += 1;
- }
- vm->cycle += 16;
+ display->cycle_state_copy_src_ptr_ = palette_default_;
+ display->cycle_state_copy_dst_addr_ = reg_b;
+ display->cycle_state_copy_words_ = 16;
+ display->cycle_state_ = CYCLE_COPY_TO_RAM;
+ dcpu16_cycle_inc(vm, 16);
break;
}
}