-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
54 lines (43 loc) · 1.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"flag"
"log"
"time"
"github.com/alabarjasteh/mips-simulator/mips"
)
func main() {
memFile := flag.String("file", "array-max-min.txt", "initiating memory state")
flag.Parse()
log.Printf("Load memory from: %v\n", *memFile)
mem := mips.NewMemory(*memFile)
cpu := mips.NewCPU(mem)
ticker := time.NewTicker(time.Millisecond * 500)
done := make(chan bool)
fetchClockChan := make(chan string) // blocking channels, for synchronization of pipeline stages
decodeClockChan := make(chan string)
executeClockChan := make(chan string)
memoryClockChan := make(chan string)
writebackClockChan := make(chan string)
ifDecChan := make(chan mips.IfDec, 1) // non-blocking channels with buffers size = 1 (async communication). These act as inter-stage's registers.
decExcChan := make(chan mips.DecExc, 1)
exMemChan := make(chan mips.ExMem, 1)
memWBChan := make(chan mips.MemWB, 1)
go func() {
for {
<-ticker.C
log.Println("\n\nTik...")
writebackClockChan <- "tik"
memoryClockChan <- "tik"
executeClockChan <- "tik"
decodeClockChan <- "tik"
fetchClockChan <- "tik"
}
}()
// run stages in parallel
go cpu.Fetch(fetchClockChan, ifDecChan)
go cpu.Decode(decodeClockChan, ifDecChan, decExcChan)
go cpu.Execute(executeClockChan, decExcChan, exMemChan)
go cpu.Memory(memoryClockChan, exMemChan, memWBChan)
go cpu.Writeback(writebackClockChan, memWBChan)
<-done
}