Skip to content
Snippets Groups Projects
Select Git revision
  • jedian1806
  • jedian
  • Strozzi
  • master default
4 results

jedidafu.c

Blame
  • Forked from Roberto Hexsel / cMIPS
    31 commits behind, 23 commits ahead of the upstream repository.
    jedidafu.c 5.87 KiB
    #include "cMIPS.h"
    #include "../include/vetorFib.h"
    
    #define MAXPOW 7
    typedef struct UARTdriver {
    int rx_hd ; // reception queue head index
    int rx_tl ; // reception queue tail index
    char rx_q [16]; // reception queue
    int tx_hd ; // transmission queue head index
    int tx_tl ; // transmission queue tail index
    char tx_q [16]; // transmission queue
    int nrx ; // number of characters in rx_queue
    int ntx ; // number of spaces in tx_queue
    } UARTdriver ;
    extern UARTdriver Ud ;
    
    
    typedef struct control { // control register fields (uses only ls byte)
      int ign   : 24,        // ignore uppermost bits
        rts     : 1,         // Request to Send output (bit 7)
        ign2    : 2,         // bits 6,5 ignored
        intTX   : 1,         // interrupt on TX buffer empty (bit 4)
        intRX   : 1,         // interrupt on RX buffer full (bit 3)
        speed   : 3;         // 4,8,16..256 tx-rx clock data rates  (bits 0..2)
    } Tcontrol;
    
    typedef struct status {  // status register fields (uses only ls byte)
      unsigned int ign : 24, // ignore uppermost 3 bytes
      cts     : 1,           // Clear To Send input=1 (bit 7)
      txEmpty : 1,           // TX register is empty (bit 6)
      rxFull  : 1,           // octet available from RX register (bit 5)
      int_TX_empt: 1,        // interrupt pending on TX empty (bit 4)
      int_RX_full: 1,        // interrupt pending on RX full (bit 3)
      ign1    : 1,           // ignored (bit 2)
      framing : 1,           // framing error (bit 1)
      overun  : 1;           // overun error (bit 0)
    } Tstatus;
    
    
    typedef union ctlStat { // control + status on same address
      Tcontrol  ctl;        // write-only
      Tstatus   stat;       // read-only
    } TctlStat;
    
    typedef union data {    // data registers on same address
      int tx;               // write-only
      int rx;               // read-only
    } Tdata;
    
    typedef struct serial {
      TctlStat cs;
      Tdata    d;
    } Tserial;
    
      volatile Tserial *uart;  // tell GCC not to optimize away code
    char Getc () {
        char c ;
        if ( Ud.nrx > 0) {
            disableInterr();
            // exclui handler enquanto altera Ud
            Ud.nrx -= 1;
            c = Ud.rx_q [ Ud.rx_hd ];
            Ud.rx_hd = (Ud.rx_hd + 1) & 15; // modulo 16
            enableInterr();
        } else {
            c = -1;
        }
        return c ;
    }
    
    int proberx () {
        return Ud.nrx;
    }
    int Strlen (char *c) {
        int i= 0;
        while(c[i++]-10);
        return i-1;
    }
    
    int pow16 (int ex) {
        int i, ret=1;
        for (i= 0; i< ex/2; i++)
            ret*=16;
        ret *= ret;
        return (ex%2?ret*16:ret);
    }
    
    int hex2int (char *hex) {
        int s= Strlen(hex), i, ans= 0;
        #if 0
            Test Signal...
        switch (hex[0]) {
            case 'F':
            case 'f':
            case 'E':
            case 'e':
            case 'D':
            case 'd':
            case 'C':
            case 'c':
            case 'B':
            case 'b':
            case 'A':
            case 'a':
            case '9':
                ans = hex[0]*pow16(s-1);
        }
        #endif
        for (i= 0; i< s; i++) {
            hex[i]-= 48; 
            if (hex[i] > 9)
                hex[i] -= 7;
            if (hex[i] > 15) 
                hex[i] -= 32; 
            if(hex[i] < 0)
                hex[i]=0;
            ans += hex[i] * pow16(s-(i+1));
        }   
        return ans;
    }
    int int2hex (char *c, int i) {
        int j= 0, val;
        int p;
        p = pow16(MAXPOW);
        while (p > 0) {
            val= 0;
            if (i/p) {
                val= i/p;
                i%= p;
            }
            if ((val < 10) && (j || val)) { // impede que '0' sejam escritos à esquerda
                c[j++] = val+'0';
            }
            else if (val > 9)
                c[j++] = 'a'+ (val-10);
            p/=16;
        }   
        c[j] = 10; 
        return j-1;
    }
    int Putc (char c) {
        int ntx = Ud.ntx;
        if (ntx == 0)
            return 0;
        if (ntx == 16 && uart->cs.stat.txEmpty)  // Empty Q && Tx
            uart->d.tx = (unsigned int) c;                   // 32-bits
        else {                                      // Sending smthng
            disableInterr();
            Ud.ntx--;
            // INCREMENTS AND THEN STORE
            Ud.tx_tl = (Ud.tx_tl+1)&0xF;          // modulo 16
            Ud.tx_q[Ud.tx_tl] = c;
            enableInterr();
        }
        return 1;
    }
    
    
    int main(void) { // receive a string through the UART serial interface
      int i, j;
      volatile int state;
      volatile Tstatus status;
      char filona[145], c;
      int hdona=0;
      int tlona=0;
      int filonx = 0;
      int valfib, qt, rxz;
      char p[15];
      Tcontrol ctrl;
    
      uart = (void *)IO_UART_ADDR; // bottom of UART address range
    
      ctrl.ign   = 0;
      ctrl.rts   = 0;   // make RTS=0 to hold RemoteUnit
      ctrl.ign2  = 0;
      ctrl.intTX = 1;
      ctrl.intRX = 1;
      ctrl.speed = 1;   // operate at the second highest data rate
      uart->cs.ctl = ctrl;
    
      i = -1;
    
      ctrl.ign   = 0;
      ctrl.rts   = 1;   // make RTS=1 to activate RemoteUnit
      ctrl.ign2  = 0;
      ctrl.intTX = 1;
      ctrl.intRX = 1;
      ctrl.speed = 1;   // operate at the second highest data rate
      uart->cs.ctl = ctrl;
      Ud.ntx = 16;
      do {
        state = i = i+1;
        while(!proberx()) to_stdout('-');//i = 1 - i;
        to_stdout('\0');    
        while((c=Getc()) != -1){
            filona[tlona] = c;
            tlona++;
            tlona%=145;
            if(c=='\n'){
                filonx++;
                filona[tlona] = 0;
            }
        }
        while(filonx > 0){
            qt = Strlen(filona+hdona);
            valfib = hex2int((filona+hdona));
            valfib = buf[valfib];
            hdona += qt+1;
            hdona %= 145;
            print(valfib);
            qt = int2hex(p, valfib);
            j = -1;
            do {
                j++;
                while(((rxz = proberx())<16) && !Putc(p[j]));
                    if(rxz >= 2){
                         while((c=Getc()) != -1){
                            filona[tlona] = c;
                             tlona++;
                             tlona%=145;
                             if(c=='\n'){
                                 filonx++;
                                 filona[tlona] = 0;
                             }
                        }                 
                        j--;
                    }
                
            } while (p[j] != 0);
            filonx--;
        }
        
      } while (i<5);
    
      return(state+i);
    
    }