2019-05-17 09:33:20 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2019-12-02 16:35:49 -05:00
|
|
|
#include "../emul.h"
|
2019-05-17 09:33:20 -04:00
|
|
|
|
|
|
|
/* runbin loads binary from stdin directly in memory address 0 then runs it
|
|
|
|
* until it halts. The return code is the value of the register A at halt time.
|
|
|
|
*/
|
|
|
|
|
2019-12-23 15:41:25 -05:00
|
|
|
static void iowr_stderr(uint8_t val)
|
|
|
|
{
|
|
|
|
fputc(val, stderr);
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:33:20 -04:00
|
|
|
int main()
|
|
|
|
{
|
2019-12-02 16:35:49 -05:00
|
|
|
Machine *m = emul_init();
|
2019-12-23 15:41:25 -05:00
|
|
|
m->iowr[0] = iowr_stderr;
|
2019-05-17 09:33:20 -04:00
|
|
|
// read stdin in mem
|
|
|
|
int i = 0;
|
|
|
|
int c = getchar();
|
|
|
|
while (c != EOF) {
|
2019-12-02 16:35:49 -05:00
|
|
|
m->mem[i] = c & 0xff;
|
2019-05-17 09:33:20 -04:00
|
|
|
i++;
|
|
|
|
c = getchar();
|
|
|
|
}
|
2019-05-28 09:57:29 -04:00
|
|
|
if (!i) {
|
|
|
|
fprintf(stderr, "No input, aborting\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-12-02 16:35:49 -05:00
|
|
|
emul_loop();
|
2019-12-23 15:41:25 -05:00
|
|
|
if (m->cpu.R1.wr.HL)
|
2019-12-02 16:35:49 -05:00
|
|
|
return m->cpu.R1.br.A;
|
2019-05-17 09:33:20 -04:00
|
|
|
}
|
|
|
|
|