1 // Copyright (c) 1996 Barry Silverman, Brian Silverman, Vadim Gerasimov.
2 // Portions Copyright (c) 2009 The Go Authors.
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // See ../../nacl/README.
40 runtime.LockOSThread()
42 go srpc.ServeRuntime()
45 w, err := av.Init(av.SubsystemVideo, 512, 512)
47 log.Exitf("av.Init: %s", err)
56 f := bytes.NewBuffer([]byte(spacewarCode))
57 if err = m.Load(f); err != nil {
58 log.Exitf("loading %s: %s", "spacewar.lst", err)
61 //fmt.Printf("step PC=%06o ", m.PC);
62 //fmt.Printf("inst=%06o AC=%06o IO=%06o OV=%o\n",
63 // m.Mem[m.PC], m.AC, m.IO, m.OV);
66 log.Exitf("step: %s", err)
69 func demuxEvents(w draw.Window, kc chan int) {
70 for event := range w.EventChan() {
71 switch e := event.(type) {
79 // A SpacewarPDP1 is a PDP-1 machine configured to run Spacewar!
80 // It responds to traps by drawing on the display, and it flushes the
81 // display and pauses every second time the program counter reaches
83 type SpacewarPDP1 struct {
92 colorModel image.ColorModel
97 func min(a, b int) int {
104 func (m *SpacewarPDP1) Init(ctxt draw.Window, kc chan int) {
107 m.screen = ctxt.Screen()
108 m.dx = m.screen.Bounds().Dx()
109 m.dy = m.screen.Bounds().Dy()
110 m.colorModel = m.screen.ColorModel()
111 m.pix = make([][]uint8, m.dy)
112 for i := range m.pix {
113 m.pix[i] = make([]uint8, m.dx)
115 m.cmap = make([]image.Color, 256)
116 for i := range m.cmap {
118 r = uint8(min(0, 255))
119 g = uint8(min(i*2, 255))
120 b = uint8(min(0, 255))
121 m.cmap[i] = m.colorModel.Convert(image.RGBAColor{r, g, b, 0xff})
126 frameDelay = 56 * 1e6 // 56 ms
129 var ctlBits = [...]pdp1.Word{
140 func (m *SpacewarPDP1) Step() os.Error {
146 t := time.Nanoseconds()
147 if t >= m.frameTime+3*frameDelay {
150 m.frameTime += frameDelay
151 for t < m.frameTime {
152 time.Sleep(m.frameTime - t)
153 t = time.Nanoseconds()
161 func (m *SpacewarPDP1) Trap(y pdp1.Word) {
164 x := int(m.AC+0400000) & 0777777
165 y := int(m.IO+0400000) & 0777777
166 x = x * m.dx / 0777777
167 y = y * m.dy / 0777777
168 if 0 <= x && x < m.dx && 0 <= y && y < m.dy {
169 n := uint8(min(int(m.pix[y][x])+128, 255))
177 func (m *SpacewarPDP1) flush() {
178 // Update screen image; simulate phosphor decay.
179 for y := 0; y < m.dy; y++ {
180 for x := 0; x < m.dx; x++ {
181 m.screen.Set(x, y, m.cmap[m.pix[y][x]])
188 func (m *SpacewarPDP1) pollInput() {
192 if 0 <= ch && ch < len(ctlBits) {
195 if 0 <= -ch && -ch < len(ctlBits) {
196 m.ctl &^= ctlBits[-ch]