-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeStateBasicProtocol.cs
More file actions
129 lines (115 loc) · 4.62 KB
/
ThreeStateBasicProtocol.cs
File metadata and controls
129 lines (115 loc) · 4.62 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace TraceDrivenSimulation
{
/// <summary>
/// 3-Stateプロトコル 制御クラス
/// </summary>
class ThreeStateBasicProtocol : Protocol
{
/// <summary>
/// キャッシュの状態
/// </summary>
[Flags]
public enum State
{
I = 1,
C = 1 << 1,
D = 1 << 2,
};
/// <summary>
/// コンストラクタ
/// </summary>
public ThreeStateBasicProtocol(List<Processor> processors) : base(processors) { }
/// <summary>
/// 読み込み処理 バスの処理
/// </summary>
protected override void Read(string tag, int index, int offset)
{
var readResult = _processors[_targetID].Cache.Read(tag, index, offset);
var message = readResult.BusMessage;
if (message == CPU.BusMessage.None)
{
// do nothing... あとで消すかも
}
else if (message == CPU.BusMessage.ReadMiss)
{
var dirtyCache = _otherProcessors
.Where(p => p.Cache.GetState(tag, index) == (int)State.D)
.Select(p => p.Cache)
.FirstOrDefault();
if (dirtyCache == null) // Dirtyなキャッシュが存在しない
{
// NOTE: 本当はここでメモリから読むこむ処理
// Line Transfer
if (_processors[_targetID].Cache.Transfer("", tag, index))
this.WriteBackCount++;
_processors[_targetID].Cache.SetState(tag, index, (int)State.C);
}
else
{
this.WriteBackCount++; // 無条件にメモリへライトバック
dirtyCache.SetState(tag, index, (int)State.C);
// NOTE: 本当はここでメモリから読むこむ処理
// Line Transfer
if (_processors[_targetID].Cache.Transfer("", tag, index))
this.WriteBackCount++;
_processors[_targetID].Cache.SetState(tag, index, (int)State.C);
}
}
else
throw new NotSupportedException();
}
/// <summary>
/// 書き込み処理
/// </summary>
protected override void Write(string tag, int index, int offset)
{
var message = _processors[_targetID].Cache.Write("", tag, index, offset);
if (message == CPU.BusMessage.None)
{
// do nothing... あとで消すかも
}
else if (message == CPU.BusMessage.Invalidation)
{
_otherProcessors
.Where(p => p.Cache.GetState(tag, index) == (int)State.C)
.ForEach(p => p.Cache.SetState(tag, index, (int)State.I));
}
else if (message == CPU.BusMessage.WriteMiss)
{
// 他キャッシュのClearはInvalidateしておく
_otherProcessors
.Where(p => p.Cache.GetState(tag, index) == (int)State.C)
.ForEach(p => p.Cache.SetState(tag, index, (int)State.I));
var dirtyCache = _otherProcessors
.Where(p => p.Cache.GetState(tag, index) == (int)State.D)
.Select(p => p.Cache)
.FirstOrDefault();
if (dirtyCache == null) // Dirtyなキャッシュが存在しない
{
// Line Transfer
if (_processors[_targetID].Cache.Transfer("", tag, index))
this.WriteBackCount++;
_processors[_targetID].Cache.SetState(tag, index, (int)State.D);
}
else
{
dirtyCache.SetState(tag, index, (int)State.I);
// NOTE: メモリにライトバック
this.WriteBackCount++;
// Line Transfer
if (_processors[_targetID].Cache.Transfer("", tag, index))
this.WriteBackCount++;
_processors[_targetID].Cache.SetState(tag, index, (int)State.C);
// NOTE: Write
_processors[_targetID].Cache.SetState(tag, index, (int)State.D);
}
}
else
throw new NotSupportedException();
}
}
}