PIC16F690 ram code


/*
 * File:   main.c
 * Author: root
 * replacement of the two 74LS189
 * Created on December 11, 2024, 5:40 PM
 */
// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF       // MCLR Pin Function Select bit 
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)

#include <xc.h>
#define _XTAL_FREQ 4000000
#define WE RA5

void main(void) {
    ANSEL = 0x00;
    ANSELH = 0x00;
    TRISA = 0xFF;  // RA0-RA3 address inputs;  RA5 = WE
    TRISB = 0xFF;  // RB4-RB7 data lines inputs
    TRISC = 0x00;  // RC0-RC3 out lines outputs
    OPTION_REG &= 0x7f; // RABPU = 0  0111 1111 to enable ind. pull-ups  
    WPUA5  = 1;  // RA5 weak pull-up
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    
    /* 
     0:  LDA 14    0001 1110  
     1:  SUB 12    0011 1100  
     2:  JC  6     0111 0110  
     3:  LDA 13    0001 1101   
     4:  OUT       1110 0000
     5:  HLT       1111 0000
     6:  STA 14    0100 1110  
     7:  LDA 13    0001 1101  
     8:  ADD 15    0010 1111  
     9:  OUT       1110 0000
    10:  STA 13    0100 1101  
    11:  JMP 0     0110 0000
    12:  1         0000 0001
    13:  product   0000 0000
    14:  x         0000 1000  
    15:  y         0000 0111   
     */
    
 
//   upper 4-bits    
     unsigned char ram[] = { 1, 3,7, 1,14,15, 4, 1, 2,14, 4, 6, 0, 0, 0, 0};
//   lower 4-bits   
//   unsigned char ram[] =   {14,12,6,13, 0, 0,14,13,15, 0,13, 0,  1, 0, 8, 7};    
    unsigned char addr = 0;
    unsigned char once = 1;
    // read address lines = result = 0 to 15
    // read data lines result = 0 to 15
    while(1) {
        addr = PORTA & 0x0f;  // RA0,RA1,RA2,RA3  0000 1111
        if ((WE == 0) && (once)) { //active low
            once = 0;
            ram[addr] = (PORTB >> 4);  // shift right 4x
            __delay_ms(20);
        }
        if (WE == 1) {
            PORTC = ram[addr];
            once = 1;
        }
     }
    return;
}