summaryrefslogtreecommitdiff
path: root/tests/clockHMS.mj
blob: ea10d4f1294022ec163d03f388085e8727c3cd66 (plain) (blame)
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
repeat<n>(a) = (x:[n]) where
	if n = 1 then
		x = a
	else
		if n - (2 * (n / 2)) = 1 then
			u = repeat<n/2>(a);
			x = a . u . u
		else
			u = repeat<n/2>(a);
			x = u . u
		end if
	end if
end where

fulladder(a,b,c) = (s, r) where
	s = (a ^ b) ^ c;
	r = (a & b) + ((a ^ b) & c);
end where

adder<n>(a:[n], b:[n], c_in) = (o:[n], c_out) where
	if n = 1 then
		(o, c_out) = fulladder(a[0], b[0], c_in)
	else
		(s_n, c_n1) = fulladder(a[0], b[0], c_in);
		(s_n1, c_out) = adder<n-1>(a[1..], b[1..], c_n1);
		o = s_n . s_n1
	end if
end where

equal<n, m>(a:[n]) = (eq) where
	if n = 0 then
		eq = 1
	else
		if m - (2 * (m / 2)) = 1 then
			eq = a[0] & equal<n-1, m/2>(a[1..]);
		else
			eq = (not a[0]) & equal<n-1, m/2>(a[1..]);
		end if
	end if
end where

reg_n<n>(a:[n]) = (r:[n]) where
	if n = 1 then
		r = reg a[0]
	else
		r = (reg a[0]) . (reg_n<n-1>(a[1..]))
	end if
end where

and_each<n>(a, b:[n]) = (o:[n]) where
	if n = 1 then
		o = b[0] and a
	else
		o = (b[0] and a) . and_each<n-1>(a, b[1..])
	end if
end where

count_mod<n, m>(in:[n], op) = (out:[n], reset) where
	(incr, carry) = adder<n>(in, op . repeat<n-1>(0), 0);
	reset = equal<n, m>(incr);
	neq = not reset;
	out = and_each<n>(neq, incr);
end where

main(tick) = (count_sec:[6],count_min:[6],count_hour:[5],next_day) where
	(count_sec, aug_min) = count_mod<6, 60>(save_sec, tick);
	save_sec = reg_n<6>(count_sec);
	
	(count_min, aug_hour) = count_mod<6, 60>(save_min, aug_min);
	save_min = reg_n<6>(count_min);

	(count_hour, next_day) = count_mod<5, 24>(save_hour, aug_hour);
	save_hour = reg_n<5>(count_hour)
end where