9front - general discussion about 9front
 help / color / mirror / Atom feed
* realemu: implementing IDIV
@ 2014-01-31  9:45 Nick Owens
  2014-02-01  9:47 ` [9front] " cinap_lenrek
  0 siblings, 1 reply; 2+ messages in thread
From: Nick Owens @ 2014-01-31  9:45 UTC (permalink / raw)
  To: 9front

currently aux/realemu does not have the IDIV instruction, which is used
by newer versions of vga bioses included with qemu (1.7 here on debian
8). please review this patch which implements IDIV (signed integer
division). it should account correctly for sign extension and overflow.
see p858-860 of the intel manual.

this patch also includes a fix for writing to 0xEXXXX, which is written
to by the newer vga bios. writes to this range were previously trapped
and considered errors.

also included is a fix for overflow in the DIV instruction. on overflow,
a division by zero trap should occur. 
"Overflow is indicated with the #DE (divide error) exception rather than
with the CF flag."
see p706 of the intel manual.

http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-manual-325462.pdf

diff -r 20c2c6fccfee sys/src/cmd/aux/realemu/main.c
--- a/sys/src/cmd/aux/realemu/main.c  Sat Jan 11 16:07:35 2014 +0100
+++ b/sys/src/cmd/aux/realemu/main.c  Thu Jan 30 01:29:46 2014 -0800
@@ -286,7 +286,7 @@
  /* B */ nil,  rrealmem, wrealmem, /* RAM: VGA framebuffer */
  /* C */ memory, rmem, wmem, /* ROM: VGA BIOS */
  /* D */ nil,  rbad, wbad,
- /* E */ memory, rmem, wbad, /* ROM: BIOS */
+ /* E */ memory, rmem, wmem, /* ROM: BIOS */
  /* F */ memory, rmem, wbad, /* ROM: BIOS */
 };
 
diff -r 20c2c6fccfee sys/src/cmd/aux/realemu/xec.c
--- a/sys/src/cmd/aux/realemu/xec.c Sat Jan 11 16:07:35 2014 +0100
+++ b/sys/src/cmd/aux/realemu/xec.c Thu Jan 30 01:29:46 2014 -0800
@@ -769,12 +769,59 @@
  n = (uvlong)ar(ra)<<s | (uvlong)ar(qa);
  q = n/d;
  if(q > m)
-   trap(cpu, EGPF);
+   trap(cpu, EDIV0);
  r = n%d;
  aw(ra, r);
  aw(qa, q);
 }
- 
+
+static void
+opidiv(Cpu *cpu, Inst *i)
+{
+ Iarg *qa, *ra;
+ vlong n, q, min, max;
+ long m, r, d;
+ int s;
+
+ s = i->a1->len*8;
+ m = mask(s);
+ d = ars(i->a1);
+ if(d == 0)
+   trap(cpu, EDIV0);
+ if(s == 8){
+   qa = areg(cpu, 1, RAX);
+   ra = adup(qa);
+   ra->tag |= TH;
+ } else {
+   qa = areg(cpu, i->olen, RAX);
+   ra = areg(cpu, i->olen, RDX);
+ }
+ n = (vlong)ars(ra)<<s | (uvlong)ars(qa);
+ q = n/d;
+ r = n%d;
+
+ /* check for overflow based on operand size */
+ switch(s) {
+ case 8:
+   min = (char)0x80;
+   max = 0x7F;
+ case 16:
+   min = (short)0x8000;
+   max = 0x7FFF;
+ case 32:
+   min = (long)0x80000000;
+   max = 0x7FFFFFFF;
+ }
+
+ fprint(2, "s: %d m: %#lux d: %ld n: %lld q: %lld r: %ld min: %lld max: %lld\n",
+      s,  m,   d,   n,  q,  r, min,    max);
+
+ if(q > max || q < min)
+   trap(cpu, EDIV0);
+
+ aw(ra, r);
+ aw(qa, q);
+}  
 
 static int
 cctrue(Cpu *cpu, Inst *i)
@@ -1249,6 +1296,7 @@
  [OMUL] = opmul,
  [OIMUL] = opimul,
  [ODIV] = opdiv,
+ [OIDIV] = opidiv,
 
  [OLEA] = oplea,
  [OMOV] = opmov,



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [9front] realemu: implementing IDIV
  2014-01-31  9:45 realemu: implementing IDIV Nick Owens
@ 2014-02-01  9:47 ` cinap_lenrek
  0 siblings, 0 replies; 2+ messages in thread
From: cinap_lenrek @ 2014-02-01  9:47 UTC (permalink / raw)
  To: 9front

looks good.

--
cinap


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-02-01  9:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-31  9:45 realemu: implementing IDIV Nick Owens
2014-02-01  9:47 ` [9front] " cinap_lenrek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).