//Modified to automatically test 74181 ALU functions //Simple program to show display lower 8 pins, output from AX2 TinyFPGA, in terminal window //Also takes input from terminal and applies to upper 8 pins of the AX2 connection //See here for more info: http://www.rayslogic.com/TinyFPGA/TinyFPGA_To_P2Eval.htm #include "Platform.h" //struct TTL74181_type { // uint8_t A; // uint8_t B; // uint8_t S; // uint8_t CN; // uint8_t M; //} TTL74181; int A, B, S, CN, M, F, CN4; static void TTL74181_update() {//modified version of this: https://github.com/mamedev/historic-mame/blob/master/src/emu/machine/74181.c //TTL74181_state* c = &chips[which]; //int A, B, S, CN, M; //A = TTL74181.A; //B = TTL74181.B; //S = TTL74181.S; //CN= TTL74181.CN; //M = TTL74181.M; uint8_t a0 = A & 1; uint8_t a1 = (A >> 1) & 1; uint8_t a2 = (A >> 2) & 1; uint8_t a3 = (A >> 3) & 1; uint8_t b0 = B & 1; uint8_t b1 = (B >> 1) & 1; uint8_t b2 = (B >> 2) & 1; uint8_t b3 = (B >> 3) & 1; uint8_t s0 = S & 1; uint8_t s1 = (S >> 1) & 1; uint8_t s2 = (S >> 2) & 1; uint8_t s3 = (S >> 3) & 1; //printf("A=%X\n", (a3 << 3) + (a2 << 2) + 1(a1 << 2) + a0); uint8_t cp = CN&1; uint8_t mp = (~M)&1; uint8_t ap0 = !(a0 | (b0 & s0) | (s1 & !b0)); uint8_t bp0 = !((!b0 & s2 & a0) | (a0 & b0 & s3)); uint8_t ap1 = !(a1 | (b1 & s0) | (s1 & !b1)); uint8_t bp1 = !((!b1 & s2 & a1) | (a1 & b1 & s3)); uint8_t ap2 = !(a2 | (b2 & s0) | (s1 & !b2)); uint8_t bp2 = !((!b2 & s2 & a2) | (a2 & b2 & s3)); uint8_t ap3 = !(a3 | (b3 & s0) | (s1 & !b3)); uint8_t bp3 = !((!b3 & s2 & a3) | (a3 & b3 & s3)); uint8_t fp0 = !(cp & mp) ^ (!ap0 & bp0); uint8_t fp1 = (!((mp & ap0) | (mp & bp0 & cp))) ^ (!ap1 & bp1); uint8_t fp2 = (!((mp & ap1) | (mp & ap0 & bp1) | (mp & cp & bp0 & bp1))) ^ (!ap2 & bp2); uint8_t fp3 = (!((mp & ap2) | (mp & ap1 & bp2) | (mp & ap0 & bp1 & bp2) | (mp & cp & bp0 & bp1 & bp2))) ^ (!ap3 & bp3); uint8_t aeqb = fp0 & fp1 & fp2 & fp3; uint8_t pp = !(bp0 & bp1 & bp2 & bp3); uint8_t gp = !((ap0 & bp1 & bp2 & bp3) | (ap1 & bp2 & bp3) | (ap2 & bp3) | ap3); //uint8_t cn4 = (!(cp & bp0 & bp1 & bp2 & bp3)) | gp; uint8_t cn4 = ((cp & bp0 & bp1 & bp2 & bp3)) | (!gp); //RJA //printf("%d %d %d %d %d %d %d, %d %d\n", cn4, cp, bp0, bp1, bp2, bp3, gp, (cp & bp0 & bp1 & bp2 & bp3), !(cp & bp0 & bp1 & bp2 & bp3)); //for (;;) {} //c->outputs[TTL74181_OUTPUT_F0] = fp0; //c->outputs[TTL74181_OUTPUT_F1] = fp1; //c->outputs[TTL74181_OUTPUT_F2] = fp2; //c->outputs[TTL74181_OUTPUT_F3] = fp3; //c->outputs[TTL74181_OUTPUT_AEQB] = aeqb; //c->outputs[TTL74181_OUTPUT_P] = pp; //c->outputs[TTL74181_OUTPUT_G] = gp; //c->outputs[TTL74181_OUTPUT_CN4] = cn4; F = (fp3 << 3) + (fp2 << 2)+(fp1 << 1)+(fp0); CN4 = (cn4 == 0) ? 0 : 1; } int main() { //Show P2 clock freq at startup int cf = _clockfreq(); printf("clock freq= %d\n", cf); int basepin = 0; char cHighPins[9]; char cLowPins[9]; cHighPins[8] = 0; //null terminate string cLowPins[8] = 0; //null terminate string //InteractiveTest(); bool bPass = true; S = 15; printf("Beginning test with S=%X\n", S); CN = 0; M = 0; for (CN=0;CN<2;CN++) for (M=0;M<2;M++) { _pinw(basepin + 7, CN); _pinw(basepin + 6, M); _waitms(1000); printf("\n"); for (A = 0; A < 16; A++) for (B = 0; B < 16; B++) { //Apply input to upper 8 pins of connection uint8_t input = (B<<4)+A; //B on upper pins, A on lower pins for (int i = 0; i < 8; i++) { _pinw(basepin + 8 + i, (input >> i) & 1); } TTL74181_update(); //Get all 16 pin states for (int i = 0; i < 8; i++) {//Get high and low pin input states //Low 8 pins (AX2 output) if (_pinr(basepin + i) == 0) cLowPins[7 - i] = '0'; else cLowPins[7 - i] = '1'; //High 8 pins (AX2 input) if (_pinr(basepin + i + 8) == 0) cHighPins[7 - i] = '0'; else cHighPins[7 - i] = '1'; } //Calculate fpga's F int ffpga = (_pinr(basepin + 3) << 3) + (_pinr(basepin + 2) << 2) + (_pinr(basepin + 1) << 1) + _pinr(basepin); int cn4fpga = (_pinr(basepin + 4)==0) ? 1 : 0; printf("M=%d, CN=%d, A=%X, B=%X, F(p2)=%X, F(fpga)=%X, CN4(p2)=%d, CN4(fpga)=%d\n", M, CN, A, B, F, ffpga, CN4, cn4fpga);// , cLowPins); _waitms(10); if (F != ffpga) bPass = false; if (M == 0) if (CN4 != cn4fpga) bPass = false; ////Get all 16 pin states //for (int i = 0; i < 8; i++) //{//Get high and low pin input states // //Low 8 pins (AX2 output) // if (_pinr(basepin + i) == 0) // cLowPins[7 - i] = '0'; // else // cLowPins[7 - i] = '1'; // //High 8 pins (AX2 input) // if (_pinr(basepin + i + 8) == 0) // cHighPins[7 - i] = '0'; // else // cHighPins[7 - i] = '1'; //} ////printf(" -->input= %c\n", cin); ////show all 16pin states in high to low order ////user input is applied to upper pins and AX2 output is on lower pins //printf("pins =%s_%s\n", cHighPins, cLowPins); //for (;;) {} } } if (bPass == true) printf("\nFPGA Passes test!\n"); else printf("\nFPGA Fails test.\n"); } void InteractiveTest() { int basepin = 0; char cHighPins[9]; char cLowPins[9]; cHighPins[8] = 0; //null terminate string cLowPins[8] = 0; //null terminate string for (;;) {//Show pin states when key pressed in terminal window //get input, if any, from terminal printf("\nEnter HEX input for AX2 (or enter for none) >"); uint8_t input=0; char cin=0; while (cin != '\n') {//get HEX input until enter pressed cin = getchar(); int value = -1; if ((cin >= '0') && (cin <= '9')) value=cin-'0'; if ((cin >= 'a') && (cin <= 'f')) value = cin - 'a'+10; if ((cin >= 'A') && (cin <= 'F')) value = cin - 'A' + 10; if (value>=0) input = (input << 4)+value; } printf("input= %X\n", input); //Apply input to upper 8 pins of connection for (int i = 0; i < 8; i++) { _pinw(basepin+8+i,(input >> i) & 1); } //wait a bit _waitus(10); //Get all 16 pin states for (int i = 0; i < 8; i++) {//Get high and low pin input states //Low 8 pins (AX2 output) if (_pinr(basepin + i) == 0) cLowPins[7-i] = '0'; else cLowPins[7-i] = '1'; //High 8 pins (AX2 input) if (_pinr(basepin + i+8) == 0) cHighPins[7 - i] = '0'; else cHighPins[7 - i] = '1'; } //printf(" -->input= %c\n", cin); //show all 16pin states in high to low order //user input is applied to upper pins and AX2 output is on lower pins printf("pins =%s_%s\n", cHighPins,cLowPins); } }