Advertisement
prjbrook

CDP1802e1.ino Arduino memory for 1802

Apr 17th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.23 KB | None | 0 0
  1. /**5 Arduino is ROM/RAM for 1802
  2. Fri Apr 17 16:53:23 NZST 2020. ..e1 works well. Final step would be a disassembler but need to think about space on nan.
  3. ..all below has been on mega.
  4. Fri Apr 17 12:43:54 NZST 2020.C:\Users\Dell\Documents\Arduino\PB_SketchApril12\CDP1802e0\CDP1802e0.ino. Trying report of all useful vars.
  5. Fri Apr 17 11:41:53 NZST 2020. Going to look at DElay changes and report.
  6. Thu Apr 16 14:26:37 NZST 2020. Sorted, I think, break points, opcode stopping with seial commands. Next:speed changes.
  7. Thu Apr 16 12:25:37 NZST 2020. Working well. Now get break points going in real time...
  8. Tue Apr 14 19:59:13 NZST 202. All code tidied up. Old routines deleted. Next: single stepping.
  9. Tue Apr 14 15:35:00 NZST 2020. Going OK. Just deleting a whole lot of rubbish and carefully retesting.
  10. Tue Apr 14 13:02:36 NZST 2020. Tidy up time, and simplify main loop into easy subroutines.
  11. Mon Apr 13 14:50:01 NZST 2020. version 1802d2.ino now working well. Norway routines have been replaced.
  12. ...by bulletproof ones. Want to make better output each line of code.
  13. ..To form like 0123: F8 F,R,cycles, clocks
  14. Mon Apr 13 13:24:55 NZST 2020. Getting rid of Norwat commands and integrating checkForCommands()
  15. * Sun Apr 12 15:40:21 NZST 2020. Got better serial command routines from bulletProofSerialI. Integrate them.
  16. * Mon Mar 30 15:05:50 NZDT 2020 ..c8. Got long branches, lbr, into page three going OK.
  17. * .. also got sep to work in same way. Jumped in and out of code by changing prog ctr with sep.
  18. * .. next. Try that SCART method of using subroutines on 1802
  19. * Sun Mar 29 14:26:46 NZDT 2020 ..c7. Have big 1024 vRam[] buffer but can't use it yet.
  20. * ..Need TPA time to record Hi adr byte.
  21. * Sorted vRAM[] up to 1024 bytes and got new comand dumpVariableVer(int) to give 256 0r 1024 dumps.
  22. * Sun Mar 29 13:49:17 NZDT 2020 ..c6. Now want to exgtend vRAM[] frolm 256 to 1024 bytes. Fake memory.
  23. * Sat Mar 28 16:23:44 NZDT 2020. This is ...c5. Now going to try fastest baudrate 115200 and fastest DELAY =1.
  24. * All worked. Fast enough for time being. Next increase vRAM[] to 1024 bytes and check Hi address byte on
  25. * ..TPA.
  26. * Sat Mar 28 16:23:44 NZDT 2020 still...c4. Got baudrate up to 57600 and DELAY down to 2. Allworking.
  27. * Sat Mar 28 14:13:16 NZDT 2020 CDP1802c4. Going to try DELAY=4, and baudrate is 38.4K. Works OK but
  28. * ..is too fast in terminal so only print every 10th TPB going-high count. (clocks/8)
  29. * Sat Mar 28 13:37:04 NZDT 2020 CDP1802c3. Going to increase speed all round. First DELAY to 16. Baud to 19.2K Both OK
  30. * Now going to try DELAY =8 and baud = 28.8K. Both worked.
  31. * Sat Mar 28 13:14:18 NZDT 2020 CDP1802c2. Need to speed up a bit. Reg dumping works but is slow.
  32. * so going to change DELAY from 50 to 32 then keep halving. Worked OK also shortened message to terminal.
  33. * Also increased serial speed to 14.4K. Working OK. Next continue speed of clock and baudrate.
  34. * The version below is about right for reading programs into mega to act like ROM for 1802.
  35. * Only hesitation is the norway commands where serial strings seem a bit error prone.
  36. *
  37. * Thu Mar 26 14:09:30 NZDT 2020 CDP1802c1. Some problems with Norway commands as serial chars still available from prevuious actions.
  38. * ..Solution might be to flush Rx buffer after each command.
  39. * Wed Mar 25 16:40:32 NZDT 2020 CDP1802c0. Now going to add in serial read into vRAM[] from bin file generated
  40. * ..by a18 assembler. Uses Bray++ terminal to send file. Norway command is "gf" for getfile. Has to be 256 bytes.
  41. * Wed Mar 25 16:01:41 NZDT 2020 CDP1802b9. All working except for TPA signal and MWR* pin turning on writeStrobe but being tirned off by TPA.
  42. * * Tue Mar 24 13:09:41 NZDT 2020. All below works. Just a bit worried writeStrobe might turn on after being..
  43. * ..dealt with during TPB. Might get TPA to turn off writeStrobe so any subsequent LOWs of MWR* before TPB time are valid only
  44. * Tue Mar 24 10:20:46 NZDT 2020 CDP18028.Playing with MWR*
  45. * CDP1802b7 works. See 1802 blog. Next version: look at MWR.
  46. * CDP1802b6 21/3/203:47 Issues woth serial.available slowing down reads of 1802 data. Trying new Norwegian code.
  47. * 3:57, works witrh dummy commands.
  48. * . Start of array use for instructions
  49. Had big problems with first TPB cycle. Seemed to get old address outp[ut on address bus low when
  50. ..it should have been zero after reset. Round about the 4th clock after reset, adress goes to old value before reset.
  51. ..Settles down into next cycles with consistent addresses 1,2,3,4. But adr=0 remains a problem. The read line doesn't seem to be
  52. ..activated on first cycle so maybe just nop in zero position and move on?
  53. ..Next, connect up BUS pins to C port on mega. Initially just output nops, then when needed by instruction fetch, then chnage
  54. .. to little program that plays with Q and does loop involving nops too. This program has too many diagnostic entries so will tidy up
  55. .. before trying next version.
  56. Tidied up and working with resistors connecting data bus to $C4 pattern = nop.
  57. Now going to send constant $C4 via PORTC of Mega2650 to breadboard containing 1802. PORTC-->BUS 1802 all the time. Should be the
  58. ..same as resistor to rail $C4 above. Worked 10:07
  59. Now try sending $c4 only when clk goes low and TPB is high.Worked 10:19.
  60. Now try sending nop from mega to 1802 bus only when 1) clock goes low,2) TPB is high and 3) read is low. Worked 10:24
  61. Now going to select nop from an array of nops. Worked with just one element of arry of instructions called vRAM[]. Do several next.
  62. Key change was [ byte vRAM[5] = {0xc4, 0xc4, 0xc4, 0xc4, 0x00}; ] Next add instructions to turn on and off Q flag.
  63. Now going to put in little sequence of instructions. Key change is: [ byte vRAM[5] = {0xc4, 0x7b, 0xc4, 0x7a, 0x00};
  64. //ie nop,seq,nop,req,idl ] Worked and got Q flag going on and off with this; [byte vRAM[5] = {0x7b, 0x7b, 0x7a, 0x7b, 0x00}; ]
  65. Now gpoing to turn Q on and off but with branch back to start. Worked OK.
  66. 11:27am 20/3/20. Adding in dump of vertual ram array as well as issuing commands from serial terminal.
  67. ..Practiced some of the routines in dumper3.
  68. Going well, apart from serial read issues, but basic 1820 stuff going well. Try new serial read instruction code.
  69. */
  70.  
  71. //1802 talks to Arduino Mega
  72. #define clockPin 13
  73. #define clearPin 12
  74. #define intPin 11
  75. #define TPBpin A1
  76. #define TPApin A0
  77. #define readPin A15 //the *MRD of 1802
  78. #define writePin A14 //the MWR* of 1802
  79. #define SC0pin A12 //When SC0=LOW it's a fetch, when HI it's executing
  80. #define TPBDivisor 1 //50 //1 // 100 //50 //10 //if ten, shows every tenth output line, otherwise too fast to see and act in terminal
  81. //#define DELAY 50 //16 //1 // 2 //4 //8 //16 //32 //50
  82. byte DELAY=50; //default. Can be changes via terminal command.
  83.  
  84. int val = 0; int bufBytes;
  85. unsigned int adrByte=0;
  86. unsigned int adrByteHi =0;
  87. unsigned int currentAdr;
  88. boolean writeStrobe = false; //should really get TBA to turn this off too. Just in case.
  89. const byte numChars = 32;
  90. char receivedChars[numChars];
  91. byte receivedBytes[numChars];
  92. char data[100];
  93. unsigned int breakPointAdr=0,TPBCtr=0,clkCtr = 0,opCode,stopCode;
  94. unsigned long fetchCtr=0;
  95. char fetchOrExecuteCh;
  96. char readOrWriteCh;
  97. //byte opCode;
  98. boolean newData = false;
  99. boolean showExecutes = true;
  100. boolean TPADone = false, TPBDone=true;
  101. boolean singleStepping = false, stepMe=false;
  102. boolean showCommands=true;
  103. boolean doOpStops = false; //breakpoint if opCode=stopCode;
  104.  
  105.  
  106. //byte vRAM[5] = {0x7b, 0x7b, 0x7a, 0x7b, 0x00}; //ie nop,seq,nop,req,idl
  107. //byte vRAM[256] = {0x7b, 0x7a, 0x7b, 0x7a, 0x30, 0x00,0x00}; //ie seq,seq,req,seq,br start, idl
  108. //byte vRAM[256] = {0x7b, 0x7a, 0x7b, 0xf8,0x00,0xb7,0xf8,0x98,0xa7,0xf8,0x6A,0x57, 0x30, 0x09,0x00};
  109. byte vRAM[1024] = {0x7b, 0x7a, 0x7b, 0xf8,0x00,0xb7,0xf8,0x98,0xa7,0xf8,0x7D,0x57,0xc4, 0x30, 0x09,0x00};
  110. //byte vRAM[512] = {0x7b, 0x7a, 0x7b, 0xf8,0x00,0xb7,0xf8,0x98,0xa7,0xf8,0x7D,0x57,0xc4, 0x30, 0x09,0x00}; //memeory short on nano
  111. void setup() {
  112.  
  113. pinMode(clockPin, OUTPUT);
  114. pinMode(clearPin, OUTPUT);
  115. pinMode(intPin,OUTPUT);
  116. pinMode(TPApin,INPUT);
  117. pinMode(TPBpin,INPUT);
  118. pinMode(readPin,INPUT);
  119. pinMode(writePin,INPUT);
  120. pinMode(SC0pin,INPUT);
  121. DDRA=0x00; //DDRA reads Adress port of 1802. All inputs.
  122. DDRC=0x00; //PORTC connects to 1802 data BUS.
  123. Serial.begin(115200); //(57600); //38400); //(28800); //(19200); //(14400); //(9600); // // open the serial port at 9600 bps:
  124. serialFlush();
  125. delay(1000); //time to sort out Bray++
  126. Serial.flush();
  127. Serial.println("Arduino talks to 1802. CDP1802c1. Pb 2020");
  128. delay(500);
  129. doReset();
  130. // dumpVariableVert(256);
  131. // doReport(); while(1) { };
  132. }
  133.  
  134. //--------------------------------------------------------------------------------------
  135. void loop() {
  136. checkForCommands(); //Still a bit clumsy with Serial lib.
  137. doClockCycle();
  138. }//*******************************************
  139.  
  140. void doClockCycle() {
  141. digitalWrite(clockPin, HIGH); // turn the LED on (HIGH is the voltage level)
  142. if(digitalRead(TPApin) ==HIGH) {
  143. doTPAStuff();
  144. }
  145. if (digitalRead(writePin)==LOW){
  146. doWriteStuff();
  147. }
  148. delay(DELAY); // wait for a second
  149. digitalWrite(clockPin, LOW); // turn the LED off by making the voltage LOW
  150. DDRC=0x00; //DDRC usually input except when sending instruction
  151. clkCtr++; //how many clocks after reset?
  152.  
  153. if (digitalRead(TPBpin) ==HIGH) {
  154. doTPBWork();
  155. }
  156. delay(DELAY); // wait for a second
  157. }
  158. //*************************************************
  159. void doTPBWork(void) {
  160. TPBCtr++;
  161. adrByte = PINA;
  162. if (writeStrobe== true) {
  163. writeStrobe = false;
  164. vRAM[adrByte+adrByteHi*256]=PINC; //adrByteHi comes from read of PORTC during TPA
  165. readOrWriteCh='W';
  166. }
  167. else {
  168. readOrWriteCh='R';
  169. }
  170.  
  171.  
  172. if(digitalRead(SC0pin)==LOW) {
  173. fetchOrExecuteCh='F';
  174. fetchCtr++;
  175. }
  176. else {
  177. fetchOrExecuteCh='E';
  178. }
  179.  
  180. if (digitalRead(readPin)==LOW) {
  181. DDRC = 0xff; //just for this version. Send nop when clk goes 1 to 0, TPB =1, MRD=0
  182. currentAdr = adrByte+adrByteHi*256;
  183. PORTC = vRAM[adrByte+adrByteHi*256] ; // read byte from virtual
  184. opCode=vRAM[adrByte+adrByteHi*256];
  185. }
  186. TPBDone=true;
  187. printTPBResults2();
  188. if((currentAdr==breakPointAdr) && (breakPointAdr>0)) {
  189. Serial.println("Breakpoint address .....");
  190. singleStepping=true;
  191. }
  192. if((doOpStops==true) && (opCode == stopCode)){
  193. Serial.println("Op code stop .....");
  194. singleStepping=true;
  195. }
  196. if(singleStepping==true){
  197. Serial.print("*");
  198. stepMe=false;
  199. while(stepMe==false){
  200. checkForCommands();
  201. }
  202. }
  203. }
  204. void doReset(void){
  205. digitalWrite(intPin,HIGH); //no interrupts yet
  206. digitalWrite(clearPin,LOW); //bring CLEAR low for about 10 clocks to reset 1802
  207. for(int i =0;i<=10;i++) {
  208. digitalWrite(clockPin, HIGH);
  209. delay(DELAY); // wait
  210. digitalWrite(clockPin, LOW);
  211. delay(DELAY);
  212. }// 10 clocks low should reset 1802
  213. digitalWrite(clearPin,HIGH); //bring back to RUN mode
  214. }
  215.  
  216. void serialFlush(void){
  217. while(Serial.available() > 0) { //while there are characters in the serial buffer, because
  218. char t = Serial.read(); // get one character
  219. }
  220. }
  221. void doWriteStuff(void) {
  222. // Serial.print("@W"); //usually happens twice in cycle when
  223. writeStrobe=true;
  224. }
  225. void doTPAStuff(void) {
  226. writeStrobe = false; //before new low-going *MWR causes writeStrobe to go true. Safe.
  227. adrByteHi = PINA; //will be used at TPB time later.
  228. TPADone=true;
  229. }
  230.  
  231. void getFile() { // go to File Send part of Bray++ terminal and put bin file into vRAM[]
  232. int inCtr=0;
  233. byte byteIn=65;
  234. while(inCtr<1024){
  235. if(Serial.available()){
  236. byteIn = Serial.read();
  237. vRAM[inCtr]=byteIn;
  238. inCtr++;
  239. }
  240. }
  241. Serial.print("Got 1024");
  242. }
  243.  
  244. void dumpVariableVert(int sizeToDump) { //new version of dumpVert(). Different sizes.
  245. char data[100];
  246. Serial.print("Col: ");
  247. for(int i=0;i<16;i++) {
  248. sprintf(data, "%02x ",i);
  249. Serial.print(data);
  250. if(i==7) Serial.print(" ");
  251. }
  252. for (int i=0;i<sizeToDump;i++){
  253. if (i%16 == 0){
  254. Serial.println ("");
  255. sprintf(data, "%02x ",i);
  256. Serial.print(data);
  257. }
  258. if(i%8==0) { Serial.print("|"); }
  259. sprintf(data, " %02x ",vRAM[i]);
  260. Serial.print(data);
  261. }
  262. Serial.println(" ");
  263. }
  264. int hexCharToInt2(byte by){ //pb version
  265. int d,r;
  266. d=by-'0';
  267. if(d>=0 && d<=9) {r=d;}
  268. else{
  269. d=by-55; //is it A,B...F?
  270. if (d>=10 && d<=15) {r=d;}
  271. else{
  272. r=by-87; //must be a,b...f. NB No error checking.
  273. }
  274. }
  275. return r;
  276. }
  277.  
  278. void doBreakPoint2(){
  279. int d;
  280. int ctr=0;
  281. unsigned int hexVal[8];
  282. hexVal[2]=hexCharToInt2(receivedBytes[2])*4096;
  283. hexVal[3]=hexCharToInt2(receivedBytes[3])*256;
  284. hexVal[4]=hexCharToInt2(receivedBytes[4])*16;
  285. hexVal[5]=hexCharToInt2(receivedBytes[5])*1;
  286. breakPointAdr=hexVal[2]+hexVal[3]+hexVal[4]+hexVal[5]; //important
  287. Serial.print("Break point adress is : ");
  288. Serial.print(breakPointAdr);
  289. Serial.print(", in hex : 0x");
  290. Serial.println(breakPointAdr,HEX);
  291. }
  292. void doOpStop2(){
  293. // int d;
  294. unsigned int hexVal[8];
  295. hexVal[2]=hexCharToInt2(receivedBytes[2])*16;
  296. hexVal[3]=hexCharToInt2(receivedBytes[3])*1;
  297. stopCode=hexVal[2]+hexVal[3];
  298. Serial.print("Op stop is : ");
  299. Serial.print(stopCode);
  300. Serial.print(", in hex : 0x");
  301. Serial.println(stopCode,HEX);
  302. }
  303. void doDelay() {
  304. unsigned int hexVal[8];
  305. hexVal[2]=hexCharToInt2(receivedBytes[2])*16;
  306. hexVal[3]=hexCharToInt2(receivedBytes[3])*1;
  307. DELAY=hexVal[2]+hexVal[3];
  308. Serial.print("DELAY now is : ");
  309. Serial.print(DELAY);
  310. Serial.print(", in hex : 0x");
  311. Serial.println(DELAY,HEX);
  312. }
  313. void interpretCommand() {
  314. String Command(receivedChars); //**important
  315. if(Command.equals("gf")){
  316. Serial.print("Getting File");
  317. getFile();
  318. serialFlush();
  319. Serial.println("**done gf flush** Now resetting");
  320. doReset();
  321. }
  322.  
  323. if(Command.equals("dk")){
  324. dumpVariableVert(1024);
  325. serialFlush();
  326. Serial.println("**done dkflush**");
  327. }
  328. if(Command.equals("du")){
  329. dumpVariableVert(256);
  330. serialFlush();
  331. Serial.println("**done duflush**");
  332. }
  333. if(Command.equals("re")){
  334. doReset();
  335. TPBCtr=0; clkCtr = 0;
  336. serialFlush();
  337. Serial.println("**did reset flush**");
  338. }
  339.  
  340. if((receivedBytes[0]==0x62)&&(receivedBytes[1]==0x70)) doBreakPoint2(); //<bpxxxx>
  341. if((receivedBytes[0]==0x6f)&&(receivedBytes[1]==0x70)) doOpStop2(); //<opxx>
  342. if((receivedBytes[0]==0x64)&&(receivedBytes[1]==0x65)) doDelay(); //<dexx>.
  343.  
  344. if(Command.equals("singleStep")) singleStepping=true;
  345. if(Command.equals("stepMe")) stepMe=true;
  346. if (Command.equals("report")) doReport();
  347. if(Command.equals("toggleCommandShow")){
  348. if(showCommands==true) showCommands=false;
  349. else showCommands=true;
  350. }
  351. if(Command.equals("toggleShowExecutes")){
  352. if(showExecutes==true) showExecutes=false;
  353. else showExecutes=true;
  354. }
  355. if(Command.equals("toggleOpStopping")){
  356. if(doOpStops==true) doOpStops=false;
  357. else doOpStops=true;
  358. }
  359. if(Command.equals("run")){
  360. singleStepping=false;
  361. stepMe=true;
  362. Serial.println("Sing step = false");
  363.  
  364. }
  365. }
  366.  
  367. void checkForCommands(){ //if any inputs from keyboard, push them into buffer and see what command it is.
  368. recvWithStartEndMarkers();
  369. showAndInterpret();
  370. }
  371.  
  372. void recvWithStartEndMarkers() { ////https://forum.arduino.cc/index.php?topic=396450
  373. static boolean recvInProgress = false;
  374. static byte ndx = 0;
  375. char startMarker = '<';
  376. char endMarker = '>';
  377. char rc;
  378. while (Serial.available() > 0 && newData == false) {
  379. rc = Serial.read();
  380.  
  381. if (recvInProgress == true) {
  382. if (rc != endMarker) {
  383. receivedChars[ndx] = rc;
  384. receivedBytes[ndx]=(byte)rc; //PB added this
  385. ndx++;
  386. if (ndx >= numChars) {
  387. ndx = numChars - 1;
  388. }
  389. }
  390. else {
  391. receivedChars[ndx] = '\0'; // terminate the string
  392. recvInProgress = false;
  393. ndx = 0;
  394. newData = true;
  395. }
  396. }
  397.  
  398. else if (rc == startMarker) {
  399. recvInProgress = true;
  400. }
  401. }
  402. }
  403.  
  404. void showAndInterpret() {
  405. if (newData == true) {
  406. if(showCommands==true){
  407. Serial.print("Command received is : ");
  408. Serial.println(receivedChars);
  409. }
  410. else {serialFlush();} //get rid of commands in input buffer
  411. interpretCommand();
  412. newData = false;
  413. }
  414. }
  415. void printTPBResults2() { //output adr and opcode and clockc et for each line...
  416. //showExecutes=true;
  417. ///temp!!!
  418. //doReport();
  419. if (fetchOrExecuteCh=='F') {
  420. // sprintf(data,"Adr: %02x%02x : %02x %u, %u %c %c",adrByteHi,adrByte,opCode, TPBCtr,clkCtr, fetchOrExecuteCh,readOrWriteCh);
  421. sprintf(data,"Adr: %02x%02x : %02x ",adrByteHi,adrByte,opCode);
  422.  
  423. Serial.println(data);
  424.  
  425.  
  426.  
  427.  
  428. }
  429. else{
  430. if(showExecutes){
  431. sprintf(data," Adr: %x%x : %02x %u, %u %c %c",adrByteHi,adrByte,opCode,TPBCtr,clkCtr, fetchOrExecuteCh,readOrWriteCh);
  432. Serial.println(data);
  433.  
  434. }
  435. }
  436. }
  437. void doReport() { //show all useful variables.
  438. sprintf(data, "delay = %02x, op Stop code = %02x, breakpoint = %04x, current address = %04x, op code = %02x", DELAY,stopCode,breakPointAdr,currentAdr,opCode);
  439. Serial.println(data);
  440. sprintf(data, "TPBCtr = %u, clkCtr= %u, readOrWriteCh = %c, fetchOrExecuteCh = %c, showExecutes = %d, doOpStops=%d ", TPBCtr,clkCtr,readOrWriteCh,fetchOrExecuteCh,showExecutes, doOpStops);
  441. Serial.println(data);
  442. sprintf(data, "TPADone = %d, TPBDone= %d, singleStepping = %d, stepMe = %d, showCommands = %d, instuction fetches=%lu ", TPADone,TPBDone,singleStepping,stepMe,showCommands,fetchCtr);
  443. Serial.println(data);
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement