Created
July 14, 2013 07:18
-
-
Save ivoscc/5993487 to your computer and use it in GitHub Desktop.
mini stack thingy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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