From b664c3c4fe88c00ee4a4b32828b8edb4d5317df2 Mon Sep 17 00:00:00 2001 From: Milton Maldonado Jr Date: Sat, 23 May 2020 20:39:59 -0300 Subject: [PATCH] V1.1.0 - May 23, 2020: - Created the Ex-Machina Autoloader. It simulates the needed keypresses to load a demo program into Ben's computer's RAM. See README.TXT for more info. --- README.TXT | 17 ++++++- src/board.c | 38 +++++++++++++++- src/board.h | 3 ++ src/computer/computer.c | 6 +++ src/computer/exmachina.c | 95 ++++++++++++++++++++++++++++++++++++++++ src/computer/exmachina.h | 13 ++++++ src/update.h | 4 +- testprogram.txt | 20 ++++----- 8 files changed, 180 insertions(+), 16 deletions(-) create mode 100644 src/computer/exmachina.c create mode 100644 src/computer/exmachina.h diff --git a/README.TXT b/README.TXT index f1d7a73..2860d12 100644 --- a/README.TXT +++ b/README.TXT @@ -7,7 +7,7 @@ // SEE LICENSE AT https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt //////////////////////////////////////////////////////////////////////////////// - Version 1.0.8 + Version 1.1.0 May 23, 2020 INTRO @@ -101,7 +101,11 @@ for that. two ways: a "Deus ex machina" way (that "magically" transfers data between the RAM and disk files, sort of a cheating), or a more beautiful way where a simulated hardware could transfer RAM data from/to a simulated EEPROM (and then -its contents could be loaded/saved from disk). Maybe I will do it someday... +its contents could be loaded/saved from disk) - (EDIT) I created the +'exmachina.c' module that sort of implements this idea, but not dynamically from +disk. The program to be loaded to the RAM is hard-coded, and to change it will +require a project rebuild. This code simulates the user actions of manually +inputting the test program, and can be improved at will. - The James Bates variant (https://www.youtube.com/watch?v=gqYFT6iecHw) and others. I didn't check whether the simulation engine can run the Bates' version @@ -191,3 +195,12 @@ hinted within brackets. (they are older tests not related to Ben's computer simulation). You can play with them by editing 'main.c'. + V1.1.0 - May 23, 2020: + + - Created the Ex-Machina Autoloader. It simulates the needed keypresses to + load a demo program into Ben's computer's RAM. It's simply a thread that + injects fake keypresses into the engine, and is implemented into the file + 'exmachina.c'. There the demo program can be changed to whatever program + the user wants, and it will work as long as it is compatible with the + Ben's computer architecture! + diff --git a/src/board.c b/src/board.c index e080c0f..f950c12 100644 --- a/src/board.c +++ b/src/board.c @@ -350,11 +350,12 @@ void restart_handlers(board_ctx_t *bctx) FD_ZERO (&bctx->readfds); FD_SET(0,&bctx->readfds); + FD_SET(bctx->pipekeys[0],&bctx->readfds); tv.tv_sec = 0; tv.tv_usec = 100; // 100 us - select (1,&bctx->readfds,NULL,NULL,&tv); + select (1+bctx->pipekeys[0],&bctx->readfds,NULL,NULL,&tv); } //////////////////////////////////////////////////////////////////////////////// @@ -363,6 +364,12 @@ int received_key(board_ctx_t *bctx) return (FD_ISSET(0,&bctx->readfds)); } +//////////////////////////////////////////////////////////////////////////////// +int received_key_simu(board_ctx_t *bctx) +{ + return (FD_ISSET(bctx->pipekeys[0],&bctx->readfds)); +} + //////////////////////////////////////////////////////////////////////////////// int read_key(board_ctx_t *bctx) { @@ -372,6 +379,20 @@ int read_key(board_ctx_t *bctx) return key; } +//////////////////////////////////////////////////////////////////////////////// +int read_key_simu(board_ctx_t *bctx) +{ + int key; + read(bctx->pipekeys[0], &key, sizeof(int)); + return key; +} + +//////////////////////////////////////////////////////////////////////////////// +void board_write_key(board_ctx_t *bctx, int key) +{ + write(bctx->pipekeys[1], &key, sizeof(int)); +} + //////////////////////////////////////////////////////////////////////////////// void sigterm_handler(int sig){ @@ -1004,6 +1025,8 @@ int board_run(board_ctx_t *ctx, event_context_t *ec, board_object *board){ ctx->board = board; + pipe(ctx->pipekeys); + if (!board) return -2; if (board->type != BOARD) @@ -1090,9 +1113,20 @@ int board_run(board_ctx_t *ctx, event_context_t *ec, board_object *board){ restart_handlers(ctx); + bool_t has_key = 0; + int key; + if (received_key(ctx)){ + key = read_key(ctx); + has_key = 1; + } + + if (received_key_simu(ctx)){ + key = read_key_simu(ctx); + has_key = 1; + } - int key = read_key(ctx); + if (has_key){ switch(key){ diff --git a/src/board.h b/src/board.h index a3f7855..54e0e83 100644 --- a/src/board.h +++ b/src/board.h @@ -78,6 +78,7 @@ typedef struct { pthread_t refthread; bool_t refresh_run; int piperefresh[2]; + int pipekeys[2]; bool_t focustable_done; int num_focuseable_boards; @@ -117,4 +118,6 @@ void board_set_refresh(board_ctx_t *ctx); board_ctx_t *board_init(void); +void board_write_key(board_ctx_t *bctx, int key); + #endif /* BOARD_H_ */ diff --git a/src/computer/computer.c b/src/computer/computer.c index 2d71a99..c240d0b 100644 --- a/src/computer/computer.c +++ b/src/computer/computer.c @@ -249,6 +249,12 @@ void computer_sim(){ //////////////// +#include "exmachina.h" + + pthread_t sim_thread; + pthread_create(&sim_thread, NULL, exmachina_thread, ctx); + pthread_detach(sim_thread); + board_run(ctx, ec, mainboard); logger_end(ec); diff --git a/src/computer/exmachina.c b/src/computer/exmachina.c new file mode 100644 index 0000000..c0c7812 --- /dev/null +++ b/src/computer/exmachina.c @@ -0,0 +1,95 @@ +/* + * exmachina.c + * + * Created on: 23 de mai de 2020 + * Author: milton + */ + +#include +#include + +#include "board.h" + +const uint8_t demoprog[] = { + + 0x00, /*OUT*/ 0xE0, + 0x01, /*ADD 15*/ 0x2F, + 0x02, /*JC 4*/ 0x74, + 0x03, /*JMP 0*/ 0x60, + 0x04, /*SUB 15*/ 0x3F, + 0x05, /*OUT*/ 0xE0, + 0x06, /*JZ 0*/ 0x80, + 0x07, /*JMP 4*/ 0x64, + 0x0F, /*1*/ 0x01, +}; + +const int LINES_PROG = sizeof(demoprog)/2; + +//////////////////////////////////////////////////////////////////////////////// +void write_key(board_ctx_t *bctx, int key){ + + board_write_key(bctx, key); + usleep(100000); +} + +//////////////////////////////////////////////////////////////////////////////// +void *exmachina_thread(void *args){ + + board_ctx_t *bctx = args; + usleep(1000000); + + uint8_t addr = 0; + uint8_t data = 0; + + int i; + + for (i = 0; i < LINES_PROG; i++){ + + uint8_t newaddr = demoprog[2*i]; + uint8_t newdata = demoprog[2*i+1]; + + uint8_t deltaaddr = addr ^ newaddr; + if (deltaaddr & 0x01){ + write_key(bctx, 'l'); + } + + if (deltaaddr & 0x02){ + write_key(bctx, 'k'); + } + if (deltaaddr & 0x04){ + write_key(bctx, 'j'); + } + if (deltaaddr & 0x08){ + write_key(bctx, 'h'); + } + + uint8_t deltadata = data ^ newdata; + + int j; + for (j = 0; j < 8; j++){ + + if (deltadata & (1 << j)){ + + int key = '0' + j; + write_key(bctx, key); + } + } + + addr = newaddr; + data = newdata; + + write_key(bctx, 'w'); + write_key(bctx, 'w'); + } + + write_key(bctx, 'p'); + write_key(bctx, KEY_F(2)); + + write_key(bctx, 'r'); + write_key(bctx, 'r'); + + for (i = 0; i < 9; i++) + write_key(bctx, KEY_F(12)); + + return NULL; +} diff --git a/src/computer/exmachina.h b/src/computer/exmachina.h new file mode 100644 index 0000000..1ae7287 --- /dev/null +++ b/src/computer/exmachina.h @@ -0,0 +1,13 @@ +/* + * exmachina.h + * + * Created on: 23 de mai de 2020 + * Author: milton + */ + +#ifndef SRC_COMPUTER_EXMACHINA_H_ +#define SRC_COMPUTER_EXMACHINA_H_ + +void *exmachina_thread(void *args); + +#endif /* SRC_COMPUTER_EXMACHINA_H_ */ diff --git a/src/update.h b/src/update.h index cd69182..7bf7e35 100644 --- a/src/update.h +++ b/src/update.h @@ -90,7 +90,7 @@ void part_destroy(void **part); #define DESTROY(X) part_destroy((void**)&X) #define SW_VERSION 1 -#define SW_REVISION 0 -#define SW_MINOR 8 +#define SW_REVISION 1 +#define SW_MINOR 0 #endif /* UPDATE_H_ */ diff --git a/testprogram.txt b/testprogram.txt index 8ce23b8..5428448 100644 --- a/testprogram.txt +++ b/testprogram.txt @@ -1,10 +1,10 @@ - -0 OUT E0 -1 ADD 15 2F -2 JC 4 74 -3 JMP 0 60 -4 SUB 15 3F -5 OUT E0 -6 JZ 0 80 -7 JMP 4 64 -15 1 01 +ADDR MNEMONIC CODE +00 OUT E0 +01 ADD 15 2F +02 JC 4 74 +03 JMP 0 60 +04 SUB 15 3F +05 OUT E0 +06 JZ 0 80 +07 JMP 4 64 +0F 1 01