<meta http-equiv="content-type" content="text/html; charset=utf-8"><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">(note: I sent this to the mailing list, under the misunderstanding that this was the SIMH maintainer's email from the bottom of the SIMH page; after mailman bounced it, I joined the list and am resending it; while it starts out talking about altair32, I do get around to SIMH before its done ;-) ).</span></font><div>

<font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br>

</span></font><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">I've been doing some work on Vince Briel's excellent AVR emulation of the 8080 and while rewriting DAA I came across what I think to be a harmless bug but thought you might want to comment on it.</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">From what I can glean DAA effects all flags including half carry. And I _think_ that half carry occurs from the +6 (if it happens at all). So if you don't adjust the LSD you get AC=0. If you add 6 then if adding the 6 gives you a carry out of Bit 3 you get AC set. Note that the carry might ripple so that's NOT to say Bit 4 is necessarily 1.</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">Here's part of Altair32's code:</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><div>static void<span style="white-space: pre-wrap; ">  </span>daa ( OP_ARG_U ) </div>

<div>{</div><div>// Decimal Adjust Accumulator</div><div>// DAA:: A=BCD format</div><div>// Flags:<span style="white-space: pre-wrap; ">      </span>SZAPC</div><div>//<span style="white-space: pre-wrap; ">                       </span>*****</div>

<div><br></div><div><span style="white-space: pre-wrap; ">      </span>register /* FJS */ word tmp = ACCUM;</div><div><br></div><div><span style="white-space: pre-wrap; "> </span>if ( (( tmp & 0x0f ) > 0x09 ) || ( FLAGS & AC_FLAG ))</div>

<div><span style="white-space: pre-wrap; ">               </span>tmp += 0x06;</div><div><br></div><div><span style="white-space: pre-wrap; "> </span>if (tmp > 0x0f)</div><div><span style="white-space: pre-wrap; ">            </span>FLAGS |= AC_FLAG;<span style="white-space: pre-wrap; ">            </span>// if adjusted LSB > 0xf, set AC</div>

<div><span style="white-space: pre-wrap; ">       </span>else</div><div><span style="white-space: pre-wrap; ">          </span>FLAGS &= ~AC_FLAG;<span style="white-space: pre-wrap; ">               </span>// else clear SC<span style="white-space: pre-wrap; ">     </span></div>

<div><br></div></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">So since tmp is not masked off, any value >0xF gets AC set even if no carry or add occurred! In other words, pretend the value of ACCUM is 90 (and thus temp is 90) with AC=0. The first if does not fire. The second if does and AC gets set. That's got to be wrong. Granted, who checks AC after DAA? But still.... </div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">

So why copy Bob? Well... I think SIMH has a similar but different problem. Here's a snip from the DAA code:</div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">

<div>/*opcode=0x27*/</div><div>static void i86op_daa(PC_ENV *m)</div><div>{</div><div>   uint16 dbyte;</div><div>   dbyte = m->R_AL;</div><div>   if (ACCESS_FLAG(m,F_AF)|| (dbyte&0xf) > 9)</div><div>     {</div>

<div>    dbyte += 6;</div><div>    if (dbyte&0x100)</div><div>      SET_FLAG(m, F_CF);</div><div>    SET_FLAG(m, F_AF);</div><div>     }</div><div>   else</div></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">

<br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">Here we add 6 to dbyte and then AC is always set. If no +6 then AC is cleared. This COULD be correct behavior, but I can't find any reference material that says it is. In any event, SIMH and Altair32 are doing something different here, so they both can't be right. Meanwhile I have my own version of DAA in AVR assembler that I will spare you unless you ask. The only real silicon I have even close to operational at the moment is a Z80 and not only is it not operational, the DAA is one place where it is a lot different so I don't trust the result there.</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">Oh. One other note about Altair32. In the debugger, the A and FLAGS display is swapped in the debug console window. The Register display up top left is ok though. I bet you've heard that one before.</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">If either of you can show documentation on the AC flag after DAA or point to a real piece of silicon's behavior I'd like to know so I can fix the DAA code in the Briel emulator to match. Otherwise, I thought you'd like to know about this bug even though it is pretty innocuous as far as I can tell.</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">

Al Williams</div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><a href="http://www.ddj.com/embedded" target="_blank" style="color: rgb(6, 88, 181); ">http://www.ddj.com/embedded</a> (among others)</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div></div>