~moody/pse

pse/section.go -rw-r--r-- 1.1 KiB
e4de17d5 — Jacob Moody Introduce pse(1) 9 months ago
                                                                                
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
54
55
56
57
58
59
60
61
62
63
package pse

const (
	SectionTI = iota
	SectionInv
	SectionState
	SectionMisc
	SectionRiv
	SectionPCA
	SectionPCB
	SectionPCC
	SectionPCD
	SectionPCE
	SectionPCF
	SectionPCG
	SectionPCH
	SectionPCI
)

type Section struct {
	ID       uint16
	Checksum uint16
	Index    uint32

	//raw data
	data [4096]byte
}

func (s *Section) Load(data [4096]byte) {
	copy(s.data[:], data[:])
	s.ID = get2(s.data[0xFF4:0xFF6])
	s.Checksum = get2(s.data[0xFF6:0xFF8])
	s.Index = get4(s.data[0xFFC:0x1000])
}

func sectionDataLen(index uint32) int {
	switch index {
	case SectionTI:
		return 3884
	case SectionRiv:
		return 3848
	case SectionPCI:
		return 2000
	default:
		return 3968
	}
}

func (s *Section) Commit() {
	//In theory these values could not be changed by the user safely
	//but we expose them so they should be changed if the user wishes
	put2(s.ID, s.data[0xFF4:0xFF6])
	put4(s.Index, s.data[0xFFC:0x1000])

	checksumLen := sectionDataLen(s.Index)
	var checksum uint16
	var tmp uint32 = 0
	for i := 0; i < checksumLen; i += 4 {
		tmp += get4(s.data[i : i+4])
	}
	checksum = uint16(tmp>>16) + uint16(tmp)
	put2(checksum, s.data[0xFF6:0xFF8])
}