简体   繁体   中英

C/Digital Logic - Why are my zero-initialized variables changing value?

They're actually initialized to the character '0' (ie 48). I'm making a logic simulator and each bit is represented as a single char, either '0' or '1'. All of my other components have worked until I made an 8-bit selector.
This is the code for the 1-bit selector. It works fine.

typedef char Bit;
...
typedef struct SelectorComp {
    Bit store;
    Bit data[2];
    NandGate nand[3];
    NotGate not;
    Bit out;
} SelectorComp;

void initSelector(SelectorComp* selector) {
    int i;
    selector->store = '0';
    for (i = 0; i < 2; i++) selector->data[i] = '0';
    initNot(&selector->not);
    for (i = 0; i < 3; i++) initNand(&selector->nand[i]);
    Selector(selector);
}

void Selector(SelectorComp* selector) {
    selector->nand[0].in[0] = selector->store;
    selector->nand[0].in[1] = selector->data[0];
    Nand(&selector->nand[0]);
    selector->not.in = selector->store;
    Not(&selector->not);
    selector->nand[1].in[0] = selector->not.out;
    selector->nand[1].in[1] = selector->data[1];
    Nand(&selector->nand[1]);
    selector->nand[2].in[0] = selector->nand[0].out;
    selector->nand[2].in[1] = selector->nand[1].out;
    Nand(&selector->nand[2]);
    selector->out = selector->nand[2].out;
}

The 8-bit selector is basically 8 1-bit selectors tied together.

typedef struct Selector8Comp {
    Bit store;
    Bit data[2][8];
    SelectorComp selector[8];
    Bit out[8];
} Selector8Comp;

void initSelector8(Selector8Comp* selector8) {
  int i, j;
    selector8->store = '0';
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 8; j++) {
            selector8->data[i][j] = '0';
        }
    }
    for (i = 0; i < 8; i++) initSelector(&selector8->selector[i]);
    Selector8(selector8);
}

void Selector8(Selector8Comp* selector8) {
    int i, j;
    for (i = 0; i < 8; i++) {
        selector8->selector[i].store = selector8->store;
        for (j = 0; j < 2; j++) selector8->selector[i].data[j] = selector8->data[i][j];
        Selector(&selector8->selector[i]);
        selector8->out[i] = selector8->selector[i].out;
    }
}

However, when I test it with the following function, I get unexpected output.

void testSelector8() {
    int i;
    Selector8Comp* selector8 = (Selector8Comp*)malloc(sizeof(Selector8Comp));
    initSelector8(selector8);

    printf("Testing 8-bit selector\n");

    printf("store: %c\n", selector8->store);
    printf("byte 0: ");
    for (i = 0; i < 8; i++) printf("%c", selector8->data[0][i]);
    printf("\nbyte 1: ");
    for (i = 0; i < 8; i++) printf("%c", selector8->data[1][i]);
    printf("\nselectors:\n");
    for (i = 0; i < 8; i++) {
        printf("%d: store: %c in: %c%c out: %c\n", i, selector8->selector[i].store, selector8->selector[i].data[0], selector8->selector[i].data[1], selector8->selector[i].out);
    }
    printf("\noutput: ");
    for (i = 0; i < 8; i++) printf("%c", selector8->out[i]);
    printf("\n");

    free(selector8);
}

Expected output:

store: 0
byte 0: 00000000
byte 1: 00000000
selectors:
0: store: 0 in: 00 out: 0
1: store: 0 in: 00 out: 0
2: store: 0 in: 00 out: 0
3: store: 0 in: 00 out: 0
4: store: 0 in: 00 out: 0
5: store: 0 in: 00 out: 0
6: store: 0 in: 00 out: 0
7: store: 0 in: 00 out: 0

output: 0000000

Actual output:

store: 0
byte 0: 00000000
byte 1: 00000000
selectors:
0: store: 0 in: 00 out: 0
1: store: 0 in: 00 out: 0
2: store: 0 in: 00 out: 0
3: store: 0 in: 11 out: 1
4: store: 0 in: 10 out: 0
5: store: 0 in: 10 out: 0
6: store: 0 in: 01 out: 1
7: store: 0 in: 01 out: 1

output: 00010011

Unless I'm reading my code wrong, all of those selectors should have their data initialized to "00". Why don't they?

EDIT: I'm using the latest gcc on ARM if that helps.

I was reading my code wrong (imagine that). I switched up an i and aj (line 5 of the Selector8 function definition). The j was supposed to come before the i in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM