No Description

stfw.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package stfw
  2. import (
  3. "encoding/binary"
  4. //"fmt"
  5. "os"
  6. )
  7. type Record struct {
  8. Addr uint32
  9. Data []byte
  10. Sum uint8
  11. ComputedSum uint8
  12. Good bool
  13. }
  14. func ReadRecord(f *os.File) (*Record, error) {
  15. r := struct {
  16. Data [36]byte
  17. Sum uint8
  18. }{}
  19. err := binary.Read(f, binary.BigEndian, &r)
  20. if err != nil {
  21. return nil, err
  22. }
  23. var t uint8
  24. for _, b := range r.Data {
  25. t += uint8(b)
  26. }
  27. t = ^t
  28. record := &Record{
  29. Addr: uint32(r.Data[0])<<24 | uint32(r.Data[1])<<16 | uint32(r.Data[2])<<8 | uint32(r.Data[3]),
  30. Data: r.Data[4:],
  31. Sum: r.Sum,
  32. ComputedSum: t,
  33. Good: r.Sum == t,
  34. }
  35. return record, nil
  36. }
  37. func WriteRecord(f *os.File, rec *Record) error {
  38. r := struct {
  39. Data [36]byte
  40. Sum uint8
  41. }{}
  42. r.Data[0] = byte(rec.Addr >> 24)
  43. r.Data[1] = byte(rec.Addr >> 16)
  44. r.Data[2] = byte(rec.Addr >> 8)
  45. r.Data[3] = byte(rec.Addr)
  46. copy(r.Data[4:], rec.Data[:])
  47. for _, b := range r.Data {
  48. r.Sum += uint8(b)
  49. }
  50. r.Sum = ^r.Sum
  51. err := binary.Write(f, binary.BigEndian, r)
  52. if err != nil {
  53. return err
  54. }
  55. return nil
  56. }
  57. func SplitBin(mem []byte) ([]byte, []byte) {
  58. low := make([]byte, len(mem)>>1)
  59. high := make([]byte, len(mem)>>1)
  60. for i := 0; i < len(mem); i += 2 {
  61. low[i/2] = mem[i]
  62. high[i/2] = mem[i+1]
  63. }
  64. return low, high
  65. }
  66. func MergeBin(low []byte, high []byte) []byte {
  67. mem := make([]byte, len(low)*2)
  68. for i := 0; i < len(low); i++ {
  69. mem[i*2] = low[i]
  70. mem[i*2+1] = high[i]
  71. }
  72. return mem
  73. }
  74. type MemoryMode int
  75. func (m MemoryMode) physAddr(addr int) int {
  76. switch m {
  77. case MemoryModeCombinedLow:
  78. return addr * 2
  79. case MemoryModeCombinedHigh:
  80. return addr*2 + 1
  81. }
  82. return addr
  83. }
  84. const (
  85. MemoryModeLinear MemoryMode = iota
  86. MemoryModeCombinedLow
  87. MemoryModeCombinedHigh
  88. )
  89. func GetChecksum(mem []byte, mm MemoryMode) uint16 {
  90. return uint16(mem[mm.physAddr(0xfffe)])*256 + uint16(mem[mm.physAddr(0xffff)])
  91. }
  92. func GetChecksums(mem []byte) (uint16, uint16) {
  93. return GetChecksum(mem, MemoryModeCombinedLow), GetChecksum(mem, MemoryModeCombinedHigh)
  94. }
  95. func CalcChecksum(mem []byte, mm MemoryMode, start int) uint16 {
  96. var cs uint16
  97. for i := 0; i <= 0xFFFB; i++ {
  98. cs = addx(uint16(mem[mm.physAddr(i)]), cs)
  99. }
  100. cs = ^cs
  101. return cs
  102. }
  103. func CalcChecksums(mem []byte, start int) (uint16, uint16) {
  104. return CalcChecksum(mem, MemoryModeCombinedLow, start), CalcChecksum(mem, MemoryModeCombinedHigh, start)
  105. }
  106. func UpdateChecksum(mem []byte, mm MemoryMode, cs uint16) {
  107. mem[mm.physAddr(0xfffe)] = byte(cs >> 8)
  108. mem[mm.physAddr(0xffff)] = byte(cs)
  109. }
  110. var xFlag bool
  111. func addx(x, y uint16) uint16 {
  112. if x+y < y {
  113. xFlag = true
  114. return x + y
  115. }
  116. if xFlag {
  117. xFlag = false
  118. return x + y + 1
  119. }
  120. return x + y
  121. }