microwatt.git
3 years agoAdd LOG_LENGTH to top-generic.vhdl
Anton Blanchard [Tue, 8 Dec 2020 08:18:34 +0000 (19:18 +1100)]
Add LOG_LENGTH to top-generic.vhdl

The other top level files allow LOG_LENGTH to be configured.

Signed-off-by: Anton Blanchard <anton@linux.ibm.com>
3 years agoAdd verilator FPGA target
Anton Blanchard [Mon, 7 Dec 2020 23:50:48 +0000 (10:50 +1100)]
Add verilator FPGA target

Our Makefiles need some work, but for now create an FPGA target:

make FPGA_TARGET=verilator microwatt-verilator

ghdl and yosys can use containers using PODMAN=1 or DOCKER=1
options.

Signed-off-by: Anton Blanchard <anton@linux.ibm.com>
3 years agoMerge pull request #253 from antonblanchard/fix-verilator
Anton Blanchard [Mon, 7 Dec 2020 11:04:46 +0000 (22:04 +1100)]
Merge pull request #253 from antonblanchard/fix-verilator

Fix verilator build

3 years agoFix verilator build
Anton Blanchard [Mon, 7 Dec 2020 10:07:14 +0000 (21:07 +1100)]
Fix verilator build

yosys and verilator did not like us passing in the verilog and
exporting it again. Pass the source directly to verilator instead.

Signed-off-by: Anton Blanchard <anton@linux.ibm.com>
3 years agoMerge pull request #252 from antonblanchard/hello-world-in-8k
Michael Neuling [Mon, 7 Dec 2020 05:20:09 +0000 (16:20 +1100)]
Merge pull request #252 from antonblanchard/hello-world-in-8k

Reduce hello_world footprint to fit in 8kB

3 years agoFix ghdl warning due to variable shadowing in icache
Anton Blanchard [Sun, 6 Dec 2020 20:17:38 +0000 (07:17 +1100)]
Fix ghdl warning due to variable shadowing in icache

Fix a couple of ghdl warnings:

icache.vhdl:387:21:warning: declaration of "i" hides constant "i" [-Whide]
icache.vhdl:400:17:warning: declaration of "i" hides constant "i" [-Whide]

Signed-off-by: Anton Blanchard <anton@linux.ibm.com>
3 years agoReduce hello_world footprint to fit in 8kB
Anton Blanchard [Thu, 3 Dec 2020 09:29:40 +0000 (20:29 +1100)]
Reduce hello_world footprint to fit in 8kB

When building with yosys we assume hello_world fits in 8kB. There's
enough free space that we can adjust the linker script to make it fit.

Signed-off-by: Anton Blanchard <anton@linux.ibm.com>
3 years agoMerge pull request #249 from paulusmack/master
Michael Neuling [Tue, 1 Dec 2020 00:25:08 +0000 (11:25 +1100)]
Merge pull request #249 from paulusmack/master

Sundry bug fixes, plus implement mtmsr

3 years agoMerge pull request #250 from umarcor/containers
Michael Neuling [Tue, 1 Dec 2020 00:10:41 +0000 (11:10 +1100)]
Merge pull request #250 from umarcor/containers

makefile: update synthesis containers

3 years agoMerge pull request #251 from umarcor/ci/containers
Michael Neuling [Tue, 1 Dec 2020 00:06:19 +0000 (11:06 +1100)]
Merge pull request #251 from umarcor/ci/containers

ci: use job.container

3 years agoci: use job.container
umarcor [Mon, 30 Nov 2020 21:17:13 +0000 (22:17 +0100)]
ci: use job.container

Signed-off-by: umarcor <unai.martinezcorral@ehu.eus>
3 years agomakefile: update synthesis containers
umarcor [Thu, 26 Nov 2020 05:14:55 +0000 (06:14 +0100)]
makefile: update synthesis containers

Signed-off-by: umarcor <unai.martinezcorral@ehu.eus>
3 years agomakefile: whitespace cleanup
umarcor [Mon, 30 Nov 2020 21:07:57 +0000 (22:07 +0100)]
makefile: whitespace cleanup

Signed-off-by: umarcor <unai.martinezcorral@ehu.eus>
3 years agotests/misc: Add a test for correct CTR and LR updating by branches
Paul Mackerras [Tue, 24 Nov 2020 01:00:48 +0000 (12:00 +1100)]
tests/misc: Add a test for correct CTR and LR updating by branches

This adds a test with a bdnzl followed immediately by a bdnz, to check
that CTR and LR both get evaluated and written back correctly in this
situation.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoexecute1: Fix forwarding of result when doing delayed LR update
Paul Mackerras [Tue, 24 Nov 2020 00:53:17 +0000 (11:53 +1100)]
execute1: Fix forwarding of result when doing delayed LR update

Random execution testcases showed that a bdnzl which doesn't branch,
followed immediately by a bdnz, uses the wrong value for CTR for the
bdnz.  Decode2 detects the read-after-write hazard on CTR and tells
execute1 to use the bypass path.  However, the bdnzl takes two cycles
because it has to write back both CTR and LR, meaning that by the time
the bdnz starts to execute, r.e.write_data no longer contains the CTR
value, but instead contains zero.

To fix this, we make execute1 maintain the written-back value of CTR
in r.e.write_data across the cycle where LR is written back (this is
possible because the LR writeback uses the exc_write_data path).

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoexecute1: Fix writing LR for bdnzl/bdzl instructions
Paul Mackerras [Sat, 21 Nov 2020 02:54:14 +0000 (13:54 +1100)]
execute1: Fix writing LR for bdnzl/bdzl instructions

Branch instructions which do a redirect and write both CTR and LR were
not doing the write to LR due to a logic error.  This fixes it.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Implement mtmsr instruction
Paul Mackerras [Sat, 21 Nov 2020 01:45:24 +0000 (12:45 +1100)]
core: Implement mtmsr instruction

This is like mtmsrd except it only alters the lower 32 bits of the MSR.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agotests/trace: Test trace interrupt vs. FP unavailable interrupt
Paul Mackerras [Fri, 25 Sep 2020 08:14:18 +0000 (18:14 +1000)]
tests/trace: Test trace interrupt vs. FP unavailable interrupt

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoexecute1: Fix bug in trace interrupt vs. ITLB miss
Paul Mackerras [Sat, 3 Oct 2020 10:08:11 +0000 (20:08 +1000)]
execute1: Fix bug in trace interrupt vs. ITLB miss

If an instruction fetch results in an instruction TLB miss, an
OP_FETCH_FAILED instruction is sent down the pipe.  If the MSR[TE]
field is set for instruction tracing, the core currently considers
that executing the OP_FETCH_FAILED counts as having executed one
instruction and so generates a trace interrupt on the next valid
instruction, meaning that the trace interrupt happens before the
desired instruction rather than after it.

Fix this by not tracing OP_FETCH_FAILED instructions.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoMerge pull request #245 from paulusmack/fpu
Michael Neuling [Thu, 17 Sep 2020 02:04:26 +0000 (12:04 +1000)]
Merge pull request #245 from paulusmack/fpu

Add a simple FPU

3 years agoMerge pull request #244 from paulusmack/master
Michael Neuling [Thu, 17 Sep 2020 01:43:54 +0000 (11:43 +1000)]
Merge pull request #244 from paulusmack/master

Implement trace interrupts plus decode improvements

3 years agoFPU: Do masking after adder rather than on A input
Paul Mackerras [Sat, 12 Sep 2020 10:13:24 +0000 (20:13 +1000)]
FPU: Do masking after adder rather than on A input

The masking enabled by opsel_amask is only used when rounding, to trim
the rounded result to the required precision.  We now do the masking
after the adder rather than before (on the A input).  This gives the
same result and helps timing.  The path from r.shift through the mask
generator and adder to v.r was showing up as a critical path.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Decide on mask length a cycle earlier
Paul Mackerras [Tue, 1 Sep 2020 05:28:19 +0000 (15:28 +1000)]
FPU: Decide on mask length a cycle earlier

This moves longmask into the reg_type record, meaning that it now
needs to be decided a cycle earlier, in order to help timing.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Decide on A input selection a cycle earlier
Paul Mackerras [Tue, 1 Sep 2020 05:09:17 +0000 (15:09 +1000)]
FPU: Decide on A input selection a cycle earlier

This moves opsel_a into the reg_type record, meaning that the A
multiplexer input now needs to be decided a cycle earlier.  This helps
timing by eliminating the combinatorial path from r.state and other
things to opsel_a and thence to in_a and result.

This means that some things now take an extra cycle, in particular
some of the exception cases such as when one or both operands are
NaNs.  The NaN handling has been moved out to its own state, which
simplifies the logic for exception cases in other places.  Additions
or subtractions where FRB's exponent is smaller than FRA's will
also take an extra cycle.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Add comments specifying the expectation of r.shift for each state
Paul Mackerras [Tue, 1 Sep 2020 01:13:17 +0000 (11:13 +1000)]
FPU: Add comments specifying the expectation of r.shift for each state

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement floating multiply-add instructions
Paul Mackerras [Sat, 1 Aug 2020 09:17:36 +0000 (19:17 +1000)]
FPU: Implement floating multiply-add instructions

This implements fmadd, fmsub, fnmadd, fnmsub and their
single-precision counterparts.  The single-precision versions operate
the same as the double-precision versions until the final rounding and
overflow/underflow steps.

This adds an S register to store the low bits of the product.  S
shifts into R on left shifts, and can be negated, but doesn't do any
other arithmetic.

This adds a test for the double-precision versions of these
instructions.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement ftdiv and ftsqrt
Paul Mackerras [Fri, 31 Jul 2020 06:46:12 +0000 (16:46 +1000)]
FPU: Implement ftdiv and ftsqrt

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement fsqrt[s] and add a test for fsqrt
Paul Mackerras [Fri, 31 Jul 2020 02:02:55 +0000 (12:02 +1000)]
FPU: Implement fsqrt[s] and add a test for fsqrt

This implements the floating square-root calculation using a table
lookup of the inverse square root approximation, followed by three
iterations of Goldschmidt's algorithm, which gives estimates of both
sqrt(FRB) and 1/sqrt(FRB).  Then the residual is calculated as
FRB - R * R and that is multiplied by the 1/sqrt(FRB) estimate to get
an adjustment to R.  The residual and the adjustment can be negative,
and since we have an unsigned multiplier, the upper bits can be wrong.
In practice the adjustment fits into an 8-bit signed value, and the
bottom 8 bits of the adjustment product are correct, so we sign-extend
them, divide by 4 (because R is in 10.54 format) and add them to R.

Finally the residual is calculated again and compared to 2*R+1 to see
if a final increment is needed.  Then the result is rounded and
written back.

This implements fsqrts as fsqrt, but with rounding to single precision
and underflow/overflow calculation using the single-precision exponent
range.  This could be optimized later.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement frsqrte[s] and a test for frsqrte
Paul Mackerras [Thu, 30 Jul 2020 06:11:58 +0000 (16:11 +1000)]
FPU: Implement frsqrte[s] and a test for frsqrte

This implements frsqrte by table lookup.  We first normalize the input
if necessary and adjust so that the exponent is even, giving us a
mantissa value in the range [1.0, 4.0), which is then used to look up
an entry in a 768-entry table.  The 768 entries are appended to the
table for reciprocal estimates, giving a table of 1024 entries in
total.  frsqrtes is implemented identically to frsqrte.

The estimate supplied is accurate to 1 part in 1024 or better.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agotests/fpu: Add tests for fsel and fcmpu
Paul Mackerras [Thu, 30 Jul 2020 03:38:09 +0000 (13:38 +1000)]
tests/fpu: Add tests for fsel and fcmpu

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement fcmpu and fcmpo
Paul Mackerras [Thu, 30 Jul 2020 00:00:25 +0000 (10:00 +1000)]
FPU: Implement fcmpu and fcmpo

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement fsel
Paul Mackerras [Wed, 29 Jul 2020 10:26:39 +0000 (20:26 +1000)]
FPU: Implement fsel

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement fre[s]
Paul Mackerras [Wed, 29 Jul 2020 07:34:03 +0000 (17:34 +1000)]
FPU: Implement fre[s]

This just returns the value from the inverse lookup table.  The result
is accurate to better than one part in 512 (the architecture requires
1/256).

This also adds a simple test, which relies on the particular values in
the inverse lookup table, so it is not a general test.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement fdiv[s]
Paul Mackerras [Tue, 28 Jul 2020 06:07:25 +0000 (16:07 +1000)]
FPU: Implement fdiv[s]

This implements floating-point division A/B by a process that starts
with normalizing both inputs if necessary.  Then an estimate of 1/B
from a lookup table is refined by 3 Newton-Raphson iterations and then
multiplied by A to get a quotient.  The remainder is calculated as
A - R * B (where R is the result, i.e. the quotient) and the remainder
is compared to 0 and to B to see whether the quotient needs to be
incremented by 1.  The calculations of 1 / B are done with 56 fraction
bits and intermediate results are truncated rather than rounded,
meaning that the final estimate of 1 / B is always correct or a little
bit low, never too high, and thus the calculated quotient is correct
or 1 unit too low.  Doing the estimate of 1 / B with sufficient
precision that the quotient is always correct to the last bit without
needing any adjustment would require many more bits of precision.

This implements fdivs by computing a double-precision quotient and
then rounding it to single precision.  It would be possible to
optimize this by e.g. doing only 2 iterations of Newton-Raphson and
then doing the remainder calculation and adjustment at single
precision rather than double precision.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement fmul[s]
Paul Mackerras [Mon, 27 Jul 2020 08:27:50 +0000 (18:27 +1000)]
FPU: Implement fmul[s]

This implements the fmul and fmuls instructions.

For fmul[s] with denormalized operands we normalize the inputs
before doing the multiplication, to eliminate the need for doing
count-leading-zeroes on P.  This adds 3 or 5 cycles to the
execution time when one or both operands are denormalized.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement fadd[s] and fsub[s] and add tests for them
Paul Mackerras [Thu, 23 Jul 2020 07:56:15 +0000 (17:56 +1000)]
FPU: Implement fadd[s] and fsub[s] and add tests for them

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement fmrgew and fmrgow and add tests for them
Paul Mackerras [Wed, 22 Jul 2020 10:51:31 +0000 (20:51 +1000)]
FPU: Implement fmrgew and fmrgow and add tests for them

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement floating round-to-integer instructions
Paul Mackerras [Wed, 22 Jul 2020 06:13:12 +0000 (16:13 +1000)]
FPU: Implement floating round-to-integer instructions

This implements frin, friz, frip and frim, and adds tests for them.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement floating convert to integer instructions
Paul Mackerras [Wed, 22 Jul 2020 02:19:12 +0000 (12:19 +1000)]
FPU: Implement floating convert to integer instructions

This implements fctiw, fctiwz, fctiwu, fctiwuz, fctid, fctidz, fctidu
and fctiduz, and adds tests for them.

There are some subtleties around the setting of the inexact (XX) and
invalid conversion (VXCVI) flags in the FPSCR.  If the rounded value
ends up being out of range, we need to set VXCVI and not XX.  For a
conversion to unsigned word or doubleword of a negative value that
rounds to zero, we need to set XX and not VXCVI.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agotests/fpu: Add tests for frsp
Paul Mackerras [Sun, 19 Jul 2020 01:53:01 +0000 (11:53 +1000)]
tests/fpu: Add tests for frsp

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement the frsp instruction
Paul Mackerras [Mon, 31 Aug 2020 07:26:33 +0000 (17:26 +1000)]
FPU: Implement the frsp instruction

This brings in the invalid exception for the case of frsp with a
signalling NaN as input, and the need to be able to convert a
signalling NaN to a quiet NaN.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement floating convert from integer instructions
Paul Mackerras [Thu, 16 Jul 2020 05:51:57 +0000 (15:51 +1000)]
FPU: Implement floating convert from integer instructions

This implements fcfid, fcfidu, fcfids and fcfidus, which convert
64-bit integer values in an FPR into a floating-point value.
This brings in a lot of the datapath that will be needed in
future, including the shifter, adder, mask generator and
count-leading-zeroes logic, along with the machinery for rounding
to single-precision or double-precision, detecting inexact results,
signalling inexact-result exceptions, and updating result flags
in the FPSCR.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement fmr and related instructions
Paul Mackerras [Wed, 15 Jul 2020 04:28:06 +0000 (14:28 +1000)]
FPU: Implement fmr and related instructions

This implements fmr, fneg, fabs, fnabs and fcpsgn and adds tests
for them.

This adds logic to unpack and repack floating-point data from the
64-bit packed form (as stored in memory and the register file) into
the unpacked form in the fpr_reg_type record.  This is not strictly
necessary for fmr et al., but will be useful for when we do actual
arithmetic.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agotests/fpu: Test remaining FPSCR-related instructions
Paul Mackerras [Mon, 31 Aug 2020 03:10:51 +0000 (13:10 +1000)]
tests/fpu: Test remaining FPSCR-related instructions

This adds tests for mffsce, mffscrn, mffscrni, mffsl, mcrfs, mtfsfi,
mtfsb0 and mtfsb1.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoFPU: Implement remaining FPSCR-related instructions
Paul Mackerras [Sat, 29 Aug 2020 10:34:55 +0000 (20:34 +1000)]
FPU: Implement remaining FPSCR-related instructions

This implements mcrfs, mtfsfi, mtfsb0/1, mffscr, mffscrn, mffscrni and
mffsl.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agotests/fpu: Add tests for basic FPSCR function and interrupt generation
Paul Mackerras [Wed, 15 Jul 2020 02:46:18 +0000 (12:46 +1000)]
tests/fpu: Add tests for basic FPSCR function and interrupt generation

This tests mffs, mtfsf and the generation of floating-point type
program interrupts that occur as a result of mtfsf.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Add framework for an FPU
Paul Mackerras [Fri, 28 Aug 2020 10:01:00 +0000 (20:01 +1000)]
core: Add framework for an FPU

This adds the skeleton of a floating-point unit and implements the
mffs and mtfsf instructions.

Execute1 sends FP instructions to the FPU and receives busy,
exception, FP interrupt and illegal interrupt signals from it.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agotests/fpu: Add tests for lfs and stfs instructions
Paul Mackerras [Thu, 2 Jul 2020 09:55:30 +0000 (19:55 +1000)]
tests/fpu: Add tests for lfs and stfs instructions

This exercises the single-to-double and double-to-single conversions,
including denormalized cases.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Add support for single-precision FP loads and stores
Paul Mackerras [Fri, 28 Aug 2020 03:35:05 +0000 (13:35 +1000)]
core: Add support for single-precision FP loads and stores

This adds code to loadstore1 to convert between single-precision and
double-precision formats, and implements the lfs* and stfs*
instructions.  The conversion processes are described in Power ISA
v3.1 Book 1 sections 4.6.2 and 4.6.3.

These conversions take one cycle, so lfs* and stfs* are one cycle
slower than lfd* and stfd*.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agotests: Add a test for FP loads and stores
Paul Mackerras [Wed, 1 Jul 2020 08:03:19 +0000 (18:03 +1000)]
tests: Add a test for FP loads and stores

This tests that floating-point unavailable exceptions occur as expected
on FP loads and stores, and that the simple FP loads and stores appear
to give reasonable results.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Add support for floating-point loads and stores
Paul Mackerras [Fri, 28 Aug 2020 02:49:48 +0000 (12:49 +1000)]
core: Add support for floating-point loads and stores

This extends the register file so it can hold FPR values, and
implements the FP loads and stores that do not require conversion
between single and double precision.

We now have the FP, FE0 and FE1 bits in MSR.  FP loads and stores
cause a FP unavailable interrupt if MSR[FP] = 0.

The FPU facilities are optional and their presence is controlled by
the HAS_FPU generic passed down from the top-level board file.  It
defaults to true for all except the A7-35 boards.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agotests: Add a test for trace interrupts
Paul Mackerras [Sat, 29 Aug 2020 09:30:56 +0000 (19:30 +1000)]
tests: Add a test for trace interrupts

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoexecute1: Implement trace interrupts
Paul Mackerras [Fri, 28 Aug 2020 10:34:09 +0000 (20:34 +1000)]
execute1: Implement trace interrupts

Trace interrupts occur when the MSR[TE] field is non-zero and an
instruction other than rfid has been successfully completed.  A trace
interrupt occurs before the next instruction is executed or any
asynchronous interrupt is taken.

Since the trace interrupt is defined to set SRR1 bits depending on
whether the traced instruction is a load or an instruction treated as
a load, or a store or an instruction treated as a store, we need to
make sure the treated-as-a-load instructions (icbi, icbt, dcbt, dcbst,
dcbf) and the treated-as-a-store instructions (dcbtst, dcbz) have the
correct opcodes in decode1.  Several of them were previously marked as
OP_NOP.

We don't yet implement the SIAR or SDAR registers, which should be set
by trace interrupts.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agodecode1: Avoid overriding fields of v.decode in decode1
Paul Mackerras [Wed, 26 Aug 2020 09:19:34 +0000 (19:19 +1000)]
decode1: Avoid overriding fields of v.decode in decode1

In the cases where we need to override the values from the decode ROMs,
we now do that overriding after the clock edge (eating into decode2's
cycle) rather than before.  This helps timing a little.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoMerge pull request #239 from paulusmack/master
Michael Neuling [Thu, 27 Aug 2020 11:28:21 +0000 (21:28 +1000)]
Merge pull request #239 from paulusmack/master

Implement BE and 32b modes

3 years agotests: Add a test for the load-reserve and store-conditional instructions
Paul Mackerras [Sat, 22 Aug 2020 09:53:59 +0000 (19:53 +1000)]
tests: Add a test for the load-reserve and store-conditional instructions

This checks that the instructions seem to update memory as expected,
and also that they generate alignment interrupts when necessary.
We don't check whether the memory update is atomic as we don't have
SMP yet.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoloadstore1: Generate alignment interrupts for unaligned larx/stcx
Paul Mackerras [Fri, 21 Aug 2020 02:16:27 +0000 (12:16 +1000)]
loadstore1: Generate alignment interrupts for unaligned larx/stcx

Load-and-reserve and store-conditional instructions are required to
generate an alignment interrupt (0x600 vector) if their EA is not
aligned.  Implement this.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agotests: Add tests for 32-bit and big-endian modes
Paul Mackerras [Thu, 20 Aug 2020 08:03:14 +0000 (18:03 +1000)]
tests: Add tests for 32-bit and big-endian modes

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Implement 32-bit mode
Paul Mackerras [Sun, 16 Aug 2020 23:38:13 +0000 (09:38 +1000)]
core: Implement 32-bit mode

In 32-bit mode, effective addresses are truncated to 32 bits, both for
instruction fetches and data accesses, and CR0 is set for Rc=1 (record
form) instructions based on the lower 32 bits of the result rather
than all 64 bits.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Implement big-endian mode
Paul Mackerras [Wed, 12 Aug 2020 11:59:28 +0000 (21:59 +1000)]
core: Implement big-endian mode

Big-endian mode affects both instruction fetches and data accesses.
For instruction fetches, we byte-swap each word read from memory when
writing it into the icache data RAM, and use a tag bit to indicate
whether each cache line contains instructions in BE or LE form.

For data accesses, we simply need to invert the existing byte_reverse
signal in BE mode.  The only thing to be careful of is to get the sign
bit from the correct place when doing a sign-extending load that
crosses two doublewords of memory.

For now, interrupts unconditionally set MSR[LE].  We will need some
sort of interrupt-little-endian bit somewhere, perhaps in LPCR.

This also fixes a debug report statement in fetch1.vhdl.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agotests/mmu: Update to use correct MSR values
Paul Mackerras [Thu, 20 Aug 2020 08:14:16 +0000 (18:14 +1000)]
tests/mmu: Update to use correct MSR values

The tests were using MSR values that did not have MSR_SF or MSR_LE
set.  Fix this so that the test still works when 32-bit and BE modes
are implemented.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoMerge pull request #235 from paulusmack/master
Michael Neuling [Thu, 13 Aug 2020 11:50:26 +0000 (21:50 +1000)]
Merge pull request #235 from paulusmack/master

More instructions and a random number generator

3 years agoMerge pull request #236 from ozbenh/targets
Michael Neuling [Thu, 13 Aug 2020 11:46:36 +0000 (21:46 +1000)]
Merge pull request #236 from ozbenh/targets

Add support for Genesys2 and Acord CLE-215

3 years agofpga: Add support for Genesys2
Boris Shingarov [Sun, 12 Jul 2020 18:07:21 +0000 (14:07 -0400)]
fpga: Add support for Genesys2

Signed-off-by: Boris Shingarov <shingarov@labware.com>
3 years agoacorn: Add support for the Acorn CLE 215+
Benjamin Herrenschmidt [Tue, 7 Jul 2020 23:37:45 +0000 (09:37 +1000)]
acorn: Add support for the Acorn CLE 215+

This is a NiteFury based PCIe M2 form-factor board originally
used for mining. It contains a speed grade 2 Artix 7 200T,
1GB of DDR3 and 32MB of flash.

The serial port is routed to pin 2 (RX) and 3 (TX) of the P2
connector (pin 1 is GND).

Note: Only 16MB of flash is currently usable until code is added
to configure the flash controller to use 4-bytes address commands
on that part.

Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
3 years agoMerge pull request #229 from ozbenh/litedram
Michael Neuling [Fri, 7 Aug 2020 04:42:31 +0000 (14:42 +1000)]
Merge pull request #229 from ozbenh/litedram

Litedram: Misc improvements and support for different DRAM geometries

3 years agocore: Implement BCD Assist instructions addg6s, cdtbcd, cbcdtod
Paul Mackerras [Thu, 6 Aug 2020 23:57:19 +0000 (09:57 +1000)]
core: Implement BCD Assist instructions addg6s, cdtbcd, cbcdtod

To avoid adding too much logic, this moves the adder used by OP_ADD
out of the case statement in execute1.vhdl so that the result can
be used by OP_ADDG6S as well.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Implement the wait instruction as a no-op
Paul Mackerras [Thu, 6 Aug 2020 10:31:09 +0000 (20:31 +1000)]
core: Implement the wait instruction as a no-op

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Implement the reserved no-op instructions
Paul Mackerras [Thu, 6 Aug 2020 09:24:40 +0000 (19:24 +1000)]
core: Implement the reserved no-op instructions

These are no-ops that are reserved for future use as performance
hints, so we just need to treat them as no-ops.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Implement the addex instruction
Paul Mackerras [Thu, 6 Aug 2020 09:15:02 +0000 (19:15 +1000)]
core: Implement the addex instruction

The addex instruction is like adde but uses the XER[OV] bit for the
carry in and out rather than XER[CA].

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoAdd random number generator and implement the darn instruction
Paul Mackerras [Wed, 5 Aug 2020 05:28:45 +0000 (15:28 +1000)]
Add random number generator and implement the darn instruction

This adds a true random number generator for the Xilinx FPGAs which
uses a set of chaotic ring oscillators to generate random bits and
then passes them through a Linear Hybrid Cellular Automaton (LHCA) to
remove bias, as described in "High Speed True Random Number Generators
in Xilinx FPGAs" by Catalin Baetoniu of Xilinx Inc., in:

https://pdfs.semanticscholar.org/83ac/9e9c1bb3dad5180654984604c8d5d8137412.pdf

This requires adding a .xdc file to tell vivado that the combinatorial
loops that form the ring oscillators are intentional.  The same
code should work on other FPGAs as well if their tools can be told to
accept the combinatorial loops.

For simulation, the random.vhdl module gets compiled in, which uses
the pseudorand() function to generate random numbers.

Synthesis using yosys uses nonrandom.vhdl, which always signals an
error, causing darn to return 0xffff_ffff_ffff_ffff.

This adds an implementation of the darn instruction.  Darn can return
either raw or conditioned random numbers.  On Xilinx FPGAs, reading a
raw random number gives the output of the ring oscillators, and
reading a conditioned random number gives the output of the LHCA.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Implement the maddhd, maddhdu and maddld instructions
Paul Mackerras [Tue, 4 Aug 2020 10:02:30 +0000 (20:02 +1000)]
core: Implement the maddhd, maddhdu and maddld instructions

These instructions use major opcode 4 and have a third GPR input
operand, so we need a decode table for major opcode 4 and some
plumbing to get the RC register operand read.

The multiply-add instructions use the same insn_type_t values as the
regular multiply instructions, and we distinguish in execute1 by
looking at the major opcode.  This turns out to be convenient because
we don't have to add any cases in the code that handles the output of
the multiplier, and it frees up some insn_type_t values.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Implement the cmpeqb and cmprb instructions
Paul Mackerras [Mon, 3 Aug 2020 12:30:23 +0000 (22:30 +1000)]
core: Implement the cmpeqb and cmprb instructions

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Implement the bpermd instruction
Paul Mackerras [Mon, 3 Aug 2020 04:45:19 +0000 (14:45 +1000)]
core: Implement the bpermd instruction

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Implement the setb instruction
Paul Mackerras [Mon, 3 Aug 2020 04:31:58 +0000 (14:31 +1000)]
core: Implement the setb instruction

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Implement the mcrxrx instruction
Paul Mackerras [Mon, 3 Aug 2020 00:29:46 +0000 (10:29 +1000)]
core: Implement the mcrxrx instruction

This also removes OP_MCRXR, as the mcrxr instruction was removed in
version 3.0B of the Power ISA, having been phased-out for the server
architecture since v2.02.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Implement the TAR register and the bctar instruction
Paul Mackerras [Mon, 3 Aug 2020 00:08:33 +0000 (10:08 +1000)]
core: Implement the TAR register and the bctar instruction

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoexecute1: Use r.<field> not v.<field> in countzero code
Paul Mackerras [Tue, 28 Jul 2020 02:09:02 +0000 (12:09 +1000)]
execute1: Use r.<field> not v.<field> in countzero code

This simplifies logic and improves timing.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoexecute1: Take an extra cycle for OE=1 multiply instructions
Paul Mackerras [Mon, 27 Jul 2020 08:54:27 +0000 (18:54 +1000)]
execute1: Take an extra cycle for OE=1 multiply instructions

We now expect the overflow signal from the multiplier to come along
one cycle later than the product.

This breaks up a long combinatorial path and improves timing.

This also changes some uses of v.<field> to r.<field> in the slow
op logic, which should help timing as well.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agomultiplier: Generalize interface to the multiplier
Paul Mackerras [Sat, 25 Jul 2020 08:23:11 +0000 (18:23 +1000)]
multiplier: Generalize interface to the multiplier

This makes the interface to the multiplier more general so an instance
of it can be used in the FPU.  It now has a 128-bit addend that is
added on to the product.  Instead of an input to negate the output,
it now has a "not_result" input to complement the output.  Execute1
uses not_result=1 and addend=-1 to get the effect of negating the
output.  The interface is defined this way because this is what can
be done easily with the Xilinx DSP slices in xilinx-mult.vhdl.

This also adds clock enable signals to the DSP slices, mostly for the
sake of reducing power consumption.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoMerge pull request #233 from paulusmack/master
Michael Neuling [Wed, 22 Jul 2020 09:51:24 +0000 (19:51 +1000)]
Merge pull request #233 from paulusmack/master

Changes to improve timing

3 years agoloadstore1: Better expression for store data formatting
Paul Mackerras [Sat, 18 Jul 2020 06:37:03 +0000 (16:37 +1000)]
loadstore1: Better expression for store data formatting

This rearranges the code used for store data formatting so that the
"for i in 0 to 7" loop indexes the output bytes rather than the
input bytes.  The new expression is formally identical to the old
but is easier to synthesize.  This reduces the number of LUTs by
about 250 on the Artix-7 and improves timing.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoloadstore1: Further tweaks to improve synthesis with yosys/nextpnr
Paul Mackerras [Wed, 15 Jul 2020 23:26:47 +0000 (09:26 +1000)]
loadstore1: Further tweaks to improve synthesis with yosys/nextpnr

This reworks the way that the busy and done signals are generated in
loadstore in order to work around some problems where yosys/nextpnr
are reporting combinatorial loops (not in fact on the current code but
on minor variations needed for supporting the FPU).  It seems that
yosys has problems with the case statement on v.state.

This also lifts the maddr and byte_sel generation out of the case
statement.  The overall result is a slight reduction in resource usage
(~30 6-input LUTs on the A7-100).

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agodcache: Ease timing on wishbone data and byte selects
Paul Mackerras [Fri, 10 Jul 2020 09:04:37 +0000 (19:04 +1000)]
dcache: Ease timing on wishbone data and byte selects

This eliminates a path where the inputs to r1.wb.dat and r1.wb.sel
depend on req_op, which depends on the TLB and cache hit detection.
In fact they only need to depend on the nature of the request in
r0.req (i.e. DCBZ, store, cacheable load, or non-cacheable load).
This sets them at the beginning of the code for IDLE state rather
than inside the req_op case statement.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agodecode1: Fix formatting
Paul Mackerras [Tue, 14 Jul 2020 23:00:44 +0000 (09:00 +1000)]
decode1: Fix formatting

Commit d5c8c33baecc ("decode1: Reformat to 4-space indentation") resulted
in some rows of major_decode_rom_array being misaligned.  This fixes it.
No code change.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoloadstore1: Separate address calculation for MMU to ease timing
Paul Mackerras [Mon, 13 Jul 2020 07:43:52 +0000 (17:43 +1000)]
loadstore1: Separate address calculation for MMU to ease timing

This computes the address sent to the MMU separately from that sent
to the dcache.  This means that the address sent to the MMU doesn't
have the delay through the lsu_sum adder, making it available earlier.
The path through the lsu_sum adder and through the MMU to the MMU
done and err outputs showed up as a critical path on some builds.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoloadstore1: Generate busy signal earlier
Paul Mackerras [Mon, 13 Jul 2020 02:18:53 +0000 (12:18 +1000)]
loadstore1: Generate busy signal earlier

This makes the calculation of busy as simple as possible and dependent
only on register outputs.  The timing of busy is critical, as it gates
the valid signal for the next instruction, and therefore any delays
in dropping busy at the end of a load or store directly impact the
timing of a host of other paths.

This also separates the 'done without error' and 'done with error'
cases from the MMU into separate signals that are both driven directly
from registers.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agodcache: Output separate done-without-error and error-done signals
Paul Mackerras [Sun, 12 Jul 2020 10:05:53 +0000 (20:05 +1000)]
dcache: Output separate done-without-error and error-done signals

This reduces the complexity of the logic in the places where these
signals are used.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agodcache: Ease timing on calculation of acks remaining
Paul Mackerras [Sat, 11 Jul 2020 12:23:31 +0000 (22:23 +1000)]
dcache: Ease timing on calculation of acks remaining

This moves the incrementing or decrementing of r1.acks_pending
to the cycle after a strobe is output or an ack is seen on the
wishbone, and simplifies the logic that determines whether the
cycle is now complete.  This means that the path from seeing
req_op equal to OP_STORE_HIT or OP_STORE_MISS to setting r1.state
and r1.cyc now just involves the stbs_done bit rather than a more
complex calculation involving the possibly incremented r1.acks_pending.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agodcache: Improve timing of valid/done outputs
Paul Mackerras [Sat, 11 Jul 2020 07:46:03 +0000 (17:46 +1000)]
dcache: Improve timing of valid/done outputs

This makes d_out.valid and m_out.done come directly from registers in
order to improve timing.  The inputs to the registers are set by the
same conditions that cause r1.hit_load_valid, r1.slow_valid,
r1.error_done and r1.stcx_fail to be set.

Note that the STORE_WAIT_ACK state doesn't test r1.mmu_req but assumes
that the request came from loadstore1.  This is because we normally
have r1.full = 0 in this state, which means that r1.mmu_req can
change at any time.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocore: Don't generate logic for log data when LOG_LENGTH = 0
Paul Mackerras [Sat, 11 Jul 2020 05:40:27 +0000 (15:40 +1000)]
core: Don't generate logic for log data when LOG_LENGTH = 0

This adds "if LOG_LENGTH > 0 generate" to the places in the core
where log output data is latched, so that when LOG_LENGTH = 0 we
don't create the logic to collect the data which won't be stored.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agocountzero: Faster algorithm for count leading/trailing zeroes
Paul Mackerras [Sat, 11 Jul 2020 02:05:43 +0000 (12:05 +1000)]
countzero: Faster algorithm for count leading/trailing zeroes

This uses an algorithm for count leading/trailing zeroes that is
faster on FPGAs, which makes timing easier.  cntlz* and cnttz*
still take two cycles, though.

For count trailing zeroes, we compute x & -x, which for non-zero x
has a single 1 bit in the position of the least-significant 1 bit
in x.  This one-hot representation can then be converted to a bit
number with six 32-input OR gates.  For count leading zeroes, we
simply do a bit-reversal on x and then use the same algorithm.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoMMU: Improve timing of done signal back to loadstore1
Paul Mackerras [Sat, 11 Jul 2020 01:00:53 +0000 (11:00 +1000)]
MMU: Improve timing of done signal back to loadstore1

This makes the l_out.done signal come from a clean latch, which
improves timing.  The cost is that TLB load and invalidation
operations to the dcache now signal done back to loadstore1 one
cycle later than before, but that doesn't seem to affect overall
performance noticeably.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agodcache: Remove dependency of r1.wb.adr/dat/sel on req_op
Paul Mackerras [Fri, 10 Jul 2020 23:10:24 +0000 (09:10 +1000)]
dcache:  Remove dependency of r1.wb.adr/dat/sel on req_op

This improves timing by setting r1.wb.{adr,dat,sel} to the next
request when doing a write cycle on the wishbone before we know
whether the next request has a TLB and cache hit or not, i.e.
without depending on req_op.  r1.wb.stb still depends on req_op.

This contains a workaround for what is probably a bug elsewhere,
in that changing r1.wb.sel unconditionally once we see stall=0
from the wishbone causes incorrect behaviour.  Making it
conditional on there being a valid following request appears
to fix the problem.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agodcache: Update TLB PLRU one cycle later
Paul Mackerras [Fri, 10 Jul 2020 10:32:35 +0000 (20:32 +1000)]
dcache: Update TLB PLRU one cycle later

This puts the inputs to the TLB PLRU through a register stage, so
the TLB PLRU update is done in the cycle after the TLB tag
matching rather than the same cycle.  This improves timing.
The PLRU output is only used when writing the TLB in response to
a tlbwe request from the MMU, and that doesn't happen within one
cycle of a virtual-mode load or store, so the fact that the
tlb victim way information is delayed by one cycle doesn't
create any problems.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoloadstore1: Eliminate two_dwords variable
Paul Mackerras [Fri, 10 Jul 2020 10:11:15 +0000 (20:11 +1000)]
loadstore1: Eliminate two_dwords variable

The computation of two_dwords from r.second_bytes has shown up as
part of a critical path at times.  Instead we add a 'last_dword'
flag to the reg_stage_t record which tells us more directly
whether a valid flag coming in from dcache means that the
instruction is done, thereby shortening the path to the busy output
back to execute1.

This also simplifies some of the trim_ctl logic.  The two_dwords = 0
case could never have use_second(i) = 1 for any of the bytes being
transferred, so "not use_second(i)" is always 1.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoexecute1: Ease timing on redirect_nia
Paul Mackerras [Fri, 10 Jul 2020 09:07:47 +0000 (19:07 +1000)]
execute1: Ease timing on redirect_nia

This eliminates a dependency of r.f.redirect_nia on the carry out
from the main adder in the case of a conditional trap instruction.
We can set r.f.redirect_nia unconditionally, even if no interrupt
is generated.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agodcache: Do PLRU update one cycle later
Paul Mackerras [Fri, 10 Jul 2020 07:47:52 +0000 (17:47 +1000)]
dcache: Do PLRU update one cycle later

This does the PLRU update based on r1.cache_hit and r1.hit_way rather
than req_op and req_hit_way, which means there is now a register
between the TLB and cache tag lookup and the PLRU update, which should
help with timing.

The PLRU victim selection now becomes valid one cycle later, in the
cycle where r1.write_tag = 1.  We now have replace_way coming from
the PLRU when r1.write_tag = 1 and from r1.store_way at other times,
and we use that instead of r1.store_way in situations where we need
it to be valid in the first cycle of the RELOAD_WAIT_ACK state.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoicache: Do PLRU update one cycle later
Paul Mackerras [Fri, 10 Jul 2020 00:04:56 +0000 (10:04 +1000)]
icache: Do PLRU update one cycle later

This does the PLRU update based on r.hit_valid and r.hit_way rather
than req_is_hit and req_hit_way, which means there is now a register
between the TLB and cache tag lookup and the PLRU update, which
should help with timing.

As a result, the PLRU victim way selection becomes valid one cycle
later, in the cycle when r.state = CLR_TAG.  So we have to use the
PLRU output directly in the CLR_TAG state and r.store_way in the
WAIT_ACK state.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
3 years agoMerge pull request #232 from gromero/for-anton
Michael Neuling [Tue, 14 Jul 2020 06:08:04 +0000 (16:08 +1000)]
Merge pull request #232 from gromero/for-anton

Enhance hello_world