-
Notifications
You must be signed in to change notification settings - Fork 20
/
table.go
53 lines (43 loc) · 1.59 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package polycounter
import (
"fmt"
)
var table6bit []string
// number of entries in 6 bit polycounter table.
const LenTable6Bit = 64
// initialise table for a 6bit polycounter. the VCS only uses 6 bit
// polycounters but the following method can be used to produce tables of any
// length.
func init() {
mask := (1 << 6) - 1
format := fmt.Sprintf("%%0%db", 6)
table6bit = make([]string, 1<<6)
table6bit[0] = fmt.Sprintf(format, 0)
var p int
for i := 1; i < len(table6bit); i++ {
p = ((p & (mask - 1)) >> 1) | (((p&1)^((p>>1)&1))^mask)<<5
p &= mask
table6bit[i] = fmt.Sprintf(format, p)
}
// sanity check that the table has looped correctly
if table6bit[len(table6bit)-1] != table6bit[0] {
panic("error creating 6 bit polycounter")
}
// force the final value to be the invalid polycounter value. this is only
// ever useful for specifying the reset pattern
table6bit[len(table6bit)-1] = fmt.Sprintf(format, mask)
}