Skip to content

Instantly share code, notes, and snippets.

@ivoscc
Created July 14, 2013 07:18
Show Gist options
  • Save ivoscc/5993487 to your computer and use it in GitHub Desktop.
Save ivoscc/5993487 to your computer and use it in GitHub Desktop.
mini stack thingy
package memories
import (
"fmt"
"errors"
)
type SRAM struct {
size int
memory []uint8
}
func (sram *SRAM) write(address int, data uint8) error {
if address >= sram.size || address < 0 {
return errors.New("Addressing out of bounds.")
}
sram.memory[address] = data
return nil
}
func (sram *SRAM) read(address int) (uint8, error) {
if address >= sram.size || address < 0 {
err := errors.New("Addressing out of bounds.")
return 0, err
}
return sram.memory[address], nil
}
func main() {
var sram_size = 1024
sram := &SRAM{sram_size, make([]uint8, sram_size)}
sram.write(0, 0x42)
val, _ := sram.read(0)
fmt.Printf("SRAM: %v\n", val)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment