# Examples

> Ready-made Stutr circuits you can open and run: half adder, full adder, 4-bit adder, counter, clock divider, RAM, a 4-bit CPU and an LED screen.

## Opening an example

In the editor choose Examples from the menu and press Open on any circuit below. The code is shown here too, so you can read it or paste it into the Code tab.

## Mini CPU

4-bit accumulator CPU with a program in ROM. Press Run to start its clock.

```stutr
// ===== Mini CPU: 4-bit accumulator machine. Press Run: each clock cycle runs one instruction =====
Clock ck;

// program counter: register + incrementer + jump mux
Register<4> pc(npc, ck);
Constant<4> one = 1;
Adder<4> inc(pc, one);
Mux<4> npc(inc, imm, take);

// program memory: opcodes and operands, address = pc
ROM<4> opm(pc, addr=4, data="1 7 7 7 7 1 3 9 2 6 8 0 0 0 0 0");
ROM<4> imm(pc, addr=4, data="1 0 0 0 0 5 1 6 3 5 0 0 0 0 0 0");
Splitter<4> op(opm);

// ALU: every operation is computed, the opcode bits pick one
Constant<2> sh1 = 1;
Shifter<4> shl(acc, sh1, mode="ll");
Adder<4> sum(acc, imm);
Subtractor<4> dif(acc, imm);
And<4> aa(acc, imm);
Or<4> oo(acc, imm);
Xor<4> xx(acc, imm);
Mux<4> m0(acc, imm, op[0]);
Mux<4> m1(sum, dif, op[0]);
Mux<4> m2(aa, oo, op[0]);
Mux<4> m3(xx, shl, op[0]);
Mux<4> n0(m0, m1, op[1]);
Mux<4> n1(m2, m3, op[1]);
Mux<4> alu(n0, n1, op[2]);
Mux<4> nacc(alu, acc, op[3]);
Register<4> acc(nacc, ck);

// jump unit: opcode 8 = JMP, 9 = JNZ (jump if acc is not 0)
Ground<4> zero;
Comparator<4> zc(acc, zero);
Not nz(zc.eq);
Not no0(op[0]);
And jnz(op[0], nz);
Or sel(no0, jnz);
And take(op[3], sel);

// displays
Output<4> ACC = acc;
HexDisplay hexa(acc);
Output<4> PC = pc;
HexDisplay hexp(pc);
```

## 4-bit adder

Buses: change the bit width of any part in the panel.

```stutr
Input<4> A = 5;
Input<4> B = 3;
Adder<4> s(A, B);
Output<4> Sum = s.sum;
Output Carry = s.carry;
```

## RAM write and read

Set WE to 1, press Run or poke the clock to store D at address A.

```stutr
Input<3> A = 2;
Input<4> D = 9;
Input WE = 1;
Clock ck;
RAM<4> r(A, D, WE, ck, addr=3);
Output<4> Q = r;
```

## Clock divider

The flip-flop toggles on every rising edge.

```stutr
Clock ck;
DFlipFlop f(f.nq, ck);
Output Q = f;
```

## 2-bit counter

Counts 0 to 3 while the clock runs.

```stutr
Clock ck;
Counter<2> c(ck);
Output<2> Q = c;
```

## Half adder

Adds two bits: XOR is the sum, AND is the carry.

```stutr
Input A = 1;
Input B = 1;
Xor s(A, B);
And c(A, B);
Output Sum = s;
Output Carry = c;
```

## Full adder

Adds two bits plus a carry-in.

```stutr
Input A = 1;
Input B = 0;
Input Cin = 1;
Xor x(A, B);
Xor s(x, Cin);
And a1(A, B);
And a2(x, Cin);
Or co(a1, a2);
Output Sum = s;
Output Cout = co;
```

## XOR from NANDs

Four NAND gates behave like one XOR.

```stutr
Input A = 1;
Input B = 0;
Nand n1(A, B);
Nand n2(A, n1);
Nand n3(B, n1);
Nand n4(n2, n3);
Output Y = n4;
```

## Majority vote

Output is 1 when at least two inputs are 1.

```stutr
Input A = 1;
Input B = 1;
Input C = 0;
And ab(A, B);
And bc(B, C);
And ac(A, C);
Or t(ab, bc);
Or m(t, ac);
Output Y = m;
```

## LED screen (8x8)

A counter visits every pixel and x XOR y picks its color. Press Run, or open the screen to paint on it.

```stutr
// an 8x8 screen: X, Y, D, WE, CLR, clock. Raise width and height for a bigger one (up to 2048)
Clock ck;
Counter<6> n(ck);
Splitter<6> sp(n);
Combiner<3> x(sp[0], sp[1], sp[2]);
Combiner<3> y(sp[3], sp[4], sp[5]);
Xor<3> d(x, y);
Constant we = 1;
LEDScreen<3> scr(x, y, d, we, None, ck, width=8, height=8);
```
