• PC NEWS: 1.4.2.3 update is out! More info here. Report bugs here
  • CONSOLE 1.4 NEWS: The current plan is to have Terraria: Journey's End out for Xbox and PlayStation in Q3 2021 (Jul-Sep). Switch will follow shortly afterwards. More info here. Stay up to date with console news here
  • MOBILE NEWS: Stay up to date with the latest mobile news here. Report mobile bugs here

PC Binary to BCD Guide

crawlr

Terrarian
There's been a lot of work recently with 7-segment displays and some people have shown various ways of going from BCD to the 7-segment, but I haven't seen any new logic gate based binary to BCD modules. (I am not the best with this stuff so if someone knows of a more efficient way to achieve this please let me know ;))

The simplest approach, and a very common way of converting binary to BCD is with the Double dabble algorithm. If you are not familiar with it I suggest you work threw the example on the wikipedia page then pick some random numbers and step threw it on paper. Once you get how it works/what's going on continue reading.

A key concept when using double dabble is that before checking whether to add 3 each 4-bit chunk can only contain the values 0-9 (0000b - 1001b). Out of those 10 possible inputs for our add 3 function half of them will be unmodified (they are <5) and the other half will have 3 added. Here is a truth table for the add 3 step in the double dabble algorithm:

Input -> Output
-------------
0000 -> 0000
0001 -> 0001
0010 -> 0010
0011 -> 0011
0100 -> 0100
-------------
0101 -> 1000
0110 -> 1001
0111 -> 1010
1000 -> 1011
1001 -> 1100
-------------

We can take this truth table and convert it into a circuit in numerous ways. This is the most succinct way I could think to do it:
vBNmBsh.png

EDIT --- I just noticed that this picture has a NAND gate attached to the OR gate right before the S1 output. This was a mistake and should be an AND gate like the rest. The pictures in game use AND gates. This was just a clerical error when I was making the picture above. Sorry.

We can then take and model this circuit in game. I tried to keep the design as compact as I could, but easy to follow. The input and output wires are color coded. Yellow is the low bit, green the second bit, blue the third bit, and red the high bit. (I think it actually looks much cleaner in game than in my circuit simulator :happy: -- someone can probably compact it further, if you can think of something let me know!)
z2uhCqc.png

With a working ADD3 module we are almost done! Using a single ADD3 we could convert 4bit numbers to BCD, but for larger numbers we need to chain multiple ADD3 modules together making sure to shift the output of each device to the left as you go. For example, an 8 bit number would require 7 of these modules linked together like so:
fHQA4py.png


As you can see this design is easily expandable and can support any size binary input by chaining on the necessary ADD3 modules.

The last thing to note is a very important game mechanic detailed by @NiraExecuto in this thread. Due to how the game handles gate activations, you can end up with out of sync signals on complex circuits. You'll notice a series of diodes along the top of the device. Whenever I carry a bit I pass it threw two diodes to ensure it reaches the next ADD3 module at the same time as the rest of the signals do. See the thread linked for more details.

If you have any questions, fire away!

PS: Someone really needs to help me figure out why TEdit just insta crashes for me. These builds are getting more complex and time consuming to do by hand... :sigh:
 
Last edited:

idkwhoiam129

Steampunker
Oh my god I have been trying to do this for the longest time using double dabble haha... What sort of adding mechanism did you use? It doesn't look like a full adder and it doesnt look like it can carry binary digits... I'm impressed! This actually works?
 

crawlr

Terrarian
Yep it works. I am not using an ALU at all. That is overkill. The circuit diagram I posted is for the truth table of the add 3 step in the double dabble. By modeling that truth table we can send in 4 bits and get out the 4 bit result from the add 3 operation.
 

idkwhoiam129

Steampunker
Yep it works. I am not using an ALU at all. That is overkill. The circuit diagram I posted is for the truth table of the add 3 step in the double dabble. By modeling that truth table we can send in 4 bits and get out the 4 bit result from the add 3 operation.
So you hard coded the truth table.. a sort of "ROM".. wow.. I was building complete full adders, hooking up 4, and using a switch to tell if the input is greater than 5 and if it is, add 3. This caused the gate smoke for me though. I'm really impressed. Do you think it could be compacted and made modular?
 

crawlr

Terrarian
Ahh you were thinking like a programmer :p . Remember, circuits are just physical representations of truth tables. Whenever you start designing a circuit start by trying to come up with the truth table of your input's and outputs. This is also how you start to reverse or understand a complex circuit someone else made, start mapping out the truth table for it to understand what it does.

There are definitely some ways to compact the design further, although I feel the current build is pretty efficient. I kept consistent wire colors for my inputs and outputs. If you removed this restraint and varied the wire color from circuit to circuit you can be a little more flexible with the layout. I am also not 100% sure that my circuit design is perfectly efficient. I built it myself without a reference so someone that is better with boolean logic may be able to look at it and simplify the design overall, idk.
 

idkwhoiam129

Steampunker
Ahh you were thinking like a programmer :p . Remember, circuits are just physical representations of truth tables. Whenever you start designing a circuit start by trying to come up with the truth table of your input's and outputs. This is also how you start to reverse or understand a complex circuit someone else made, start mapping out the truth table for it to understand what it does.

There are definitely some ways to compact the design further, although I feel the current build is pretty efficient. I kept consistent wire colors for my inputs and outputs. If you removed this restraint and varied the wire color from circuit to circuit you can be a little more flexible with the layout. I am also not 100% sure that my circuit design is perfectly efficient. I built it myself without a reference so someone that is better with boolean logic may be able to look at it and simplify the design overall, idk.
Funny.. I started with redstone in Minecraft and THEN moved to program.. Strange how I adapted programming more than the redstone. Or maybe I never really learned that stuff.
I can definitely try to compact it a bit. I have a few ideas on how to do so.
 

crawlr

Terrarian
Thinking about it more, I realize a lot of people that may be looking for a binary to BCD approach probably have a specific use case. If you are just counting something and want to display the result with a 7-segment display/etc, then it is much easier to simply count directly in BCD. The point of this thread is to show how to build a combinational circuit to achieve double dabble, but if you just want to count we can make it MUCH easier.


First we would need a binary counter that self resets after 9:
JQBtjt0.png


You can then connect multiple of these modules together to easily count directly in BCD:
0ZaDwYy.png
 

idkwhoiam129

Steampunker
Here's what I've done... I don't know if it's more compact, it may be, but its definitely more tile-able, in a sense. It's also top to bottom instead of left to right.
f3d9c2c393.png

The bottom wires are rotated over to the right for the shifting part of double dabble
And here's an 8 bit binary to BCD
3b7b9d9f89.png
 

DicemanX

Brain of Cthulhu
Very interesting approach! Given that Terraria's pulse-based system allows for a ton of flexibility, I'm sure we'll get a wide variety of constructions in due time.

I actually decided on a much different approach that you might be interested in. I already built all the converters prior to 1.3.1 using hoiktronics (http://forums.terraria.org/index.ph...culator-with-decimal-inputs-and-output.32610/), so I decided to mimic my approaches using the 1.3.1 logic gates. Specifically I want to recreate my adder with decimal inputs and outputs and expand it to include other arithmetic functions. I'd do it by now except that I cannot get TEdit to work either and I don't plan to build something so gigantic by hand.

For my converters I'm using a type of 1-to-n decoder (I used the term "giant logic gates" in past threads to describe such decoders). Here's an example of the SUBTRACT-3 function that I use in the binary-to-BCD conversion via reverse-double-dabble (described here: http://forums.terraria.org/index.php?threads/project-bcd-to-binary-converter.30458/):


The 1-to-n decoder is on the right, and it generates outputs in the form of subtraction instructions that are processed by the system of gates near the bottom. For instance, when the 8 and 1 inputs are ON, the signal emerging from the 1-to-n decoder is the instruction "switch from 9 to 6". That operation is achieved by switching OFF the 1, and turning the 2 and 4 ON.

These can be chained together to handle conversion of multi-digit numbers. Similar approaches can be used in making the ADD-3 function and chaining the ADD-3s in a manner similar to yours.

I have not attempted to compress this build - the purpose of the video is to showcase how the build functions and I spaced out the gates to make it easier to understand the wiring.

Here's the schematic showing the branching in the SUBTRACT-3 decoder:

s3VAIXy.jpg



The signal starts in the far left node, represented by the black circle. The blue lines are the input lines, and when all inputs are OFF the signal always takes the lower path at each node. So, for instance, if 8 and 2 are ON, the signal emerges via the "10 to 7" output line, and goes on to modify the binary number from 10 to 7.

The starting node was only necessary pre-1.3.1 because a signaling agent would first pass through that node. In 1.3.1 I only need to use 4 nodes. The nodes can be constructed by pairing two AND gates in this fashion (described in detail here: http://forums.terraria.org/index.ph...ogic-gates-using-a-systematic-approach.43884/):

ICZy1LL.png
qGVlxsf.png


The green wire is the input wire, and the blue and yellow wires are the two output lines. A signal from a preceeding node travels through the red wire.

In any event this approach isn't better that what you presented since it actually ends up using slightly more gates (mainly because of the annoyance of having to equalize the number of gates signals pass through before they reconvene). However, I thought you might be curious about other approaches since you mentioned in your opening post that you haven't seen other approaches used yet. The 1-to-n decoder approach makes it easier to build any kind of converter at the cost of not achieving ultimate compressions of the builds.
 
Last edited:

idkwhoiam129

Steampunker
Very interesting approach! Given that Terraria's pulse-based system allows for a ton of flexibility, I'm sure we'll get a wide variety of constructions in due time.


The 1-to-n decoder is on the right, and it generates outputs in the form of subtraction instructions that are processed by the system of gates near the bottom. For instance, when the 8 and 1 inputs are ON, the signal emerging from the 1-to-n decoder is the instruction "switch from 9 to 6". That operation is achieved by switching OFF the 1, and turning the 2 and 4 ON.
However, wouldn't this method require 2^N-1 nodes where N is the width of the binary value? Then each node would have to be hard coded by hand... Or, am I missing something or doing it wrong?
 

DicemanX

Brain of Cthulhu
However, wouldn't this method require 2^N-1 nodes where N is the width of the binary value? Then each node would have to be hard coded by hand... Or, am I missing something or doing it wrong?

What you are describing is a single 1-to-n decoder that converts the entire binary number to BCD (which would be easy to construct but would be way too large), whereas I only presented a decoder that performs a -3 function (and can be modified to perform +3 functions as well). The plan is to use reverse double-dabble to do the conversion from BCD to binary, which means that after each SUBTRACT-3 operation we do a shift. Pre-1.3.1 using hoiks, we could create stationary SUBTRACT-3 decoders and simply shift the signals into the decoders, but in 1.3.1 using logic gates the solution is to instead chain multiple SUBTRACT-3 decoders. The actual shifting is not done by physically transferring the signals; instead, it happens automatically based on how the decoders are linked.

This is exactly what @crawlr presented in his opening post for the binary-to-BCD conversion - he showcased the ADD-3 decoder then chained them together in the converter for 8-bit binary numbers. All I'm doing is showing an alternate way that the ADD-3 or SUBTRACT-3 can be done, but the way to chain the decoders is a separate matter which hasn't been discussed in this thread yet (only shown via crawlr's picture of his 8-bit converter).

One of the links I provided above already shows the finished product pre-1.3.1 using hoiks. Once I build the full converters using 1.3.1 logic gates we'll be able to see how the decoders can be chained together.
 
Last edited:

crawlr

Terrarian
@idkwhoiam129 Your version of the ADD3 module is definitely more compact. I think the top down design as opposed to my left to right is also nice since it allows you to more easily follow the bit shift and not get lost in the wires. If you count the square area of each module including room for padding due to wire colors, yours is about 23% smaller than mine.

@DicemanX I really like the idea of using 1-to-n decoders. This is an elegant approach I totally overlooked. Those decoders are deceptively useful and could be used in a lot of other situations where complex combinational circuits are more traditional. In the real world the fact that a 1-to-n decoder often require more gates to function means it results in a larger power-product time, aka greater latency on the circuit. But in the wonderful realm of terraria we don't have to deal with that and thus the design that balances space and clarity usually wins.

For everyone else following at home, the three of us keep mentioning the bit shift part of double dabble. No one in this thread has explicitly pointed out how we are handling that but if you trace threw the 8-bit converter examples it should be apparent. BUT, if you're having trouble wrapping your head around it here is a diagram that should help clarify:
US20130246490A1-20130919-D00004.png


The "Conditional Adder" blocks represent either the combinational ADD3 module, or DicemanX's decorder module. You can see that the highest three bits from our input are fed into the first adder. After that the result is shifted by connecting the output of each adder one bit over to the next one. @idkwhoiam129 's version closely represents this diagram and if you put them side by side things should be pretty clear.

Thanks for all the feedback everyone! It'll be exciting to see who builds something that requires this type of logic and what approach they end up using to fit with their specific design. :cool:

EDIT: I should probably mention I did not create that diagram. It was lifted from a US patent application for a binary to BCC chip with an optional BCD conversion step. US 20130246490 A1
 
Last edited:

idkwhoiam129

Steampunker
Thanks for all the feedback everyone! It'll be exciting to see who builds something that requires this type of logic and what approach they end up using to fit with their specific design. :cool:
I think it could very well be used with computer or calculator displays :) I might try to get some sort of calculator built. Has anyone built a full blown computer with the new gates yet? I haven't seen one on the forums.. I've built one myself but I plan on compacting it.
 

crawlr

Terrarian
When people say they built a computer it's kinda confusing. Most people mean they built a fancy ALU, or have you expanded your build to actually work with a clock signal/registers/and have its own instruction set? (aka an actual processor not just an ALU) either way you should clean it up and make a thread for it!
 

idkwhoiam129

Steampunker
When people say they built a computer it's kinda confusing. Most people mean they built a fancy ALU, or have you expanded your build to actually work with a clock signal/registers/and have its own instruction set? (aka an actual processor not just an ALU) either way you should clean it up and make a thread for it!
Yeah... I mean.. "Computer" just draws the attention of more people haha XP What I have is an ALU, 2 cells of RAM, and program memory. I could hook up a program counter and clock, but I haven't done that yet. I might make a Fibonacci "processor" program of sorts, since it's well known. I might clean it up soon :)
 
Top Bottom