How does a computer calculate a sin value? Logically, when I think about it the only apparent way is to put many sin values into memory, and when a sin value needs to be "calculated" it would just pull data from a specific memory address.(ex. sin(x) would pull data from the memory address containing the value of sin(x) )That seems like the only possible way to do it. Or is there a function which can be used to calculate the sin of a value? I'm really trying to ask how a computer calculates sin on a base level. Is there a way to approximate sin values using a different function composed of more "basic" operations, and the ALU would be able to do multiple "basic" operations to approximate the sin value, or is it just pulling values from memory?
Electronic – How do computers calculate sin values?
In terms of "separating" the functions, that's not really how digital logic works. Digital logic is "always doing everything". You need a mux (multiplexer) in there. The multiplexer is used to pick the right output from all of those generated.
Assume inputs A and B, output Q. Assume the ALU does two different things: Q=A+B, or Q=A&B.
The ALU will have an adder. It will also have a big AND gate.
A and B both go to the adder, and the AND gate. Always. Every moment of every day, the adder is adding A and B, and the gate is ANDing A and B.
The mux is used to select which one of the outputs we want to pass to Q. If the control signals to the ALU say "add", then the mux will select the output of the adder and pass it to Q; the output of the AND gate is unused. If the control says "and", the mux will select the output of the AND gate and pass it to Q instead, while the output of the adder is unused.
Imagine A = 0b0001 and B = 0b0010 on the inputs of the ALU. The adder is always producing 0b0011, and the AND gate is always producing 0b0000. If you provide the "add" control signal, the 0b0011 is passed to Q. You can leave A and B alone, and change the control signal to "and", then 0b0000 is passed to Q.
So you want to be able to display the ALU result both before and after it is stored in memory, and be able to load that result into the ALU's a or b input?
Connect all the data lines together to form a bus. Obviously this bus can only carry one 8 bit value at a time, so each device connected to it needs an input register (8 bit latch) to write (STORE) values to it, and a tri-state buffer to connect its output to the bus only when being read.
Your memory already has input registers, so it just needs a tri-state buffer on the output. This buffer is enabled when you want to read (LOAD) the memory into the ALU or display.
The ALU needs two input latches (let's call them Register A and Register B). It also needs a buffer on the output, which is enabled when displaying the result and/or writing it to memory.
The display is write-only, so just connect it to the bus and it will show whatever is there.
Now create a sequence to control it. Let's say you want to load the ALU with two numbers taken from different memory locations, add them together, and store the result in another memory location. This can be done in 3 steps, each of which requires one bus cycle:-
Set the first memory address, enable the memory's output buffer, wait a bit for the data to stabilize and then latch it into register A.
Set the second memory address and latch the data into register B.
Disable the memory's output buffer, set the destination address, enable the ALU's output buffer and write the data to memory.
Typically high resolution sin(x) functions would be implemented with a CORDIC (COrdiate Rotation DIgital Computer) algorithm, which can be accomplished with a small number of iterations using only shifts and add/subtract and a small lookup table. The original paper The CORDIC Computing Technique by Jack Volder is from 1959. It also works nicely when implemented with hardware in an FPGA (and a similar algorithm would be implemented in a hardware FPU for those micros that have an FPU).
For lower resolution, for example to create synthesized sine wave for an inverter or motor VFD (Variable Frequency Drive), a lookup table (LUT) with or without interpolation works well. It's only necessary to store the values for one quadrant of the sine wave because of symmetry.
As @Temlib points out, the algorithms used inside modern FPUs use range reduction followed by an evaluation using something like the Remez algorithm to limit the maximum absolute error. More can be found in this Intel paper Formal verification of floating point trigonometric functions.