Advertisement
prjbrook

1802 and Arduino mega

Mar 25th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.85 KB | None | 0 0
  1. /**
  2. * The version below is about right for reading programs into mega to act like ROM for 1802.
  3. * Only hesitation is the norway commands where serial strings seem a bit error prone.
  4. * Thu Mar 26 14:09:30 NZDT 2020 CDP1802c1. Some problems with Norway commands as serial chars still available from prevuious actions.
  5. * ..Solution might be to flush Rx buffer after each command.
  6. * Wed Mar 25 16:40:32 NZDT 2020 CDP1802c0. Now going to add in serial read into vRAM[] from bin file generated
  7. * ..by a18 assembler. Uses Bray++ terminal to send file. Norway command is "gf" for getfile. Has to be 256 bytes.
  8. * 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.
  9. * * Tue Mar 24 13:09:41 NZDT 2020. All below works. Just a bit worried writeStrobe might turn on after being..
  10. * ..dealt with during TPB. Might get TPA to turn off writeStrobe so any subsequent LOWs of MWR* before TPB time are valid only
  11. * Tue Mar 24 10:20:46 NZDT 2020 CDP18028.Playing with MWR*
  12. * CDP1802b7 works. See 1802 blog. Next version: look at MWR.
  13. * CDP1802b6 21/3/203:47 Issues woth serial.available slowing down reads of 1802 data. Trying new Norwegian code.
  14. * 3:57, works witrh dummy commands.
  15. * . Start of array use for instructions
  16. Had big problems with first TPB cycle. Seemed to get old address outp[ut on address bus low when
  17. ..it should have been zero after reset. Round about the 4th clock after reset, adress goes to old value before reset.
  18. ..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
  19. ..activated on first cycle so maybe just nop in zero position and move on?
  20. ..Next, connect up BUS pins to C port on mega. Initially just output nops, then when needed by instruction fetch, then chnage
  21. .. to little program that plays with Q and does loop involving nops too. This program has too many diagnostic entries so will tidy up
  22. .. before trying next version.
  23. Tidied up and working with resistors connecting data bus to $C4 pattern = nop.
  24. Now going to send constant $C4 via PORTC of Mega2650 to breadboard containing 1802. PORTC-->BUS 1802 all the time. Should be the
  25. ..same as resistor to rail $C4 above. Worked 10:07
  26. Now try sending $c4 only when clk goes low and TPB is high.Worked 10:19.
  27. 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
  28. Now going to select nop from an array of nops. Worked with just one element of arry of instructions called vRAM[]. Do several next.
  29. Key change was [ byte vRAM[5] = {0xc4, 0xc4, 0xc4, 0xc4, 0x00}; ] Next add instructions to turn on and off Q flag.
  30. Now going to put in little sequence of instructions. Key change is: [ byte vRAM[5] = {0xc4, 0x7b, 0xc4, 0x7a, 0x00};
  31. //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}; ]
  32. Now gpoing to turn Q on and off but with branch back to start. Worked OK.
  33. 11:27am 20/3/20. Adding in dump of vertual ram array as well as issuing commands from serial terminal.
  34. ..Practiced some of the routines in dumper3.
  35. Going well, apart from serial read issues, but basic 1820 stuff going well. Try new serial read instruction code.
  36. */
  37.  
  38. //1802 talks to Arduino Mega
  39. #define clockPin 13
  40. #define clearPin 12
  41. #define TPBpin A1
  42. #define TPApin A0
  43. #define readPin A15 //the *MRD of 1802
  44. #define writePin A14 //the MWR* of 1802
  45. #define SC0pin A12 //When SC0=LOW it's a fetch, when HI it's executing
  46. #define DELAY 50
  47.  
  48. int val = 0; int bufBytes, TPBCtr, clkCtr = 0;
  49. int adrByte=0;
  50. boolean writeStrobe = false; //should really get TBA to turn this off too. Just in case.
  51.  
  52. //byte vRAM[5] = {0x7b, 0x7b, 0x7a, 0x7b, 0x00}; //ie nop,seq,nop,req,idl
  53. //byte vRAM[256] = {0x7b, 0x7a, 0x7b, 0x7a, 0x30, 0x00,0x00}; //ie seq,seq,req,seq,br start, idl
  54. byte vRAM[256] = {0x7b, 0x7a, 0x7b, 0xf8,0x00,0xb7,0xf8,0x98,0xa7,0xf8,0x5b,0x57, 0x30, 0x09,0x00};
  55. void setup() {
  56.  
  57. pinMode(clockPin, OUTPUT);
  58. pinMode(clearPin, OUTPUT);
  59. pinMode(TPApin,INPUT);
  60. pinMode(TPBpin,INPUT);
  61. pinMode(readPin,INPUT);
  62. pinMode(writePin,INPUT);
  63. pinMode(SC0pin,INPUT);
  64. DDRA=0x00; //DDRA reads Adress port of 1802. All inputs
  65. DDRC=0x00; //PORTC connects to 1802 data BUS.
  66. Serial.begin(9600); // open the serial port at 9600 bps:
  67. serialFlush();
  68. delay(1000); //time to sort out Bray++
  69. Serial.flush();
  70. Serial.println("Arduino talks to 1802. CDP1802c1. Pb 2020");
  71. delay(500);
  72. doReset();
  73. dumpVert();
  74. }
  75.  
  76. //--------------------------------------------------------------------------------------
  77. void loop() {
  78. norwayCommand();
  79. digitalWrite(clockPin, HIGH); // turn the LED on (HIGH is the voltage level)
  80. if(digitalRead(TPApin) ==HIGH) {
  81. doTPAStuff();
  82. }
  83. if (digitalRead(writePin)==LOW){
  84. doWriteStuff();
  85. }
  86. delay(DELAY); // wait for a second
  87. digitalWrite(clockPin, LOW); // turn the LED off by making the voltage LOW
  88. DDRC=0x00; //DDRC usually input except when sending instruction
  89. clkCtr++; //how many clocks after reset?
  90.  
  91. if (digitalRead(TPBpin) ==HIGH) {
  92. doTPBWork();
  93. }
  94. delay(DELAY); // wait for a second
  95. }
  96. void doTPBWork(void) {
  97. TPBCtr++;
  98.  
  99. adrByte = PINA;
  100. if (writeStrobe== true) {
  101. writeStrobe = false;
  102. vRAM[adrByte]=PINC;
  103. }
  104. Serial.print("TPB with clock low. Num=");
  105. Serial.print(TPBCtr);
  106. Serial.print(" Clocks=");
  107. Serial.print(clkCtr);
  108. Serial.print(" AdrL=");
  109.  
  110. Serial.print(PINA,HEX);
  111. if (digitalRead(readPin)==LOW) {
  112. DDRC = 0xff; //just for this version. Send nop when clk goes 1 to 0, TPB =1, MRD=0
  113. PORTC = vRAM[adrByte] ; // read byte from virtual
  114. Serial.print(" (R ");
  115. Serial.print(vRAM[adrByte],HEX);
  116. }
  117.  
  118. if(digitalRead(SC0pin)==LOW) {
  119. Serial.print(" F "); //fetching instruction
  120. }
  121. else {
  122. Serial.print(" E "); //executing instruction
  123. }
  124. Serial.println(" ]");
  125. }
  126. void doReset(void){
  127. digitalWrite(clearPin,LOW); //bring CLEAR low for about 10 clocks to reset 1802
  128. for(int i =0;i<=10;i++) {
  129. digitalWrite(clockPin, HIGH);
  130. delay(DELAY); // wait
  131. digitalWrite(clockPin, LOW);
  132. delay(DELAY);
  133. }// 10 clocks low should reset 1802
  134. digitalWrite(clearPin,HIGH); //bring back to RUN mode
  135. }
  136. void serialFlush(void){
  137. while(Serial.available() > 0) { //while there are characters in the serial buffer, because Serial.available is >0
  138. char t = Serial.read(); // get one character
  139. }
  140. }
  141. void showLenOfTxBuffer(void) {
  142. bufBytes=Serial.availableForWrite();
  143. Serial.print(bufBytes);
  144. Serial.println("~");
  145. }
  146.  
  147. void dumpVert(void) {
  148. char data[100];
  149. Serial.print("Col: ");
  150. for(int i=0;i<16;i++) {
  151. sprintf(data, "%02x ",i);
  152. Serial.print(data);
  153. if(i==7) Serial.print(" ");
  154. }
  155. for (int i=0;i<256;i++){
  156. if (i%16 == 0){
  157. Serial.println ("");
  158. sprintf(data, "%02x ",i);
  159. Serial.print(data);
  160. }
  161. if(i%8==0) { Serial.print("|"); }
  162. sprintf(data, " %02x ",vRAM[i]);
  163. Serial.print(data);
  164. }
  165. Serial.println(" ");
  166. }
  167. void doDump(void) {
  168. Serial.println("Got a dump");
  169. dumpVert();
  170. }
  171. void doGetProgram(void){
  172. Serial.println("Doing getProgram.");
  173. }
  174. void doWriteStuff(void) {
  175. // Serial.print("@W"); //usually happens twice in cycle when
  176. writeStrobe=true;
  177. }
  178. void doTPAStuff(void) {
  179. writeStrobe = false; //before new low-going *MWR causes writeStrobe to go true. Safe.
  180. }
  181.  
  182. void getFile() { // go to File Send part of Bray++ terminal and put bin file into vRAM[]
  183. int inCtr=0;
  184. byte byteIn=65;
  185. while(inCtr<256){
  186. if(Serial.available()){
  187. byteIn = Serial.read();
  188. vRAM[inCtr]=byteIn;
  189. inCtr++;
  190. }
  191. }
  192. Serial.print("Got 256");
  193. }
  194. void norwayCommand(void){ //trying better instruction method from serial https://www.norwegiancreations.com/2017/12/arduino-tutorial-serial-inputs/
  195. String command;
  196.  
  197. if(Serial.available()){
  198. command = Serial.readStringUntil('\n'); //Not happy with this method but works if exactly 256 bytes sent.
  199.  
  200. if(command.equals("gf")){
  201. Serial.print("Getting File");
  202. getFile();
  203. serialFlush();
  204. Serial.println("**done gf flush** Now resetting");
  205. doReset();
  206. }
  207.  
  208. else if(command.equals("du")){
  209. dumpVert();
  210. serialFlush();
  211. Serial.println("**done flush**");
  212. }
  213. else{
  214. Serial.println("Invalid command");
  215. Serial.println(command);
  216. }
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement