-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
150 lines (135 loc) · 3.93 KB
/
cache.js
File metadata and controls
150 lines (135 loc) · 3.93 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var cache_row_size = 64; // bytes
var cache_num_rows = 10; // rows
var element_size = 4; // bytes
// cache stores memory indices converted to logical row numbers
function index_to_row(i) { return Math.floor(i / cache_row_size); }
function element_cache_line(e) { return index_to_row(e * element_size); }
function draw_cache_lines() {
reset();
var sz = get_memory_size();
var e = 0;
var styles = [
"rgb(100, 0, 0)"
,"rgb(0, 100, 0)"
,"rgb(0, 0, 100)"
,"rgb(100, 100, 100)"
,"rgb(000, 000, 000)"
,"rgb(000, 100, 100)"
,"rgb(100, 100, 000)"
];
for (var i = 0; i < sz.x; ++i) {
for (var j = 0; j < sz.y; ++j) {
var r = element_cache_line(e++);
cache.context.fillStyle = styles[ r % styles.length ];
cache_draw_touch(i, j);
}
}
}
function cache_contains(e) {
var row = element_cache_line(e);
var i = cache.rows.length;
while (i--) {
if (cache.rows[i] == row) {
// cache hit!
return true;
}
}
// no cache hit; pull cache row
cache.rows[cache.replace_row] = row;
++cache.replace_row;
if (cache.replace_row == cache_num_rows) {
cache.replace_row = 0;
}
return false;
}
// Types of cache misses
const TYPE_COLD = 0;
const TYPE_CAPACITY = 1;
const TYPE_CONFLICT = 2;
// Touch a given element index (at x, y in array)
// Produces either cache hit or cache miss
function cache_touch(e, x, y) {
if (cache_contains(e)) {
cache.hits.x.push(x);
cache.hits.y.push(y);
cache.was_hit.push(true);
} else {
// log('cache miss, pullin row ' + index_to_row(e*element_size) + '; ' + x + ', ' +y);
cache.misses.x.push(x);
cache.misses.y.push(y);
var miss_type = TYPE_COLD;
if (x == 0 || x == 199) miss_type = TYPE_COLD;
var r = element_cache_line(e);
if (cache.touched[r]) {
miss_type = TYPE_CAPACITY;
}
cache.touched[r] = true;
cache.misses.type.push(miss_type);
cache.was_hit.push(false);
}
}
function cache_draw_touch(x, y) {
cache.context.fillRect(y, x, 1, 1);
}
function cache_draw_miss(m) {
var y = cache.misses.y[m];
var x = cache.misses.x[m];
cache_draw_touch(x, y);
}
function cache_draw_hit(h) {
var y = cache.hits.y[h];
var x = cache.hits.x[h];
cache_draw_touch(x, y);
}
function cache_done() {
window.cache_renderer = {
reset: function() {
this.i = 0; // memory touch index
this.m = 0; // miss index
this.h = 0; // hit index
this.touches = cache.misses.x.length + cache.hits.x.length;
this.missStyle = "rgb(200, 0, 0)";
this.compulsoryMissStyle = "rgb(0, 0, 200)";
this.hitStyle = "rgb(220, 250, 220)"
},
can_step: function() { return this.i < this.touches; },
step: function() {
if (!this.can_step()) { return; }
if (cache.was_hit[this.i++]) {
cache.context.fillStyle = this.hitStyle;
cache_draw_hit(this.h++);
} else {
var miss_type = cache.misses.type[this.m];
if (miss_type == TYPE_CONFLICT) {
cache.context.fillStyle = this.missStyle;
} else if (miss_type == TYPE_CAPACITY) {
cache.context.fillStyle = this.missStyle;
} else {
cache.context.fillStyle = this.compulsoryMissStyle;
}
cache_draw_miss(this.m++);
}
},
draw_oneshot: function() { while (this.can_step()) { this.step(); } },
start_animation: function() {
this.cache_anim_interval = setInterval('cache_renderer.animate_steps()', 5);
},
animate_steps: function() { // wrap this.step() and stop animation when done
for (var i = 0; i < window.animationSpeed; ++i) {
if (this.can_step()) {
this.step();
} else {
this.stop_animation();
}
}
},
stop_animation: function() { clearInterval(this.cache_anim_interval); }
};
cache_renderer.reset();
cache.context.fillStyle = "rgb(200, 0, 0)";
if (window.animate) {
cache_renderer.start_animation();
} else {
cache_renderer.draw_oneshot();
}
}