Conditional branching
Apr 28, 09:43 AM
Arguably the key feature that differentiates a computer from other kinds of logic is conditional branching. A computer must be able to execute different code based on conditions. Many instruction sets dedicate a great deal of space to different kinds of conditions, but everybody has to have at least one conditional instruction to be able to execute arbitrary code.
Today, Computer.Build acquired the ability to synthesize conditionals in instructions. This is completely general-purpose, so you could write an instruction that adds 32 to the Program Counter if it’s a multiple of 4, or some such thing. I’m using it right now to implement a “branch if accumulator equals 0” instruction, but it’s general enough to implement just about anything. It’s only supported in the Ruby implementation, but now that I have a grip on how to do it, the Clojure version is coming soon. Here’s an example of what you can do
computer.instruction "bra0" do |i|
i.if equal(:A, 0) do |thn|
thn.move :pc, bitwise_and(:IR, 0x0F)
end
end
I’m not thrilled with the block-parameter metaprogramming technique here, but some of my DSL keywords are Ruby keywords, so instance_eval isn’t going to cut it. The Clojure DSL will be a lot nicer, but then again Lisp is almost cheating :)
With conditionals finished, Computer.Build is pretty much complete as far as this semester goes. Indirect addressing is just a matter of writing the appropriate microcode, which I’ve mostly finished, and multi-byte instructions can go the same route (the microcode just has to add N to the Program Counter). Sure, the computer isn’t very efficient, but it takes 0.05 seconds to generate. The next person who picks this project up can work on data path optimization and other things to make all Computer.Build-generated processors better.