type Word uint32
-const mask = 0777777;
-const sign = 0400000;
+const mask = 0777777
+const sign = 0400000
const (
- _ = iota; // 00
+ _ = iota; // 00
opAND;
opIOR;
opXOR;
// The machine calls the Trap method to implement the
// PDP-1 IOT instruction.
type Trapper interface {
- Trap(y Word)
+ Trap(y Word);
}
// An M represents the machine state of a PDP-1.
// Clients can set Display to install an output device.
type M struct {
- AC, IO, PC, OV Word;
- Mem [010000]Word;
- Flag [7]bool;
- Sense [7]bool;
- Halt bool;
+ AC, IO, PC, OV Word;
+ Mem [010000]Word;
+ Flag [7]bool;
+ Sense [7]bool;
+ Halt bool;
}
}
type UnknownInstrError struct {
- Inst Word;
- PC Word;
+ Inst Word;
+ PC Word;
}
func (e UnknownInstrError) String() string {
if op < opSKP && op != opCALJDA {
for n := 0; ib != 0; n++ {
if n > 07777 {
- return LoopError(m.PC-1);
+ return LoopError(m.PC - 1);
}
- ib = (m.Mem[y]>>12) & 1;
+ ib = (m.Mem[y] >> 12)&1;
y = m.Mem[y] & 07777;
}
}
a = 64;
}
m.Mem[a] = m.AC;
- m.AC = (m.OV<<17) + m.PC;
- m.PC = a + 1;
+ m.AC = (m.OV << 17) + m.PC;
+ m.PC = a+1;
case opLAC:
m.AC = m.Mem[y];
case opLIO:
case opDAC:
m.Mem[y] = m.AC;
case opDAP:
- m.Mem[y] = m.Mem[y]&0770000 | m.AC&07777;
+ m.Mem[y] = m.Mem[y] & 0770000 | m.AC & 07777;
case opDIO:
m.Mem[y] = m.IO;
case opDZM:
m.Mem[y] = 0;
case opADD:
m.AC += m.Mem[y];
- m.OV = m.AC>>18;
+ m.OV = m.AC >> 18;
m.AC = norm(m.AC);
case opSUB:
diffSigns := (m.AC ^ m.Mem[y])>>17 == 1;
- m.AC += m.Mem[y]^mask;
+ m.AC += m.Mem[y] ^ mask;
m.AC = norm(m.AC);
- if diffSigns && m.Mem[y]>>17 == m.AC>>17 {
+ if diffSigns && m.Mem[y] >> 17 == m.AC >> 17 {
m.OV = 1;
}
case opIDX:
- m.AC = norm(m.Mem[y]+1);
+ m.AC = norm(m.Mem[y] + 1);
m.Mem[y] = m.AC;
case opISP:
- m.AC = norm(m.Mem[y]+1);
+ m.AC = norm(m.Mem[y] + 1);
m.Mem[y] = m.AC;
- if m.AC&sign == 0 {
+ if m.AC & sign == 0 {
m.PC++;
}
case opSAD:
m.PC++;
}
case opMUS:
- if m.IO&1 == 1 {
+ if m.IO & 1 == 1 {
m.AC += m.Mem[y];
- m.AC = norm(m.AC)
+ m.AC = norm(m.AC);
}
- m.IO = (m.IO>>1 | m.AC<<17) & mask;
+ m.IO = (m.IO >> 1 | m.AC << 17)&mask;
m.AC >>= 1;
case opDIS:
- m.AC, m.IO = (m.AC<<1 | m.IO>>17) & mask,
- ((m.IO<<1 | m.AC>>17) & mask) ^ 1;
- if m.IO&1 == 1 {
- m.AC = m.AC + (m.Mem[y]^mask);
+ m.AC, m.IO = (m.AC << 1 | m.IO >> 17)&mask,
+ ((m.IO << 1 | m.AC >> 17)&mask)^1;
+ if m.IO & 1 == 1 {
+ m.AC = m.AC + (m.Mem[y] ^ mask);
} else {
m.AC = m.AC + 1 + m.Mem[y];
}
case opJMP:
m.PC = y;
case opJSP:
- m.AC = (m.OV<<17) + m.PC;
+ m.AC = (m.OV << 17) + m.PC;
m.PC = y;
case opSKP:
- cond := y&0100 == 0100 && m.AC == 0
- || y&0200 == 0200 && m.AC>>17 == 0
- || y&0400 == 0400 && m.AC>>17 == 1
- || y&01000 == 01000 && m.OV == 0
- || y&02000 == 02000 && m.IO>>17 == 0
- || y&7 != 0 && !m.Flag[y&7]
- || y&070 != 0 && !m.Sense[(y&070)>>3]
- || y&070 == 010;
- if (ib==0) == cond {
+ cond := y&0100 == 0100 && m.AC == 0 ||
+ y&0200 == 0200 && m.AC >> 17 == 0 ||
+ y&0400 == 0400 && m.AC >> 17 == 1 ||
+ y&01000 == 01000 && m.OV == 0 ||
+ y&02000 == 02000 && m.IO >> 17 == 0 ||
+ y&7 != 0 && !m.Flag[y&7] ||
+ y&070 != 0 && !m.Sense[(y&070)>>3] ||
+ y&070 == 010;
+ if (ib == 0) == cond {
m.PC++;
}
if y&01000 == 01000 {
}
switch (inst>>9)&017 {
case 001: // rotate AC left
- m.AC = (m.AC<<1 | m.AC>>17) & mask;
+ m.AC = (m.AC << 1 | m.AC >> 17)&mask;
case 002: // rotate IO left
- m.IO = (m.IO<<1 | m.IO>>17) & mask;
+ m.IO = (m.IO << 1 | m.IO >> 17)&mask;
case 003: // rotate AC and IO left.
w := uint64(m.AC)<<18 | uint64(m.IO);
w = w<<1 | w>>35;
- m.AC = Word(w>>18) & mask;
- m.IO = Word(w) & mask;
+ m.AC = Word(w>>18)&mask;
+ m.IO = Word(w)&mask;
case 005: // shift AC left (excluding sign bit)
- m.AC = (m.AC<<1 | m.AC>>17)&mask&^sign | m.AC&sign;
+ m.AC = (m.AC << 1 | m.AC >> 17)&mask&^sign | m.AC & sign;
case 006: // shift IO left (excluding sign bit)
- m.IO = (m.IO<<1 | m.IO>>17)&mask&^sign | m.IO&sign;
+ m.IO = (m.IO << 1 | m.IO >> 17)&mask&^sign | m.IO & sign;
case 007: // shift AC and IO left (excluding AC's sign bit)
w := uint64(m.AC)<<18 | uint64(m.IO);
w = w<<1 | w>>35;
- m.AC = Word(w>>18)&mask&^sign | m.AC&sign;
- m.IO = Word(w)&mask&^sign | m.AC&sign;
+ m.AC = Word(w>>18)&mask&^sign | m.AC & sign;
+ m.IO = Word(w)&mask&^sign | m.AC & sign;
case 011: // rotate AC right
- m.AC = (m.AC>>1 | m.AC<<17) & mask;
+ m.AC = (m.AC >> 1 | m.AC << 17)&mask;
case 012: // rotate IO right
- m.IO = (m.IO>>1 | m.IO<<17) & mask;
+ m.IO = (m.IO >> 1 | m.IO << 17)&mask;
case 013: // rotate AC and IO right
w := uint64(m.AC)<<18 | uint64(m.IO);
w = w>>1 | w<<35;
- m.AC = Word(w>>18) & mask;
- m.IO = Word(w) & mask;
+ m.AC = Word(w>>18)&mask;
+ m.IO = Word(w)&mask;
case 015: // shift AC right (excluding sign bit)
- m.AC = m.AC>>1 | m.AC&sign;
+ m.AC = m.AC >> 1 | m.AC & sign;
case 016: // shift IO right (excluding sign bit)
- m.IO = m.IO>>1 | m.IO&sign;
+ m.IO = m.IO >> 1 | m.IO & sign;
case 017: // shift AC and IO right (excluding AC's sign bit)
w := uint64(m.AC)<<18 | uint64(m.IO);
w = w>>1;
- m.AC = Word(w>>18) | m.AC&sign;
- m.IO = Word(w) & mask;
+ m.AC = Word(w>>18) | m.AC & sign;
+ m.IO = Word(w)&mask;
default:
goto Unknown;
}
m.PC--;
return HaltError(m.PC);
}
- switch i, f := y&7, y&010==010; {
+ switch i, f := y&7, y&010 == 010; {
case i == 7:
for i := 2; i < 7; i++ {
m.Flag[i] = f;
}
default:
Unknown:
- return UnknownInstrError{inst, m.PC-1};
+ return UnknownInstrError{inst, m.PC - 1};
}
return nil;
}
i := 1;
a := Word(0);
for ; i < len(line) && '0' <= line[i] && line[i] <= '7'; i++ {
- a = a*8 + Word(line[i] - '0');
+ a = a*8 + Word(line[i]-'0');
}
- if i >= len(line) || line[i] != '\t' || i == 1{
+ if i >= len(line) || line[i] != '\t' || i == 1 {
continue;
}
v := Word(0);
j := i;
for i++; i < len(line) && '0' <= line[i] && line[i] <= '7'; i++ {
- v = v*8 + Word(line[i] - '0');
+ v = v*8 + Word(line[i]-'0');
}
if i == j {
continue;
}
return nil;
}
-
func TestInt(t *testing.T) {
reqs := NewInt("requests");
if reqs.i != 0 {
- t.Errorf("reqs.i = %v, want 4", reqs.i)
+ t.Errorf("reqs.i = %v, want 4", reqs.i);
}
if reqs != Get("requests").(*Int) {
- t.Errorf("Get() failed.")
+ t.Errorf("Get() failed.");
}
reqs.Add(1);
reqs.Add(3);
if reqs.i != 4 {
- t.Errorf("reqs.i = %v, want 4", reqs.i)
+ t.Errorf("reqs.i = %v, want 4", reqs.i);
}
if s := reqs.String(); s != "4" {
func TestString(t *testing.T) {
name := NewString("my-name");
if name.s != "" {
- t.Errorf("name.s = %q, want \"\"", name.s)
+ t.Errorf("name.s = %q, want \"\"", name.s);
}
name.Set("Mike");
if name.s != "Mike" {
- t.Errorf("name.s = %q, want \"Mike\"", name.s)
+ t.Errorf("name.s = %q, want \"Mike\"", name.s);
}
if s := name.String(); s != "\"Mike\"" {
colours.Add("red", 2);
colours.Add("blue", 4);
if x := colours.m["red"].(*Int).i; x != 3 {
- t.Errorf("colours.m[\"red\"] = %v, want 3", x)
+ t.Errorf("colours.m[\"red\"] = %v, want 3", x);
}
if x := colours.m["blue"].(*Int).i; x != 4 {
- t.Errorf("colours.m[\"blue\"] = %v, want 4", x)
+ t.Errorf("colours.m[\"blue\"] = %v, want 4", x);
}
// colours.String() should be '{"red":3, "blue":4}',
s := colours.String();
j, ok, errtok := json.StringToJson(s);
if !ok {
- t.Errorf("colours.String() isn't valid JSON: %v", errtok)
+ t.Errorf("colours.String() isn't valid JSON: %v", errtok);
}
if j.Kind() != json.MapKind {
- t.Error("colours.String() didn't produce a map.")
+ t.Error("colours.String() didn't produce a map.");
}
red := j.Get("red");
if red.Kind() != json.NumberKind {
- t.Error("red.Kind() is not a NumberKind.")
+ t.Error("red.Kind() is not a NumberKind.");
}
if x := red.Number(); x != 3 {
- t.Error("red = %v, want 3", x)
+ t.Error("red = %v, want 3", x);
}
}
package math
const (
- uvnan = 0x7FF0000000000001;
- uvinf = 0x7FF0000000000000;
- uvneginf = 0xFFF0000000000000;
- mask = 0x7FF;
- shift = 64 - 11 - 1;
- bias = 1022;
+ uvnan = 0x7FF0000000000001;
+ uvinf = 0x7FF0000000000000;
+ uvneginf = 0xFFF0000000000000;
+ mask = 0x7FF;
+ shift = 64-11-1;
+ bias = 1022;
)
// Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.
// IsNaN returns whether f is an IEEE 754 ``not-a-number'' value.
func IsNaN(f float64) (is bool) {
x := Float64bits(f);
- return uint32(x>>shift) & mask == mask && x != uvinf && x != uvneginf;
+ return uint32(x>>shift)&mask == mask && x != uvinf && x != uvneginf;
}
// IsInf returns whether f is an infinity, according to sign.
return;
}
x := Float64bits(f);
- exp = int((x>>shift)&mask) - bias;
+ exp = int((x>>shift)&mask)-bias;
x &^= mask<<shift;
x |= bias<<shift;
frac = Float64frombits(x);
// It returns frac × 2<sup>exp</sup>.
func Ldexp(frac float64, exp int) float64 {
x := Float64bits(frac);
- exp += int(x>>shift) & mask;
+ exp += int(x>>shift)&mask;
if exp <= 0 {
return 0; // underflow
}
x &^= 1<<(64-11-e) - 1;
}
int = Float64frombits(x);
- frac = f - int;
+ frac = f-int;
return;
}
-