Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
  4.  
  5. void setup() {
  6. Serial.begin(9600);
  7. lcd.begin(16, 2);
  8. setProgram();
  9. compile();
  10. lcd.setCursor(0,1);
  11. lcd.write("ACC=");
  12. lcd.setCursor(8,1);
  13. lcd.write("BAK=");
  14. }
  15.  
  16. const int MAX_LINES = 15;
  17.  
  18. typedef struct {
  19. char *id;
  20. char *cmd;
  21. char *arg1;
  22. char *arg2;
  23. } line;
  24.  
  25. line program_lines[MAX_LINES];
  26.  
  27. static char program[MAX_LINES][16];
  28.  
  29. char mode[2] = "-";
  30.  
  31. void setProgram(){
  32. for(int i=0;i<MAX_LINES;i++){
  33. sprintf(program[i], "");
  34. }
  35. int i=0;
  36. //sprintf(program[i++], "MOV UP ACC");
  37. //sprintf(program[i++], "ADD ACC");
  38. //sprintf(program[i++], "SUB UP");
  39. //sprintf(program[i++], "MOV ACC DOWN");
  40. sprintf(program[i++], "MOV 10 ACC");
  41. sprintf(program[i++], "SWP");
  42. sprintf(program[i++], "MOV 5 ACC");
  43. sprintf(program[i++], "L:SUB 1");
  44. sprintf(program[i++], "SWP");
  45. sprintf(program[i++], "JEZ E");
  46. sprintf(program[i++], "JMP L");
  47. sprintf(program[i++], "E:SWP");
  48. sprintf(program[i++], "MOV ACC DOWN");
  49. }
  50.  
  51. void debug(char *str){
  52. Serial.write("\"");
  53. Serial.write(str);
  54. Serial.write("\"\n");
  55. }
  56.  
  57. void compile(){
  58. for(int i=0;i<MAX_LINES;i++){
  59. line *line = &program_lines[i];
  60. char *source = malloc(17);
  61. strcpy(source, program[i]);
  62. //Label
  63. char *found = strstr(source, ":");
  64. if(found == NULL){
  65. line->id = NULL;
  66. }else{
  67. line->id = source;
  68. *found = '\0';
  69. source = found+1;
  70. }
  71. //command
  72. found = strstr(source, " ");
  73. if(found == NULL){
  74. line->cmd = source; //no args
  75. line->arg1 = NULL;
  76. line->arg2 = NULL;
  77. continue;
  78. }else{
  79. line->cmd = source;
  80. *found = '\0';
  81. source = found+1;
  82. }
  83. //arg 1
  84. found = strstr(source, " ");
  85. if(found == NULL){
  86. line->arg1 = source; //no 2nd arg
  87. line->arg2 = NULL;
  88. continue;
  89. }else{
  90. line->arg1 = source;
  91. *found = '\0';
  92. line->arg2 = found+1;
  93. }
  94. }
  95. }
  96.  
  97. void spitOutCompiled(){
  98. debug("All:");
  99. for(int i=0;i<MAX_LINES;i++){
  100. line *line = &program_lines[i];
  101. debug(line->id);
  102. debug(line->cmd);
  103. debug(line->arg1);
  104. debug(line->arg2);
  105. debug("---");
  106. }
  107. }
  108.  
  109. int pc; //program counter
  110. int acc; //accumulator register
  111. int bck; //backup register
  112.  
  113. void lcdWriteDecimal(int value){
  114. static char temp[6];
  115. sprintf(&temp[0], "%03d ", value); //"-999 "
  116. temp[4]='\0';
  117. lcd.write(temp);
  118. }
  119.  
  120. void displayCurrentCommand(){
  121. static char temp[17];
  122. sprintf(temp, "%-16s", program[pc]);
  123. lcd.setCursor(0,0);
  124. lcd.write(temp);
  125. }
  126.  
  127. void displayRegisterValues(){
  128. lcd.setCursor(4,1);
  129. lcdWriteDecimal(acc);
  130. lcd.setCursor(12,1);
  131. lcdWriteDecimal(bck);
  132. }
  133.  
  134. void displayTheMode(){
  135. lcd.setCursor(15, 0);
  136. lcd.write(mode);
  137. }
  138.  
  139. void moveNextCommand(){
  140. do {
  141. pc=(pc+1)%MAX_LINES;
  142. } while(program_lines[pc].cmd == NULL);
  143. }
  144.  
  145. bool startsWith(const char *str, const char *pre) {
  146. return strncmp(pre, str, strlen(pre)) == 0;
  147. }
  148.  
  149. int serialReadInt(){
  150. setReadMode();
  151. Serial.write("Input required:");
  152. static char readString[15];
  153. int idx = 0;
  154. while (idx < 2 || readString[idx-1] != '\n') {
  155. delay(3); //delay to allow buffer to fill
  156. if (Serial.available() >0) {
  157. readString[idx++] = Serial.read();
  158. }
  159. }
  160. readString[idx-1] = '\0';
  161. debug(readString);
  162. return atoi(readString);
  163. }
  164.  
  165. int evaluate(char *arg){
  166. if(strcmp(arg, "UP")==0){
  167. return clamp(serialReadInt());
  168. }
  169. if(strcmp(arg, "ACC")==0){
  170. return acc;
  171. }
  172. return clamp(atoi(arg));
  173. }
  174.  
  175. void executeMov(){
  176. line *line = &program_lines[pc];
  177. int inputValue = evaluate(line->arg1);
  178. char *dest = line->arg2;
  179. if(strcmp(dest, "ACC")==0){
  180. acc = inputValue;
  181. return;
  182. }
  183. if(strcmp(dest, "DOWN")==0){
  184. setWriteMode();
  185. char temp[5];
  186. sprintf(temp, "%03d\n", inputValue);
  187. Serial.write(temp);
  188. return;
  189. }
  190. }
  191.  
  192. void setRunMode1(){
  193. mode[0] = '>';
  194. displayTheMode();
  195. }
  196.  
  197. void setRunMode2(){
  198. mode[0] = 255;
  199. displayTheMode();
  200. }
  201.  
  202. void setReadMode(){
  203. mode[0] = 'R';
  204. displayTheMode();
  205. }
  206.  
  207. void setWriteMode(){
  208. mode[0] = 'W';
  209. displayTheMode();
  210. }
  211.  
  212. int clamp(int val){
  213. if(val > 999) return 999;
  214. if(val < -999) return -999;
  215. return val;
  216. }
  217.  
  218. void executeAdd(char *arg){
  219. acc = clamp(acc + evaluate(arg));
  220. }
  221.  
  222. void executeSub(char *arg){
  223. acc = clamp(acc - evaluate(arg));
  224. }
  225.  
  226. void executeSwp(){
  227. int temp = bck;
  228. bck = acc;
  229. acc = temp;
  230. }
  231.  
  232. bool executeJmp(char *label){
  233. for(int i=0;i<MAX_LINES;i++){
  234. char *lineLabel = program_lines[i].id;
  235. if(lineLabel != NULL && strcmp(lineLabel, label)==0){
  236. pc = i;
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242.  
  243. bool executeCommand(){
  244. line *line = &program_lines[pc];
  245. if(strcmp(line->cmd, "MOV")==0){
  246. executeMov();
  247. return false;
  248. }
  249. if(strcmp(line->cmd, "ADD")==0){
  250. executeAdd(line->arg1);
  251. return false;
  252. }
  253. if(strcmp(line->cmd, "SUB")==0){
  254. executeSub(line->arg1);
  255. return false;
  256. }
  257. if(strcmp(line->cmd, "JMP")==0){
  258. return executeJmp(line->arg1);
  259. }
  260. if(strcmp(line->cmd, "JEZ")==0){
  261. if(acc==0)
  262. return executeJmp(line->arg1);
  263. return false;
  264. }
  265. if(strcmp(line->cmd, "JNZ")==0){
  266. if(acc!=0)
  267. return executeJmp(line->arg1);
  268. return false;
  269. }
  270. if(strcmp(line->cmd, "JGZ")==0){
  271. if(acc>0)
  272. return executeJmp(line->arg1);
  273. return false;
  274. }
  275. if(strcmp(line->cmd, "JLZ")==0){
  276. if(acc<0)
  277. return executeJmp(line->arg1);
  278. return false;
  279. }
  280. if(strcmp(line->cmd, "SWP")==0){
  281. executeSwp();
  282. return false;
  283. }
  284.  
  285. return false;
  286. }
  287.  
  288. void loop() {
  289. //digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
  290. //delay(1000); // wait for a second
  291. //digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
  292. //delay(1000); // wait for a second
  293. displayCurrentCommand();
  294. displayRegisterValues();
  295. setRunMode1();
  296. delay(500);
  297. bool jumped = executeCommand();
  298. if(!jumped)
  299. moveNextCommand();
  300. setRunMode2();
  301. displayRegisterValues();
  302. delay(500);
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement