nim64/chips/ic74139

An emulation of the 74139 dual 2-to-4 demultiplexer.

The 74139 is one of the 7400-series TTL logic chips, consisting of a pair of 2-input, 4-output demultiplexers. There are four possible binary combinations on two pins (LL, HL, LH, and HH), and each of these combinations selects a different one of the output pins to activate. Each demultiplexer also has an enable pin.

Most literature names the pins with numbers first. This makes sense since there are really two numbers that go into the output's name (the demultiplexer number and the output number) and having a letter separate them is quite readable. But since each of these pin names becomes the name of a constant, that scheme cannot be used here. Therefore each demultiplexer has two inputs starting with A and B, an active-low enable pin starting with G, and four inverted outputs whose names start with Y.

GnAnBnY0nY1nY2nY3n
HXXHHHH
LLLLHHH
LHLHLHH
LLHHHLH
LHHHHHL

In the Commodore 64, the two demultiplexers are chained together by connecting one of the outputs from demux 1 to the enable pin of demux 2. The inputs are the address lines A8-A11, and the enable pin of demux 1 comes directly from the PLA's IO output. Thus the demultiplexers only do work when IO is selected, which requires that the address be from $D000 - $DFFF, among other things. A more specific table for this setup can thus be created.

IOA8A9A10A11AddressActive Output
HXXXXN/ANone
LXXLL$D000 - $D3FFVIC
LXXHL$D400 - $D7FFSID
LXXLH$D800 - $DBFFColor RAM
LLLHH$DC00 - $DCFFCIA 1
LHLHH$DD00 - $DDFFCIA 2
LLHHH$DE00 - $DEFFI/O 1
LHHHH$DF00 - $DFFFI/O 2

The decoding resolution is only 2 hexadecimal digits for the VIC, SID, and color RAM and 3 hexadecimal digits for the CIAs and I/Os. This means that there will be memory locations that repeat. For example, the VIC only uses 64 addressable locations for its registers (47 registers and 17 more unused addresses) but gets a 1024-address block. The decoding can't tell the difference between $D000, $D040, $D080, and so on because it can only resolve the first two digits, so using any of those addresses will access the VIC's first register, meaning that it's mirrored 16 times. The same goes for the SID (29 registers and 3 usused addresses, mirrored in 1024 addresses 32 times) and the CIAs (16 registers mirrored in 256 addresses 16 times). The color RAM is not mirrored at all (though it does use only 1000 of its 1024 addresses) and the I/O blocks are free to be managed by cartridges as they like.

The chip comes in a 16-pin dual in-line package with the following pin assignments.

         +---+--+---+
      G1 |1  +--+ 16| VCC
      A1 |2       15| G2
      B1 |3       14| A2
     Y01 |4       13| B2
     Y11 |5 74139 12| Y02
     Y21 |6       11| Y12
     Y31 |7       10| Y22
     GND |8        9| Y32
         +----------+
GND and VCC are ground and power supply pins respectively, and they are not emulated.

In the Commodore 64, U15 is a 74LS139 (a lower-power, faster variant whose emulation is the same). Its two demultiplexers are chained together to provide additional address decoding when the PLA's IO output is selected.

Types

Ic74139 = ref object
  pins: Pins

Consts

G1 = 1
The pin assignment for demultipelxer 1's enable pin.
A1 = 2
The pin assignment for demultipelxer 1's first input pin.
B1 = 3
The pin assignment for demultipelxer 1's second input pin.
Y01 = 4
The pin assignment for demultipelxer 1's first output pin.
Y11 = 5
The pin assignment for demultipelxer 1's second output pin.
Y21 = 6
The pin assignment for demultipelxer 1's third output pin.
Y31 = 7
The pin assignment for demultipelxer 1's fourth output pin.
GND = 8
The pin assignment for the ground pin.
Y32 = 9
The pin assignment for demultiplexer 2's fourth output pin.
Y22 = 10
The pin assignment for demultiplexer 2's third output pin.
Y12 = 11
The pin assignment for demultiplexer 2's second output pin.
Y02 = 12
The pin assignment for demultiplexer 2's first output pin.
B2 = 13
The pin assignment for demultiplexer 2's second input pin.
A2 = 14
The pin assignment for demultiplexer 2's first input pin.
G2 = 15
The pin assignment for demultiplexer 2's enable pin.
VCC = 16
The pin assignment for the +5V power supply pin.

Procs

proc `[]`(chip`gensym0: Ic74139; index`gensym0: int): Pin {...}{.inline, raises: [],
    tags: [].}
Looks up a pin by the pin's number.
proc `[]`(chip`gensym0: Ic74139; index`gensym0: string): Pin {...}{.inline,
    raises: [KeyError], tags: [].}
Looks up a pin by the pin's name.
proc pins(chip`gensym0: Ic74139): seq[PinInfo] {...}{.raises: [], tags: [].}
Returns a read-only view of the chip's pins.
proc newIc74139(): Ic74139 {...}{.raises: [Exception, KeyError, ValueError],
                             tags: [RootEffect].}

Iterators

iterator items(chip`gensym0: Ic74139): Pin {...}{.raises: [], tags: [].}
Iterates over the chip's pins, yielding them in order by pin number.
iterator pairs(chip`gensym0: Ic74139): (int, Pin) {...}{.raises: [], tags: [].}
Iterates over the chip's pins, yielding both the pin number and the pin itself.