Notice
Recent Posts
Recent Comments
Link
반응형
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

doksan-gooner

Implementing Memory Reading Using Only Logic Gates 본문

Computer

Implementing Memory Reading Using Only Logic Gates

Asraiii 2024. 11. 21. 20:08
반응형
SMALL

Purpose of the Post

 

In computing, it’s commonly known that when a unique address is given, the corresponding memory can be read directly. But how does this work? In this post, we’ll explore the concept and attempt a simple implementation using logic gates.

 

Preview of the Result

Let’s say we have memory addresses ranging from 0 to 3, each storing the following values:

address 0: [false, false]
value (binary): [true, false, true, false, true, false, true, false]
value (decimal): 170

address 1: [false, true]
value (binary): [false, true, false, true, false, true, false, true]
value (decimal): 85

address 2: [true, false]
value (binary): [true, true, true, true, false, false, false, false]
value (decimal): 240

address 3: [true, true]
value (binary): [false, false, false, false, true, true, true, true]
value (decimal): 15

Current Memory State:
Address 0: [true, false, true, false, true, false, true, false]
Address 1: [false, true, false, true, false, true, false, true]
Address 2: [true, true, true, true, false, false, false, false]
Address 3: [false, false, false, false, true, true, true, true]

 

When an address is provided, the goal is to read only the value stored at that address using logic gates, without random access.

 


How Does It Work?

 

1. Address Decoding

When an address is provided, a decoder is required to activate only that address. For example, if the input is 2, the decoder will return {false, false, true, false}. This is equivalent to the row/column selection in computer memory.

Decoder Output: {false, false, true, false}

 

2. Activating Memory Cells Using AND

Through an AND operation, only the bits of the activated address remain, while all other bits are set to false.

Memory State After AND:

Address 0: [false, false, false, false, false, false, false, false]

Address 1: [false, false, false, false, false, false, false, false]

Address 2: [true, true, true, true, false, false, false, false]

Address 3: [false, false, false, false, false, false, false, false]

 

3. Generating the Output Using OR

Finally, through an OR operation, only the data of the activated address is used to generate the output.

Final Output: [true, true, true, true, false, false, false, false] (value of address 2)

 


Decoder

 

To enable memory reading for a selected address, a decoder must be implemented. A decoder takes an input address and activates only that specific address among all available options. This can be done using logic gates.

// Decoder
class LogicDecoder {
    private final int inputBits; // input bit count
    private final int outputLines; // output line count

    public LogicDecoder(int inputBits) {
        this.inputBits = inputBits;
        this.outputLines = (int) Math.pow(2, inputBits);
    }

    // 디코더 동작: 논리 게이트로 구현
    public boolean[] decode(boolean[] inputs) {
        boolean[] outputs = new boolean[outputLines];
        Arrays.fill(outputs, false); // initialize

        for (int i = 0; i < outputLines; i++) {
            outputs[i] = true; // initial value
            for (int bit = 0; bit < inputBits; bit++) {
                boolean bitValue = (i & (1 << bit)) != 0; // Whether the bit bit is 1 in output i

                if (bitValue) {
                    outputs[i] = LogicGate.AND(outputs[i], inputs[inputBits - 1 - bit]); // If the bit is 1

                } else {
                    outputs[i] = LogicGate.AND(outputs[i], LogicGate.NOT(inputs[inputBits - 1 - bit])); // If the bit is 0

                }
            }
        }
        return outputs;
    }
}

 

Logical Circuit Representation

 

If the addresses 0, 1, 2, and 3 are represented as binary {a, b}, the outputs x, y, z, and w for each address can be calculated as follows:

x = NOT(a) AND NOT(b)

y = NOT(a) AND b

z = a AND NOT(b)

w = a AND b

 

Truth Table

a b x(0) y(1) z(2) w(3)
0 0 1 0 0 0
0 1 0 1 0 0
1 0 0 0 1 0
1 1 0 0 0 1

 

Final Output Process

 

 

The final output is generated using the following steps:

1. AND Operation to Keep Only Active Bits

Example:

Decoder Output: {false, false, true, false}

Memory State:

Address 0: [false, false, false, false, false, false, false, false]

Address 1: [false, false, false, false, false, false, false, false]

Address 2: [true, true, true, true, false, false, false, false]

Address 3: [false, false, false, false, false, false, false, false]

2. OR Operation to Combine Active Data

Example:

Final Output: [true, true, true, true, false, false, false, false]

 

class Memory {
    private final boolean[][] memory; // Save memory values as bit arrays
    private final LogicDecoder decoder; // decoder
    private final int bitWidth; // data bit width

    public Memory(int size, int bitWidth) {
        this.memory = new boolean[size][bitWidth];
        this.decoder = new LogicDecoder((int) (Math.log(size) / Math.log(2)));
        this.bitWidth = bitWidth;
        for (int i = 0; i < size; i++) {
            Arrays.fill(this.memory[i], false); // memory initialization
        }
    }

    // Write memory data (not based on logic circuit: for initialization)
    public void write(int address, boolean[] data) {
        System.arraycopy(data, 0, this.memory[address], 0, bitWidth);
    }

    // Logical circuit-based data read
    public boolean[] read(boolean[] binaryInput) {
        boolean[] decodedOutput = decoder.decode(binaryInput); // Enable address with decoder
        boolean[] result = new boolean[bitWidth]; // Output value initialization
        Arrays.fill(result, false); // initialization

        // Combine all memory values with AND results from decoder output into OR
        for (int bit = 0; bit < bitWidth; bit++) {
            boolean output = false;
            for (int address = 0; address < memory.length; address++) {
                output = LogicGate.OR(output, LogicGate.AND(decodedOutput[address], memory[address][bit]));
            }
            result[bit] = output; // Save final OR results
        }

        return result;
    }

    // Converting true/false arrays to numbers
    public int toDecimal(boolean[] binary) {
        int decimal = 0;
        for (int i = 0; i < binary.length; i++) {
            if (binary[i]) {
                decimal += (1 << (binary.length - 1 - i)); // add 2^(n-i-1)
            }
        }
        return decimal;
    }

    // Memory Status Output
    public void displayMemory() {
        System.out.println("Current Memory State:");
        for (int i = 0; i < memory.length; i++) {
            System.out.println("Address " + i + ": " + Arrays.toString(memory[i]));
        }
    }
}

 

Output test

public class LogicCircuitMemoryTest {
    public static void main(String[] args) {
        Memory memory = new Memory(4, 8); // 4 memory slots, each storing 8 bits

        // Initialize memory
        memory.write(0, new boolean[]{true, false, true, false, true, false, true, false}); // Address 0, value: 170
        memory.write(1, new boolean[]{false, true, false, true, false, true, false, true}); // Address 1, value: 85
        memory.write(2, new boolean[]{true, true, true, true, false, false, false, false}); // Address 2, value: 240
        memory.write(3, new boolean[]{false, false, false, false, true, true, true, true}); // Address 3, value: 15

        // Convert input values to true/false arrays
        boolean[][] inputs = {
                {false, false}, // Input 0
                {false, true},  // Input 1
                {true, false},  // Input 2
                {true, true}    // Input 3
        };

        // Read data based on logic circuit
        for (int i = 0; i < inputs.length; i++) {
            System.out.println("Input: " + Arrays.toString(inputs[i]));
            boolean[] output = memory.read(inputs[i]); // Read data based on the input
            int decimalOutput = memory.toDecimal(output); // Convert true/false array to decimal value
            System.out.println("Output (binary): " + Arrays.toString(output));
            System.out.println("Output (decimal): " + decimalOutput);
            System.out.println();
        }

        // Display current memory state
        memory.displayMemory();
    }
}

 

Output

Input: [false, false]
Output (binary): [true, false, true, false, true, false, true, false]
Output (decimal): 170

Input: [false, true]
Output (binary): [false, true, false, true, false, true, false, true]
Output (decimal): 85

Input: [true, false]
Output (binary): [true, true, true, true, false, false, false, false]
Output (decimal): 240

Input: [true, true]
Output (binary): [false, false, false, false, true, true, true, true]
Output (decimal): 15

Current Memory State:
Address 0: [true, false, true, false, true, false, true, false]
Address 1: [false, true, false, true, false, true, false, true]
Address 2: [true, true, true, true, false, false, false, false]
Address 3: [false, false, false, false, true, true, true, true]

 


Summary and Reflection

 

Through this exploration, we’ve learned how to use logic gates to read values stored at specific addresses. While this is a simplified demonstration, it provides insight into how computers efficiently handle memory operations.

 

This experiment inspired me to think beyond the abstraction layers we often take for granted when working with computers. By diving deeper into the inner workings, I gained a renewed appreciation for the elegance of these foundational principles.

반응형
LIST