-
Notifications
You must be signed in to change notification settings - Fork 2
/
bf64.c
115 lines (93 loc) · 2.42 KB
/
bf64.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <cbm.h>
#include "menu.h"
#include "reader.h"
#include "compile.h"
#include "transpile.h"
static void bf_input(bool transpile_mode)
{
char c;
unsigned int len = 0;
clrscr();
printf("Type your Brainfuck program below,\r\n"
"finish with RUN/STOP:\r\n\n");
reader_init ();
while (c = reader_get ())
{
transpile_mode ? transpile (c) : compile (c);
++len;
}
printf("\r\n");
printf("Program length: %d\r\n", len);
}
static void bf_file(bool transpile_mode, const char *filename, bool show)
{
int c;
FILE *f;
unsigned int len = 0;
if (show)
clrscr();
printf("Loading from %s\n\n", filename);
f = fopen (filename, "r");
while ((c = fgetc(f)) != EOF)
{
if (show)
putchar(c);
transpile_mode ? transpile (c) : compile(c);
++len;
}
fclose (f);
if (show)
printf("\n");
printf("\nProgram source length: %d\n", len);
}
static void bf_compile (const char *filename)
{
unsigned int prog_origin;
unsigned int mem_origin;
printf ("Memory start address? ");
mem_origin = read_hex (0x3000);
printf ("Program start address? ");
prog_origin = read_hex (0x5000);
compile_init (mem_origin, prog_origin);
if (filename)
bf_file (false, filename, true);
else
bf_input (false);
compile_finish ();
printf("Run with\nsys %u\n", prog_origin);
}
static void bf_transpile (const char *filename)
{
unsigned int prog_origin, prog_end;
printf ("Program start address? ");
prog_origin = read_hex (0xc000);
transpile_init (prog_origin);
if (filename)
bf_file (true, filename, true);
else
bf_input (true);
prog_end = transpile_finish ();
*((unsigned int*)0x002b) = prog_origin+1;
*((unsigned int*)0x002d) = prog_end+1;
*((unsigned int*)0x002f) = prog_end+1;
*((unsigned int*)0x0031) = prog_end+1;
*((unsigned int*)0x0037) = prog_origin + 0x1000;
}
int main()
{
char *filename;
bool transpile;
clrscr();
printf ("Brainfuck 64\n(C) 2013 Gergo Erdi\nhttp://gergo.erdi.hu/\n\n\n");
filename = main_menu ();
transpile = mode_menu () == 'B';
transpile ? bf_transpile(filename) : bf_compile(filename);
// Clear STOP status
*((char*)0x0091) = 0;
return EXIT_SUCCESS;
}