kernels: merge bf to demo

This commit is contained in:
Zihao Yu 2023-07-22 22:10:07 +08:00
parent c5bcf378e8
commit 4707aefcc5
4 changed files with 28 additions and 11 deletions

View file

@ -1,3 +0,0 @@
NAME = bf
SRCS = bf.c
include $(AM_HOME)/Makefile

View file

@ -6,18 +6,35 @@
*/ */
#include <am.h> #include <am.h>
#include <klib.h> #include <klib.h>
#include <io.h>
const char *prog = static const char *prog =
#include "mandelbrot.h" #include "mandelbrot.h"
; ;
struct bfi { char cmd; struct bfi *next, *jmp; }; struct bfi { char cmd; struct bfi *next, *jmp; };
struct mem { char val; struct mem *next, *prev; }; struct mem { char val; struct mem *next, *prev; };
int main() { static inline void putch2(char c) {
static int x = 0, y = 0;
if (c == '\n') {
y ++;
x = 0;
} else {
if (x % 2 == 0) {
print_char(c, y, x / 2);
screen_refresh();
}
x ++;
}
}
void bf() {
int ch; int ch;
struct bfi *p=0, *n=0, *j=0, *pgm = 0; struct bfi *p=0, *n=0, *j=0, *pgm = 0;
struct mem *m = malloc(sizeof(*m)); struct mem *m = malloc(sizeof(*m));
memset(m, 0, sizeof(*m));
screen_clear();
/* /*
* For each character, if it's a valid BF command add it onto the * For each character, if it's a valid BF command add it onto the
@ -35,8 +52,9 @@ int main() {
ch == ',' || ch == '.' || ch == '[' || (ch == ']' && j)) { ch == ',' || ch == '.' || ch == '[' || (ch == ']' && j)) {
if ((n = malloc(sizeof(*n))) == NULL) { if ((n = malloc(sizeof(*n))) == NULL) {
printf("malloc failed! exiting...\n"); printf("malloc failed! exiting...\n");
return 1; halt(1);
} }
memset(n, 0, sizeof(*n));
if (p) p->next = n; else pgm = n; if (p) p->next = n; else pgm = n;
n->cmd = ch; p = n; n->cmd = ch; p = n;
if (n->cmd == '[') { n->jmp=j; j = n; } if (n->cmd == '[') { n->jmp=j; j = n; }
@ -54,28 +72,27 @@ int main() {
switch(n->cmd) { switch(n->cmd) {
case '+': m->val++; break; case '+': m->val++; break;
case '-': m->val--; break; case '-': m->val--; break;
case '.': putch(m->val); break; case '.': putch2(m->val); break;
case ',': if((ch=*(s ++))!='\0') m->val=ch; break; case ',': if((ch=*(s ++))!='\0') m->val=ch; break;
case '[': if (m->val == 0) n=n->jmp; break; case '[': if (m->val == 0) n=n->jmp; break;
case ']': if (m->val != 0) n=n->jmp; break; case ']': if (m->val != 0) n=n->jmp; break;
case '<': case '<':
if (!(m=m->prev)) { if (!(m=m->prev)) {
printf("Hit start of tape\n"); printf("Hit start of tape\n");
return 1; halt(1);
} }
break; break;
case '>': case '>':
if (m->next == 0) { if (m->next == 0) {
if ((m->next = malloc(sizeof(*m))) == NULL) { if ((m->next = malloc(sizeof(*m))) == NULL) {
printf("malloc failed! exiting...\n"); printf("malloc failed! exiting...\n");
return 1; halt(1);
} }
memset(m->next, 0, sizeof(*m));
m->next->prev = m; m->next->prev = m;
} }
m=m->next; m=m->next;
break; break;
} }
} }
return 0;
} }

View file

@ -9,6 +9,7 @@ void game_of_life();
void aclock(); void aclock();
void cmatrix(); void cmatrix();
void donut(); void donut();
void bf();
int main(const char *args) { int main(const char *args) {
ioe_init(); ioe_init();
@ -21,6 +22,7 @@ int main(const char *args) {
case '5': aclock(); break; case '5': aclock(); break;
case '6': cmatrix(); break; case '6': cmatrix(); break;
case '7': donut(); break; case '7': donut(); break;
case '8': bf(); break;
default: default:
printf("Usage: make run mainargs=*\n"); printf("Usage: make run mainargs=*\n");
printf(" 1: ant\n"); printf(" 1: ant\n");
@ -30,6 +32,7 @@ int main(const char *args) {
printf(" 5: aclock\n"); printf(" 5: aclock\n");
printf(" 6: cmatrix\n"); printf(" 6: cmatrix\n");
printf(" 7: donut\n"); printf(" 7: donut\n");
printf(" 8: bf\n");
} }
printf("Press Q to Exit\n"); printf("Press Q to Exit\n");