From 51c7a8cb57216a11192c9fafad28a57801eb2d01 Mon Sep 17 00:00:00 2001 From: Ivan Izaguirre Date: Wed, 15 May 2019 16:01:04 +0200 Subject: [PATCH] Use ioutil.ReadFile() --- apple2/apple2.go | 17 +++-------------- apple2/cardDisk2.go | 21 ++++----------------- apple2/characterGenerator.go | 18 ++++-------------- core6502/memory.go | 22 +++------------------- 4 files changed, 14 insertions(+), 64 deletions(-) diff --git a/apple2/apple2.go b/apple2/apple2.go index c817699..8e5981f 100644 --- a/apple2/apple2.go +++ b/apple2/apple2.go @@ -1,10 +1,9 @@ package apple2 import ( - "bufio" "fmt" "go6502/core6502" - "os" + "io/ioutil" "time" ) @@ -185,24 +184,14 @@ func (a *Apple2) releaseFastMode() { } func (a *Apple2) loadRom(filename string) { - f, err := os.Open(filename) + bytes, err := ioutil.ReadFile(filename) if err != nil { panic(err) } - defer f.Close() - - stats, statsErr := f.Stat() - if statsErr != nil { - panic(err) - } - - size := stats.Size() + size := len(bytes) if size != apple2RomSize && size != apple2eRomSize { panic("Rom size not supported") } - bytes := make([]byte, size) - buf := bufio.NewReader(f) - buf.Read(bytes) romStart := 0 if size == apple2eRomSize { diff --git a/apple2/cardDisk2.go b/apple2/cardDisk2.go index fad5f60..5577585 100644 --- a/apple2/cardDisk2.go +++ b/apple2/cardDisk2.go @@ -1,9 +1,6 @@ package apple2 -import ( - "bufio" - "os" -) +import "io/ioutil" /* https://applesaucefdc.com/woz/reference2/ @@ -141,29 +138,19 @@ func newCardDisk2(filename string) *cardDisk2 { } func loadCardRom(filename string) []memoryPage { - f, err := os.Open(filename) + bytes, err := ioutil.ReadFile(filename) if err != nil { 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 if (size % 256) > 0 { 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]) } diff --git a/apple2/characterGenerator.go b/apple2/characterGenerator.go index dd77320..627ae12 100644 --- a/apple2/characterGenerator.go +++ b/apple2/characterGenerator.go @@ -1,9 +1,8 @@ package apple2 import ( - "bufio" "fmt" - "os" + "io/ioutil" ) /* @@ -28,24 +27,15 @@ func NewCharacterGenerator(filename string) *CharacterGenerator { } func (cg *CharacterGenerator) load(filename string) { - f, err := os.Open(filename) + bytes, err := ioutil.ReadFile(filename) if err != nil { panic(err) } - defer f.Close() - - stats, statsErr := f.Stat() - if statsErr != nil { - panic(err) - } - - size := stats.Size() + size := len(bytes) if size != rev7CharGenSize { panic("Character ROM size not supported") } - cg.data = make([]uint8, size) - buf := bufio.NewReader(f) - buf.Read(cg.data) + cg.data = bytes } func (cg *CharacterGenerator) getPixel(char uint8, row int, column int) bool { diff --git a/core6502/memory.go b/core6502/memory.go index 1387fb2..4248562 100644 --- a/core6502/memory.go +++ b/core6502/memory.go @@ -1,9 +1,6 @@ package core6502 -import ( - "bufio" - "os" -) +import "io/ioutil" // Memory represents the addressable space of the processor 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))) } -// FlatMemory puts RAM on the 64Kb addeessable by the processor +// FlatMemory puts RAM on the 64Kb addressable by the processor type FlatMemory struct { data [65536]uint8 } @@ -35,23 +32,10 @@ func (m *FlatMemory) Poke(address uint16, value uint8) { } func (m *FlatMemory) loadBinary(filename string) { - // Load file - f, err := os.Open(filename) + bytes, err := ioutil.ReadFile(filename) if err != nil { 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 { m.Poke(uint16(i), uint8(v))