Use ioutil.ReadFile()

This commit is contained in:
Ivan Izaguirre 2019-05-15 16:01:04 +02:00
parent 9fa6b4d1c0
commit 51c7a8cb57
4 changed files with 14 additions and 64 deletions

View File

@ -1,10 +1,9 @@
package apple2 package apple2
import ( import (
"bufio"
"fmt" "fmt"
"go6502/core6502" "go6502/core6502"
"os" "io/ioutil"
"time" "time"
) )
@ -185,24 +184,14 @@ func (a *Apple2) releaseFastMode() {
} }
func (a *Apple2) loadRom(filename string) { func (a *Apple2) loadRom(filename string) {
f, err := os.Open(filename) bytes, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer f.Close() size := len(bytes)
stats, statsErr := f.Stat()
if statsErr != nil {
panic(err)
}
size := stats.Size()
if size != apple2RomSize && size != apple2eRomSize { if size != apple2RomSize && size != apple2eRomSize {
panic("Rom size not supported") panic("Rom size not supported")
} }
bytes := make([]byte, size)
buf := bufio.NewReader(f)
buf.Read(bytes)
romStart := 0 romStart := 0
if size == apple2eRomSize { if size == apple2eRomSize {

View File

@ -1,9 +1,6 @@
package apple2 package apple2
import ( import "io/ioutil"
"bufio"
"os"
)
/* /*
https://applesaucefdc.com/woz/reference2/ https://applesaucefdc.com/woz/reference2/
@ -141,29 +138,19 @@ func newCardDisk2(filename string) *cardDisk2 {
} }
func loadCardRom(filename string) []memoryPage { func loadCardRom(filename string) []memoryPage {
f, err := os.Open(filename) bytes, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer f.Close()
stats, statsErr := f.Stat()
if statsErr != nil {
panic(err)
}
size := stats.Size()
bytes := make([]byte, size)
buf := bufio.NewReader(f)
buf.Read(bytes)
size := len(bytes)
pages := size / 256 pages := size / 256
if (size % 256) > 0 { if (size % 256) > 0 {
pages++ pages++
} }
rom := make([]romPage, pages) rom := make([]romPage, pages)
for i := int64(0); i < size; i++ { for i := 0; i < size; i++ {
rom[i>>8].burn(uint8(i), bytes[i]) rom[i>>8].burn(uint8(i), bytes[i])
} }

View File

@ -1,9 +1,8 @@
package apple2 package apple2
import ( import (
"bufio"
"fmt" "fmt"
"os" "io/ioutil"
) )
/* /*
@ -28,24 +27,15 @@ func NewCharacterGenerator(filename string) *CharacterGenerator {
} }
func (cg *CharacterGenerator) load(filename string) { func (cg *CharacterGenerator) load(filename string) {
f, err := os.Open(filename) bytes, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer f.Close() size := len(bytes)
stats, statsErr := f.Stat()
if statsErr != nil {
panic(err)
}
size := stats.Size()
if size != rev7CharGenSize { if size != rev7CharGenSize {
panic("Character ROM size not supported") panic("Character ROM size not supported")
} }
cg.data = make([]uint8, size) cg.data = bytes
buf := bufio.NewReader(f)
buf.Read(cg.data)
} }
func (cg *CharacterGenerator) getPixel(char uint8, row int, column int) bool { func (cg *CharacterGenerator) getPixel(char uint8, row int, column int) bool {

View File

@ -1,9 +1,6 @@
package core6502 package core6502
import ( import "io/ioutil"
"bufio"
"os"
)
// Memory represents the addressable space of the processor // Memory represents the addressable space of the processor
type Memory interface { type Memory interface {
@ -19,7 +16,7 @@ func getZeroPageWord(m Memory, address uint8) uint16 {
return uint16(m.Peek(uint16(address))) + 0x100*uint16(m.Peek(uint16(address+1))) return uint16(m.Peek(uint16(address))) + 0x100*uint16(m.Peek(uint16(address+1)))
} }
// FlatMemory puts RAM on the 64Kb addeessable by the processor // FlatMemory puts RAM on the 64Kb addressable by the processor
type FlatMemory struct { type FlatMemory struct {
data [65536]uint8 data [65536]uint8
} }
@ -35,23 +32,10 @@ func (m *FlatMemory) Poke(address uint16, value uint8) {
} }
func (m *FlatMemory) loadBinary(filename string) { func (m *FlatMemory) loadBinary(filename string) {
// Load file bytes, err := ioutil.ReadFile(filename)
f, err := os.Open(filename)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer f.Close()
stats, statsErr := f.Stat()
if statsErr != nil {
panic(err)
}
size := stats.Size()
bytes := make([]byte, size)
buf := bufio.NewReader(f)
buf.Read(bytes)
for i, v := range bytes { for i, v := range bytes {
m.Poke(uint16(i), uint8(v)) m.Poke(uint16(i), uint8(v))