uas_arduino

SRAM Emulator

Sep 7th, 2014
223
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define ADDR_BITS 4
  2. #define ADDR_SIZE 16
  3. #define DATA_BITS 4
  4.  
  5. #define CE_LOW_DELAY_COUNT 1024
  6.  
  7. volatile int ram[ADDR_SIZE] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  8.  
  9. const byte dataPins[DATA_BITS] = {4, 5, 6, 7};
  10. const byte addrPins[ADDR_BITS] = {10, 11, 12, 13};
  11. const byte cePin = 2;
  12. const byte wePin = 9;
  13.  
  14. void setup(){
  15.     for(short a = 0; a < DATA_BITS; a++){
  16.         pinMode(dataPins[a], INPUT);
  17.     }
  18.  
  19.     for(short a = 0; a < ADDR_BITS; a++){
  20.         pinMode(addrPins[a], INPUT);
  21.     }
  22.  
  23.     pinMode(3, OUTPUT);
  24.     pinMode(cePin, INPUT);
  25.     pinMode(wePin, INPUT);
  26.  
  27.     attachInterrupt(0, ceLow, FALLING);
  28.  
  29. #ifdef DEBUG
  30.     Serial.begin(115200);
  31. #endif
  32. }
  33.  
  34. #ifdef DEBUG
  35. volatile int lastDataRead = -1;
  36. volatile int lastAddressRead = -1;
  37. volatile int lastDataWritten = -1;
  38. #endif
  39.  
  40. void loop(){
  41.     digitalWrite(3, LOW);
  42.  
  43. #ifdef DEBUG
  44.     if(lastDataRead != -1){
  45.         Serial.print("Last Data Read: ");
  46.         Serial.println(lastDataRead, HEX);
  47.         lastDataRead = -1;
  48.     }
  49.  
  50.     if(lastDataWritten != -1){
  51.         Serial.print("Last Data Written: ");
  52.         Serial.println(lastDataWritten, HEX);
  53.         lastDataWritten = -1;
  54.     }
  55.     if(lastAddressRead != -1){
  56.         Serial.print("Last Address Read: ");
  57.         Serial.println(lastAddressRead, HEX);
  58.         lastAddressRead = -1;
  59.     }
  60. #endif
  61.  
  62.     if(digitalRead(cePin) == HIGH){
  63.         setDataOutput(false);
  64.     }
  65. }
  66.  
  67. void setDataOutput(bool isOutputMode){
  68.     for(short a = 0; a < DATA_BITS; a++){
  69.         if(isOutputMode == true){
  70.             pinMode(dataPins[a], OUTPUT);
  71.         }else{
  72.             pinMode(dataPins[a], INPUT);
  73.         }
  74.     }
  75. }
  76.  
  77.  
  78. void ceLow(){
  79.     digitalWrite(3, HIGH);
  80.     int address = 0;
  81.     int data = 0;
  82.  
  83.     for(short counter = 0; counter < ADDR_BITS; counter++){
  84.         if(digitalRead(addrPins[counter]) == HIGH){
  85.             address |= (0x01 << counter);
  86.         }
  87.     }
  88.  
  89. #ifdef DEBUG
  90.     lastAddressRead = address;
  91. #endif
  92.  
  93.     if(digitalRead(wePin) == HIGH){
  94.         setDataOutput(false);
  95.        
  96.         for(short counter = 0; counter < DATA_BITS; counter++){
  97.             if(digitalRead(dataPins[counter]) == HIGH){
  98.                 data |= (0x01 << counter);
  99.             }
  100.         }
  101.  
  102. #ifdef DEBUG
  103.         lastDataRead = data;
  104. #endif
  105.         ram[address] = data;
  106.        
  107.     }else{
  108.         setDataOutput(true);
  109.  
  110. #ifdef DEBUG
  111.         lastDataWritten = ram[address];
  112. #endif
  113.  
  114.         for(short counter = 0; counter < DATA_BITS; counter++){
  115.             if((ram[address] & (0x01 << counter)) != 0){
  116.                 digitalWrite(dataPins[counter], HIGH);
  117.             }else{
  118.                 digitalWrite(dataPins[counter], LOW);
  119.             }
  120.         }
  121.     }
  122.         while(digitalRead(cePin) == LOW){
  123.         }
  124. }
RAW Paste Data