projects
/
dcpu16
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
better shell loop
[dcpu16]
/
dcpu16.c
diff --git
a/dcpu16.c
b/dcpu16.c
index 6f5617ccff86cdd76accf814a68d77bbb011edbe..2a59d24e1cca31f5e161163fc8578d14fdcdc968 100644
(file)
--- a/
dcpu16.c
+++ b/
dcpu16.c
@@
-25,6
+25,8
@@
* TODO
* drop checks for assigning to literals -- it won't affect anything anyhow
* debug short literal decoding
* TODO
* drop checks for assigning to literals -- it won't affect anything anyhow
* debug short literal decoding
+ * add callbacks queues for set/get, attach your own filters
+ * such as a display
*/
static const char * const src_id_ = "$Id$";
*/
static const char * const src_id_ = "$Id$";
@@
-33,9
+35,7
@@
static const char * const src_id_ = "$Id$";
static const char regnames_[] = "ABCXYZIJ";
static const char regnames_[] = "ABCXYZIJ";
-
-static unsigned int trace_mode_ = 0; /* spew overly verbose internals */
-
+/* some default warning and debug reporting functions, which can be overridden by clients */
#define WARN(...) do { if (warn_cb_) warn_cb_(__VA_ARGS__); } while (0)
static inline void warn_(char *fmt, ...) __attribute__((format(printf, 1, 2)));
static inline
#define WARN(...) do { if (warn_cb_) warn_cb_(__VA_ARGS__); } while (0)
static inline void warn_(char *fmt, ...) __attribute__((format(printf, 1, 2)));
static inline
@@
-54,7
+54,8
@@
void dcpu16_warn_cb_set(void (*fn)(char *fmt, ...)) {
warn_cb_ = fn;
}
warn_cb_ = fn;
}
-#define TRACE(...) do { if (trace_cb_ && trace_mode_) trace_cb_(__VA_ARGS__); } while (0)
+#ifdef DEBUG
+#define TRACE(...) do { if (trace_cb_) trace_cb_(__VA_ARGS__); } while (0)
static inline void trace_(char *fmt, ...) __attribute__((format(printf, 1, 2)));
static inline
void trace_(char *fmt, ...) {
static inline void trace_(char *fmt, ...) __attribute__((format(printf, 1, 2)));
static inline
void trace_(char *fmt, ...) {
@@
-67,12
+68,20
@@
void trace_(char *fmt, ...) {
fprintf(stdout, "\n");
fflush(stdout);
}
fprintf(stdout, "\n");
fflush(stdout);
}
-static void (*trace_cb_)(char *fmt, ...) = trace_;
+#else /* DEBUG */
+#define TRACE(...) do {} while(0)
+#endif /* DEBUG */
+static void (*trace_cb_)(char *fmt, ...) =
+#ifdef DEBUG
+ trace_
+#else /* DEBUG */
+ NULL
+#endif
+ ;
void dcpu16_trace_cb_set(void (*fn)(char *fmt, ...)) {
trace_cb_ = fn;
}
void dcpu16_trace_cb_set(void (*fn)(char *fmt, ...)) {
trace_cb_ = fn;
}
-
/* value_decode_
* sets *v to be the destination of the value
* workv is buffer to use to accumulate literal value before use
/* value_decode_
* sets *v to be the destination of the value
* workv is buffer to use to accumulate literal value before use
@@
-305,7
+314,7
@@
OP_IMPL(add) {
if (val_a < 0x1f) {
*a = acc;
}
if (val_a < 0x1f) {
*a = acc;
}
- d->o =
acc >> 16
;
+ d->o =
(acc > 0xffff)
;
d->cycle += 2;
}
d->cycle += 2;
}
@@
-317,9
+326,9
@@
OP_IMPL(sub) {
if (val_a < 0x1f) {
*a = acc;
if (val_a < 0x1f) {
*a = acc;
- }
- d->o = acc >> 16;
+ d->o = (acc > 0xffff);
+ }
d->cycle += 2;
}
d->cycle += 2;
}
@@
-543,7
+552,7
@@
void dcpu16_disassemble_print(struct dcpu16 *d, WORD addr) {
assert(a <= 0x3f);
assert(b <= 0x3f);
assert(a <= 0x3f);
assert(b <= 0x3f);
- printf("
next instr 0x%04x: %04x", addr
, d->ram[addr]);
+ printf("
%04x"
, d->ram[addr]);
if (opcode != 0)
{
if (opcode != 0)
{
@@
-574,8
+583,6
@@
void dcpu16_disassemble_print(struct dcpu16 *d, WORD addr) {
}
dump_value(b, d->ram[addr + 1]);
}
dump_value(b, d->ram[addr + 1]);
-
- printf("\n");
}
void dcpu16_step(struct dcpu16 *d) {
}
void dcpu16_step(struct dcpu16 *d) {
@@
-603,26
+610,42
@@
void dcpu16_step(struct dcpu16 *d) {
void dcpu16_state_print(struct dcpu16 *d) {
unsigned int i;
void dcpu16_state_print(struct dcpu16 *d) {
unsigned int i;
- printf("
---- cycle:0x%08llx %2s:0x%04x %2s:0x%04x %2s:0x%04x\n
",
+ printf("
(0x%08llx) %2s:0x%04x %2s:0x%04x %2s:0x%04x [%2s]:
",
d->cycle,
d->cycle,
- "
PC", d->pc
,
+ "
O", d->o
,
"SP", d->sp,
"SP", d->sp,
- "O", d->o);
- printf(" ");
+ "PC", d->pc,
+ "PC");
+
+ dcpu16_disassemble_print(d, d->pc);
+ printf("\n ");
+
for (i = 0; i < 8; i++)
printf(" %c:0x%04x", regnames_[i], d->reg[i]);
printf("\n");
}
for (i = 0; i < 8; i++)
printf(" %c:0x%04x", regnames_[i], d->reg[i]);
printf("\n");
}
-void dcpu16_dump_ram(struct dcpu16 *d, WORD start, WORD stop) {
+/* dcpu16_dump_ram
+ * print raw ram contents from start to stop
+ */
+void dcpu16_dump_ram(struct dcpu16 *d, WORD start, WORD end) {
unsigned int i, j;
const unsigned int n = 8; /* words per line */
unsigned int i, j;
const unsigned int n = 8; /* words per line */
- for (i = start, j = 0; i <=
stop
; i++, j++) {
+ for (i = start, j = 0; i <=
end
; i++, j++) {
if (j % n == 0)
printf("0x%04x:\t", i);
printf(" %04x%s", d->ram[i], (j % n) == (n - 1) ? "\n" : "");
}
if (j % n == 0)
printf("0x%04x:\t", i);
printf(" %04x%s", d->ram[i], (j % n) == (n - 1) ? "\n" : "");
}
+ if ((j % n) != (n - 1))
+ printf("\n");
+}
+
+/* dcpu16_reset
+ * resets a dcpu16 instance to initial state
+ */
+void dcpu16_reset(struct dcpu16 *d) {
+ memset(d, 0, sizeof *d);
}
/* dcpu16_new
}
/* dcpu16_new