1

I'm trying to create a 8086 processor in Verilog, and I have a better-than-average fundamental understanding of most of the architecture (and can get along happily once I get past this point), but I can't seem to wrap my head around how the Carry and Auxiliary flags function in the ALU.

I understand that CF is triggered upon an addition or subtraction (in which case it's called borrow) that would cause the result to be larger than the bit width of the ALU.

But, how would I write Verilog code for addition and subtraction that would allow me to write to the FLAGS[0] (CF) bit and then re-access it to continue the operation? Can anyone give me examples that I can deconstruct?

Also, this is even more of a n00b question, but how is it that an ALU with carry operation can support creation of a 17-bit number if the SI and DI registers are only 16 bits in width? Where does that extra bit go or what gets done with it? What happens if multiplication creates that same bit overflow?

Many apologies for the novice-level questions. I almost feel like I'm set to get yelled at for some obvious ignorance or lack of understanding with regards to this. Many thanks to anyone who can help and give code lines for understanding to elucidate this for me.

ecfedele
  • 306
  • 3
  • 15

1 Answers1

1

I don't quite know what you mean and then re-access it to continue the operation, but if you're just asking how you can generate a carry bit from a 16 bit addition/subtraction, this is one way to do it (use concatenation to write result to two different registers):

always @ posedge clk begin
  if(add_with_carry)
    {CF[0], result[15:0]} <= a[15:0] + b[15:0];
  else if(sub_with_carry)
    {CF[0], result[15:0]} <= a[15:0] - b[15:0];
  else if(add_without_carry)
            result[15:0]  <= a[15:0] + b[15:0];
  else if(sub_without_carry)
            result[15:0]  <= a[15:0] - b[15:0];
end

This is also basically the same thing as writing the result to a 17 bit register, and then just designating result[16] as the carry flag.

Tim
  • 35,413
  • 11
  • 95
  • 121
  • Oh okay. So the result of such an operation would be the combination of the CF FLAGS bit and then the register result. That makes a lot more sense to me - I, in my folly, thought somehow the ALU summed them together and put them somewhere, hah. – ecfedele Aug 03 '13 at 05:34
  • Now, what occurs if the processor is told to add without carry two values which would set the CF bit? – ecfedele Aug 03 '13 at 05:41
  • If you need additional operation that does add without touching the carry bit, then you can just add additional clauses to the `if` there to do different things (like do addition without writing to the CF). There's lots of different ways it could be done. – Tim Aug 03 '13 at 15:32
  • Here, let me rephrase it a bit - what would occur if I gave the processor the ADD command (add without carry) but with two values that would overflow into 17-bit and therefore set the carry bit? Also, is there any equivalent in multiplication - that is, what happens for a multiplication operation if the output overflows? – ecfedele Aug 03 '13 at 23:26
  • See the edited answer and see if that makes sense to you. You could do something similar for multiplication as well. – Tim Aug 04 '13 at 00:45