-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple-nix.hsfiles
767 lines (653 loc) · 24.4 KB
/
simple-nix.hsfiles
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
{-# START_FILE .vscode/extensions.json #-}
{
"recommendations": ["haskell.haskell"]
}
{-# START_FILE .vscode/settings.json #-}
{
"files.exclude": {
"**/*.dyn_hi": true,
"**/*.dyn_o": true,
"**/*.hi": true,
"**/*.o": true,
"**/dist-newstyle": true,
"**/.stack-work": true,
"**/.ghc.environment.*": true,
"**/*.o-boot": true,
"**/*.hi-boot": true,
},
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"editor.tabSize": 2
}
{-# START_FILE README.md #-}
<!-- omit in toc -->
# Simple Nix Starter Project
This starter project contains the scaffolding needed to integrate Clash with the Nix Stack build system. Read [Simple Starter Project](https://github.com/clash-lang/clash-starters/blob/main/simple/README.md) for more information on the various files.
<!-- omit in toc -->
# Table of Contents
- [Getting this project](#getting-this-project)
- [Building and testing this project](#building-and-testing-this-project)
- [REPL](#repl)
- [Adding custom dependencies / updating nix](#adding-custom-dependencies--updating-nix)
# Getting this project
Run `nix-shell --packages stack --run "stack new my-clash-project clash-lang/simple-nix"`.
# Building and testing this project
Build the project with:
```bash
nix-build
```
Verilog code will be available under the `result/share/verilog` directory.
Modify the `hdl` variable in `default.nix` to configure whether to generate
SystemVerilog or VHDL.
However development itself is more streamlined by using a Nix shell. Start one
by invoking:
```
nix-shell
```
Then, to run the tests defined in `tests/`:
```bash
cabal run test-library
cabal run doctests
```
To compile the project to VHDL run:
```bash
cabal run clash -- Example.Project --vhdl
```
You can find the HDL files in `vhdl/`. The source can be found in `src/Example/Project.hs`.
# REPL
Clash offers a [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) as a quick way to try things, similar to Python's `python` or Ruby's `irb`. Open the REPL by invoking:
```
cabal run clashi
```
# Adding custom dependencies / updating nix
`niv` is available after opening `nix-shell`. See [niv on Hackage](https://hackage.haskell.org/package/niv) for more information.
{-# START_FILE bin/Clash.hs #-}
import Prelude
import System.Environment (getArgs)
import Clash.Main (defaultMain)
main :: IO ()
main = getArgs >>= defaultMain
{-# START_FILE bin/Clashi.hs #-}
import Prelude
import System.Environment (getArgs)
import Clash.Main (defaultMain)
main :: IO ()
main = getArgs >>= defaultMain . ("--interactive":)
{-# START_FILE cabal.project #-}
packages:
{{name}}.cabal
write-ghc-environment-files: always
-- Eliminates the need for `--enable-tests`, which is needed for HLS.
tests: true
{-# START_FILE default.nix #-}
{ nixpkgs ? import ./nix/nixpkgs.nix {} }:
with nixpkgs.pkgs;
with gitignore;
let
hdl = "verilog";
topModule = "Example.Project";
hs-build = haskellPackages.callCabal2nix "{{name}}" (gitignoreSource ./.) {};
in haskell.lib.overrideCabal hs-build (drv: {
enableLibraryProfiling = false;
postBuild = ''
dist/build/clash/clash \
${topModule} --${hdl} \
-package-db dist/package.conf.inplace
'';
postInstall = ''
mkdir -p "$out/share"
cp -r "${hdl}/" "$out/share/${hdl}"
'';
})
{-# START_FILE hie.yaml #-}
cradle:
cabal:
- path: "./src"
component: "lib:{{ name }}"
- path: "./tests/doctests.hs"
component: "{{ name }}:doctests"
- path: "./tests"
component: "{{ name }}:test-library"
- path: "./bin/Clashi.hs"
component: "{{ name }}:exe:clashi"
- path: "./bin/Clash.hs"
component: "{{ name }}:exe:clash"
{-# START_FILE nix/nixpkgs.nix #-}
{ sources ? import ./sources.nix }:
let
overlay = _: pkgs: {
# Nix tooling
niv = (import sources.niv {}).niv;
gitignore = import sources.gitignore { inherit (pkgs) lib; };
# Haskell overrides
haskellPackages = pkgs.haskellPackages.override {
overrides = self: super: {
# Add overrides here
doctest-parallel =
self.callCabal2nix "doctest-parallel" sources.doctest-parallel {};
clash-prelude =
self.callCabal2nix "clash-prelude" (sources.clash-compiler + "/clash-prelude") {};
clash-lib =
self.callCabal2nix "clash-lib" (sources.clash-compiler + "/clash-lib") {};
clash-ghc =
self.callCabal2nix "clash-ghc" (sources.clash-compiler + "/clash-ghc") {};
clash-prelude-hedgehog =
self.callCabal2nix "clash-prelude" (sources.clash-compiler + "/clash-prelude-hedgehog") {};
tasty-hedgehog =
self.callCabal2nix "tasty-hedgehog" sources.tasty-hedgehog {};
hedgehog =
self.callCabal2nix "hedgehog" (sources.haskell-hedgehog + "/hedgehog") {};
};
};
};
in import sources.nixpkgs { overlays = [ overlay ]; }
{-# START_FILE nix/sources.json #-}
{
"clash-compiler": {
"branch": "1.8",
"description": "Haskell to VHDL/Verilog/SystemVerilog compiler",
"homepage": "https://clash-lang.org/",
"owner": "clash-lang",
"repo": "clash-compiler",
"rev": "49f33ea971eb52b1dbdb7b11dafe0849408001f6",
"sha256": "102l671jr1jxvs8qmqxxrq5fdf254pcmjv9z1dwc7sbfbzy9jxqb",
"type": "tarball",
"url": "https://github.com/clash-lang/clash-compiler/archive/49f33ea971eb52b1dbdb7b11dafe0849408001f6.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
"version": "1.8.2"
},
"doctest-parallel": {
"branch": "main",
"description": "Test interactive Haskell examples",
"homepage": null,
"owner": "martijnbastiaan",
"repo": "doctest-parallel",
"rev": "d73df0a2fd58b0b6aba438eb40aa56d30724e42a",
"sha256": "1k88bkwz2crvb6dafcf6y5y6wm0m2qvds57f3b0rx4id7la4qv89",
"type": "tarball",
"url": "https://github.com/martijnbastiaan/doctest-parallel/archive/d73df0a2fd58b0b6aba438eb40aa56d30724e42a.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
"version": "0.3.1"
},
"gitignore": {
"branch": "master",
"description": "Nix function for filtering local git sources",
"homepage": "",
"owner": "hercules-ci",
"repo": "gitignore",
"rev": "bff2832ec341cf30acb3a4d3e2e7f1f7b590116a",
"sha256": "0va0janxvmilm67nbl81gdbpppal4aprxzb25gp9pqvf76ahxsci",
"type": "tarball",
"url": "https://github.com/hercules-ci/gitignore/archive/bff2832ec341cf30acb3a4d3e2e7f1f7b590116a.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"haskell-hedgehog": {
"branch": "master",
"description": "Release with confidence, state-of-the-art property testing for Haskell.",
"homepage": "",
"owner": "hedgehogqa",
"repo": "haskell-hedgehog",
"rev": "52c35cabe24de2a1c03e72dde9d04b64f81d1f44",
"sha256": "1f9znljkmrdd4nlfmjfi8kx0fgcysp328rz27099n7bygchpgjr6",
"type": "tarball",
"url": "https://github.com/hedgehogqa/haskell-hedgehog/archive/52c35cabe24de2a1c03e72dde9d04b64f81d1f44.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
"version": "1.4"
},
"niv": {
"branch": "master",
"description": "Easy dependency management for Nix projects",
"homepage": "https://github.com/nmattia/niv",
"owner": "nmattia",
"repo": "niv",
"rev": "723f0eeb969a730db3c30f977c2b66b9dce9fe4a",
"sha256": "0016l7230gd2kdh0g2w573r9a2krqb7x4ifcjhhsn4h1bwap7qr0",
"type": "tarball",
"url": "https://github.com/nmattia/niv/archive/723f0eeb969a730db3c30f977c2b66b9dce9fe4a.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs": {
"branch": "nixpkgs-unstable",
"description": "Nix Packages collection",
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "911ad1e67f458b6bcf0278fa85e33bb9924fed7e",
"sha256": "0l7q3scvkq94749nkxv9p25494aspicf2559p5lyy2yii1dqyf1m",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/911ad1e67f458b6bcf0278fa85e33bb9924fed7e.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"tasty-hedgehog": {
"branch": "master",
"description": "Tasty integration for the Hedgehog property testing library",
"homepage": "",
"owner": "qfpl",
"repo": "tasty-hedgehog",
"rev": "ed07ecef3f6a01572b577b450ba6d58108173125",
"sha256": "1b8y5ibg1ihgf44nyym4g45lwmabymfcjb2nigv93s2fmng9zp6r",
"type": "tarball",
"url": "https://github.com/qfpl/tasty-hedgehog/archive/ed07ecef3f6a01572b577b450ba6d58108173125.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
"version": "1.4.0.2"
}
}
{-# START_FILE nix/sources.nix #-}
# This file has been generated by Niv.
let
#
# The fetchers. fetch_<type> fetches specs of type <type>.
#
fetch_file = pkgs: name: spec:
let
name' = sanitizeName name + "-src";
in
if spec.builtin or true then
builtins_fetchurl { inherit (spec) url sha256; name = name'; }
else
pkgs.fetchurl { inherit (spec) url sha256; name = name'; };
fetch_tarball = pkgs: name: spec:
let
name' = sanitizeName name + "-src";
in
if spec.builtin or true then
builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
else
pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
fetch_git = name: spec:
let
ref =
spec.ref or (
if spec ? branch then "refs/heads/${spec.branch}" else
if spec ? tag then "refs/tags/${spec.tag}" else
abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!"
);
submodules = spec.submodules or false;
submoduleArg =
let
nixSupportsSubmodules = builtins.compareVersions builtins.nixVersion "2.4" >= 0;
emptyArgWithWarning =
if submodules
then
builtins.trace
(
"The niv input \"${name}\" uses submodules "
+ "but your nix's (${builtins.nixVersion}) builtins.fetchGit "
+ "does not support them"
)
{ }
else { };
in
if nixSupportsSubmodules
then { inherit submodules; }
else emptyArgWithWarning;
in
builtins.fetchGit
({ url = spec.repo; inherit (spec) rev; inherit ref; } // submoduleArg);
fetch_local = spec: spec.path;
fetch_builtin-tarball = name: throw
''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
$ niv modify ${name} -a type=tarball -a builtin=true'';
fetch_builtin-url = name: throw
''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
$ niv modify ${name} -a type=file -a builtin=true'';
#
# Various helpers
#
# https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695
sanitizeName = name:
(
concatMapStrings (s: if builtins.isList s then "-" else s)
(
builtins.split "[^[:alnum:]+._?=-]+"
((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name)
)
);
# The set of packages used when specs are fetched using non-builtins.
mkPkgs = sources: system:
let
sourcesNixpkgs =
import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; };
hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
hasThisAsNixpkgsPath = <nixpkgs> == ./.;
in
if builtins.hasAttr "nixpkgs" sources
then sourcesNixpkgs
else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
import <nixpkgs> { }
else
abort
''
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
add a package called "nixpkgs" to your sources.json.
'';
# The actual fetching function.
fetch = pkgs: name: spec:
if ! builtins.hasAttr "type" spec then
abort "ERROR: niv spec ${name} does not have a 'type' attribute"
else if spec.type == "file" then fetch_file pkgs name spec
else if spec.type == "tarball" then fetch_tarball pkgs name spec
else if spec.type == "git" then fetch_git name spec
else if spec.type == "local" then fetch_local spec
else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
else if spec.type == "builtin-url" then fetch_builtin-url name
else
abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
# If the environment variable NIV_OVERRIDE_${name} is set, then use
# the path directly as opposed to the fetched source.
replace = name: drv:
let
saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name;
ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
in
if ersatz == "" then drv else
# this turns the string into an actual Nix path (for both absolute and
# relative paths)
if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}";
# Ports of functions for older nix versions
# a Nix version of mapAttrs if the built-in doesn't exist
mapAttrs = builtins.mapAttrs or (
f: set: with builtins;
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
);
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
range = first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1);
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
concatMapStrings = f: list: concatStrings (map f list);
concatStrings = builtins.concatStringsSep "";
# https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
optionalAttrs = cond: as: if cond then as else { };
# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12" then
fetchTarball ({ inherit url; } // (optionalAttrs (name != null) { inherit name; }))
else
fetchTarball attrs;
# fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12" then
fetchurl ({ inherit url; } // (optionalAttrs (name != null) { inherit name; }))
else
fetchurl attrs;
# Create the final "sources" from the config
mkSources = config:
mapAttrs
(
name: spec:
if builtins.hasAttr "outPath" spec
then
abort
"The values in sources.json should not have an 'outPath' attribute"
else
spec // { outPath = replace name (fetch config.pkgs name spec); }
)
config.sources;
# The "config" used by the fetchers
mkConfig =
{ sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
, sources ? if sourcesFile == null then { } else builtins.fromJSON (builtins.readFile sourcesFile)
, system ? builtins.currentSystem
, pkgs ? mkPkgs sources system
}: rec {
# The sources, i.e. the attribute set of spec name to spec
inherit sources;
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
inherit pkgs;
};
in
mkSources (mkConfig { }) // { __functor = _: settings: mkSources (mkConfig settings); }
{-# START_FILE release.nix #-}
let
pkgs = import ./nix/nixpkgs.nix { };
in
pkgs.haskellPackages.callPackage ./default.nix { }
{-# START_FILE shell.nix #-}
{ nixpkgs ? import ./nix/nixpkgs.nix {} }:
let
inherit (nixpkgs) pkgs;
inherit (pkgs) haskellPackages;
project = import ./release.nix;
in
pkgs.stdenv.mkDerivation {
name = "shell";
buildInputs = project.env.nativeBuildInputs ++ [
haskellPackages.cabal-install
];
LC_ALL = "C.UTF-8";
}
{-# START_FILE src/Example/Project.hs #-}
-- @createDomain@ below generates a warning about orphan instances, but we like
-- our code to be warning-free.
{-# OPTIONS_GHC -Wno-orphans #-}
module Example.Project where
import Clash.Prelude
-- Create a domain with the frequency of your input clock. For this example we used
-- 50 MHz.
createDomain vSystem{vName="Dom50", vPeriod=hzToPeriod 50e6}
-- | @topEntity@ is Clash@s equivalent of @main@ in other programming languages.
-- Clash will look for it when compiling "Example.Project" and translate it to
-- HDL. While polymorphism can be used freely in Clash projects, a @topEntity@
-- must be monomorphic and must use non- recursive types. Or, to put it
-- hand-wavily, a @topEntity@ must be translatable to a static number of wires.
--
-- Top entities must be monomorphic, meaning we have to specify all type variables.
-- In this case, we are using the @Dom50@ domain, which we created with @createDomain@
-- and we are using 8-bit unsigned numbers.
topEntity ::
Clock Dom50 ->
Reset Dom50 ->
Enable Dom50 ->
Signal Dom50 (Unsigned 8) ->
Signal Dom50 (Unsigned 8)
topEntity = exposeClockResetEnable accum
-- To specify the names of the ports of our top entity, we create a @Synthesize@ annotation.
{-# ANN topEntity
(Synthesize
{ t_name = "accum"
, t_inputs = [ PortName "CLK"
, PortName "RST"
, PortName "EN"
, PortName "DIN"
]
, t_output = PortName "DOUT"
}) #-}
-- Make sure GHC does not apply any optimizations to the boundaries of the design.
-- For GHC versions 9.2 or older, use: {-# NOINLINE topEntity #-}
{-# OPAQUE topEntity #-}
-- | A simple accumulator that works on unsigned numbers of any size.
-- It has hidden clock, reset, and enable signals.
accum ::
(HiddenClockResetEnable dom, KnownNat n) =>
Signal dom (Unsigned n) ->
Signal dom (Unsigned n)
accum = mealy accumT 0
where
accumT s i = (s + i, s)
{-# START_FILE stack.yaml #-}
resolver: lts-22.43
extra-deps:
- clash-ghc-1.8.2@sha256:a5e5fe902fbc02f2fc825e13855b9e57bbc782928bc1820c29d3faabaa8457bb,9802
- clash-lib-1.8.2@sha256:bf494184c73cdb27016ce61d471a2ba3d3959703e576dda3181d85431fba47e2,15721
- clash-prelude-1.8.2@sha256:a9409f93aa7806a4fb4d39efc7fae78f638cfd0e9bd57140adf04e0f2fcc27bf,17871
- clash-prelude-hedgehog-1.8.2@sha256:4ffc0a4897a60cfae1bee5feaa946be652942bcdd624b3bf153b4596754ddd0b,1410
- prettyprinter-interp-0.2.0.0@sha256:69c339a95b265dab9b3478ca19ec96952b6b472bd0ff6e2127112a9562362c1d,2086
{-# START_FILE tests/Tests/Example/Project.hs #-}
module Tests.Example.Project where
import Prelude
import Clash.Hedgehog.Sized.Unsigned
import Test.Tasty
import Test.Tasty.TH
import Test.Tasty.Hedgehog
import qualified Clash.Prelude as C
import qualified Hedgehog as H
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
-- Import the module containing the @accum@ function
import Example.Project (accum)
-- Define a Hedgehog property to test the @accum@ function
prop_accum :: H.Property
prop_accum = H.property $ do
-- Simulate for a random duration between 1 and 100 cycles
simDuration <- H.forAll (Gen.integral (Range.linear 1 100))
-- Generate a list of random unsigned numbers.
inp <- H.forAll
(Gen.list (Range.singleton simDuration)
(genUnsigned Range.linearBounded))
let
-- Simulate the @accum@ function for the pre-existing @System@ domain
-- and 8 bit unsigned numbers.
--
-- The (hidden) reset input of @accum@ will be asserted in the first cycle;
-- during this cycle it will emit its initial value and the input is
-- ignored. So we need to present a dummy input value.
simOut = C.sampleN (simDuration + 1) (accum @C.System @8 (C.fromList (0:inp)))
-- Calculate the expected output. The first cycle is the initial value, and
-- the result of the final input value does not appear because the
-- accumulator has 1 cycle latency.
expected = 0 : init (scanl (+) 0 inp)
-- Check that the simulated output matches the expected output
simOut H.=== expected
accumTests :: TestTree
accumTests = $(testGroupGenerator)
main :: IO ()
main = defaultMain accumTests
{-# START_FILE tests/doctests.hs #-}
module Main where
import System.Environment (getArgs)
import Test.DocTest (mainFromCabal)
main :: IO ()
main = mainFromCabal "{{ name }}" =<< getArgs
{-# START_FILE tests/unittests.hs #-}
import Prelude
import Test.Tasty
import qualified Tests.Example.Project
main :: IO ()
main = defaultMain $ testGroup "."
[ Tests.Example.Project.accumTests
]
{-# START_FILE {{name}}.cabal #-}
cabal-version: 2.4
name: {{name}}
version: 0.1
license: BSD-2-Clause
author: John Smith <[email protected]>
maintainer: John Smith <[email protected]>
common common-options
default-extensions:
BangPatterns
BinaryLiterals
ConstraintKinds
DataKinds
DefaultSignatures
DeriveAnyClass
DeriveDataTypeable
DeriveFoldable
DeriveFunctor
DeriveGeneric
DeriveLift
DeriveTraversable
DerivingStrategies
FlexibleContexts
InstanceSigs
KindSignatures
LambdaCase
NamedFieldPuns
NoStarIsType
PolyKinds
RankNTypes
RecordWildCards
ScopedTypeVariables
StandaloneDeriving
TupleSections
TypeApplications
TypeFamilies
TypeOperators
ViewPatterns
-- TemplateHaskell is used to support convenience functions such as
-- 'listToVecTH' and 'bLit'.
TemplateHaskell
QuasiQuotes
-- Prelude isn't imported by default as Clash offers Clash.Prelude
NoImplicitPrelude
ghc-options:
-Wall -Wcompat
-haddock
-- Plugins to support type-level constraint solving on naturals
-fplugin GHC.TypeLits.Extra.Solver
-fplugin GHC.TypeLits.Normalise
-fplugin GHC.TypeLits.KnownNat.Solver
-- Clash needs access to the source code in compiled modules
-fexpose-all-unfoldings
-- Worker wrappers introduce unstable names for functions that might have
-- blackboxes attached for them. You can disable this, but be sure to add
-- a no-specialize pragma to every function with a blackbox.
-fno-worker-wrapper
-- Strict annotations - while sometimes preventing space leaks - trigger
-- optimizations Clash can't deal with. See:
--
-- https://github.com/clash-lang/clash-compiler/issues/2361
--
-- These flags disables these optimizations. Note that the fields will
-- remain strict.
-fno-unbox-small-strict-fields
-fno-unbox-strict-fields
build-depends:
base,
Cabal,
-- clash-prelude will set suitable version bounds for the plugins
clash-prelude >= 1.8.2 && < 1.10,
ghc-typelits-natnormalise,
ghc-typelits-extra,
ghc-typelits-knownnat
library
import: common-options
hs-source-dirs: src
exposed-modules:
Example.Project
default-language: Haskell2010
-- Builds the executable 'clash', with {{name}} project in scope
executable clash
main-is: bin/Clash.hs
default-language: Haskell2010
Build-Depends: base, clash-ghc, {{name}}
-- Builds the executable 'clashi', with {{name}} project in scope
executable clashi
main-is: bin/Clashi.hs
default-language: Haskell2010
build-depends: base, clash-ghc, {{name}}
test-suite doctests
type: exitcode-stdio-1.0
default-language: Haskell2010
main-is: doctests.hs
ghc-options: -Wall -Wcompat -threaded
hs-source-dirs: tests
build-depends:
base,
{{ name }},
doctest-parallel >= 0.2 && < 0.4,
test-suite test-library
import: common-options
default-language: Haskell2010
hs-source-dirs: tests
type: exitcode-stdio-1.0
ghc-options: -threaded
main-is: unittests.hs
other-modules:
Tests.Example.Project
build-depends:
{{name}},
QuickCheck,
clash-prelude-hedgehog,
hedgehog,
tasty >= 1.2 && < 1.6,
tasty-hedgehog,
tasty-th