diff --git a/sim/HelloWorld.nc b/sim/HelloWorld.nc
new file mode 100644
index 0000000..637a4f3
--- /dev/null
+++ b/sim/HelloWorld.nc
@@ -0,0 +1,323 @@
+( Made using CamBam - http://www.cambam.co.uk )
+( Untitled 2/13/2012 2:54:50 PM )
+( T0 : 0.0394 )
+G20 G90 G64 G40
+G0 Z0.125
+( T0 : 0.0394 )
+T0 M6
+( Engrave1 )
+G17
+M3 S1000
+G0 X-3.0 Y-0.0451
+G1 F10.0 Z-0.001
+G1 F60.0 X-2.8845 Y-0.035
+G3 X-2.8723 Y-0.0943 I0.3244 J0.0363
+G3 X-2.8467 Y-0.1492 I0.2072 J0.0631
+G3 X-2.805 Y-0.1917 I0.1471 J0.1027
+G3 X-2.7533 Y-0.2212 I0.1578 J0.2169
+G3 X-2.6114 Y-0.2483 I0.1366 J0.3297
+G3 X-2.4877 Y-0.2275 I0.006 J0.3418
+G3 X-2.4438 Y-0.2048 I-0.0766 J0.2022
+G3 X-2.4082 Y-0.1707 I-0.0792 J0.1182
+G3 X-2.3817 Y-0.0912 I-0.1063 J0.0796
+G3 X-2.4069 Y-0.0155 I-0.1191 J0.0024
+G3 X-2.4449 Y0.0175 I-0.1101 J-0.0883
+G3 X-2.4902 Y0.0394 I-0.1379 J-0.2279
+G3 X-2.572 Y0.0637 I-0.3019 J-0.8663
+G3 X-2.6549 Y0.0842 I-1.0468 J-4.0562
+G2 X-2.7457 Y0.1086 I0.3878 J1.6213
+G2 X-2.8334 Y0.1422 I0.1472 J0.5158
+G2 X-2.8886 Y0.1787 I0.1401 J0.2718
+G2 X-2.9325 Y0.2281 I0.1404 J0.1692
+G2 X-2.9647 Y0.3435 I0.1821 J0.1129
+G2 X-2.9249 Y0.4741 I0.2336 J0.0003
+G2 X-2.8088 Y0.5669 I0.2044 J-0.1368
+G2 X-2.6391 Y0.5984 I0.1639 J-0.4097
+G2 X-2.5469 Y0.5908 I0.0024 J-0.5372
+G2 X-2.458 Y0.565 I-0.0669 J-0.395
+G2 X-2.3369 Y0.4678 I-0.0947 J-0.2421
+G2 X-2.2915 Y0.3221 I-0.2346 J-0.1531
+G1 X-2.4088 Y0.3132
+G3 X-2.4277 Y0.386 I-0.236 J-0.0223
+G3 X-2.4732 Y0.4457 I-0.1488 J-0.0662
+G3 X-2.5498 Y0.4817 I-0.1158 J-0.1469
+G3 X-2.6341 Y0.4905 I-0.08 J-0.3586
+G3 X-2.7184 Y0.4829 I-0.0045 J-0.4232
+G3 X-2.7962 Y0.4495 I0.0315 J-0.1804
+G3 X-2.8331 Y0.4066 I0.0864 J-0.1118
+G3 X-2.8467 Y0.3517 I0.1005 J-0.0539
+G3 X-2.8107 Y0.2697 I0.1052 J-0.0028
+G3 X-2.7679 Y0.2452 I0.1001 J0.125
+G3 X-2.698 Y0.2215 I0.2359 J0.583
+G3 X-2.6265 Y0.2034 I0.3957 J1.414
+G2 X-2.5228 Y0.1782 I-0.5539 J-2.5053
+G2 X-2.4215 Y0.1448 I-0.1803 J-0.7161
+G2 X-2.3553 Y0.1057 I-0.1442 J-0.3193
+G2 X-2.3022 Y0.0501 I-0.1407 J-0.1878
+G2 X-2.2637 Y-0.0811 I-0.19 J-0.127
+G2 X-2.306 Y-0.2205 I-0.2532 J0.0006
+G2 X-2.4278 Y-0.3221 I-0.2339 J0.1566
+G2 X-2.6057 Y-0.3581 I-0.1712 J0.3891
+G2 X-2.7129 Y-0.3503 I-0.0037 J0.6909
+G2 X-2.8164 Y-0.3215 I0.0637 J0.4292
+G2 X-2.9495 Y-0.2117 I0.1147 J0.2747
+G2 X-3.0 Y-0.0451 I0.265 J0.1712
+G0 Z0.125
+G0 X-2.111
+G1 F10.0 Z-0.001
+G1 F60.0 Y0.5826
+G1 X-1.9975
+G1 Y0.2508
+G2 X-1.9077 Y0.3198 I0.2024 J-0.1703
+G2 X-1.7969 Y0.3429 I0.1077 J-0.2394
+G2 X-1.6675 Y0.3132 I0.0063 J-0.2692
+G2 X-1.5893 Y0.2325 I-0.0755 J-0.1515
+G2 X-1.5698 Y0.1586 I-0.2396 J-0.1028
+G2 X-1.5653 Y0.0823 I-0.5851 J-0.0725
+G1 Y-0.3423
+G1 X-1.6789
+G1 Y0.0823
+G3 X-1.6859 Y0.1476 I-0.2729 J0.0036
+G3 X-1.7161 Y0.206 I-0.1238 J-0.027
+G3 X-1.764 Y0.2366 I-0.0845 J-0.0794
+G3 X-1.8202 Y0.2451 I-0.0537 J-0.1649
+G3 X-1.9155 Y0.2186 I-0.0001 J-0.1841
+G3 X-1.9786 Y0.1479 I0.072 J-0.1278
+G3 X-1.9938 Y0.087 I0.221 J-0.0878
+G3 X-1.9975 Y0.0243 I0.4875 J-0.0599
+G1 Y-0.3423
+G1 X-2.111
+G1 Y-0.0451
+G0 Z0.125
+G0 X-1.4292 Y-0.1385
+G1 F10.0 Z-0.001
+G2 F60.0 X-1.4088 Y-0.076 I0.182 J-0.0249
+G2 X-1.3502 Y-0.0104 I0.1662 J-0.0896
+G2 X-1.2681 Y0.0268 I0.1554 J-0.2334
+G2 X-1.1672 Y0.0438 I0.1926 J-0.8334
+G3 X-1.065 Y0.0586 I-0.2128 J1.8303
+G3 X-0.9647 Y0.0829 I-0.1109 J0.6767
+G3 X-0.9641 Y0.1126 I-1.9501 J0.0563
+G3 X-0.97 Y0.1648 I-0.2046 J0.003
+G3 X-0.9962 Y0.2104 I-0.0883 J-0.0204
+G3 X-1.0574 Y0.2412 I-0.0943 J-0.111
+G3 X-1.1256 Y0.2489 I-0.0647 J-0.2692
+G3 X-1.187 Y0.2436 I-0.0032 J-0.3217
+G3 X-1.2442 Y0.2205 I0.0237 J-0.141
+G3 X-1.2805 Y0.1756 I0.0774 J-0.0998
+G3 X-1.3003 Y0.1214 I0.2336 J-0.1162
+G1 X-1.4114 Y0.1366
+G2 X-1.3931 Y0.1969 I0.337 J-0.0694
+G2 X-1.3615 Y0.2514 I0.2096 J-0.0848
+G2 X-1.3162 Y0.2923 I0.1453 J-0.1155
+G2 X-1.2612 Y0.3189 I0.1409 J-0.221
+G2 X-1.1092 Y0.3429 I0.1442 J-0.4206
+G2 X-1.0386 Y0.3388 I0.0029 J-0.5642
+G2 X-0.9697 Y0.3227 I-0.0351 J-0.3052
+G2 X-0.927 Y0.3022 I-0.0865 J-0.2354
+G2 X-0.8909 Y0.2716 I-0.0727 J-0.1224
+G2 X-0.8555 Y0.1946 I-0.1386 J-0.1102
+G2 X-0.8507 Y0.1424 I-0.3836 J-0.0615
+G2 X-0.8499 Y0.0899 I-1.3758 J-0.0494
+G1 Y-0.0615
+G3 X-0.8489 Y-0.1619 I4.6791 J-0.0068
+G3 X-0.8429 Y-0.2622 I0.9512 J0.0067
+G3 X-0.8139 Y-0.3423 I0.2546 J0.0469
+G1 X-0.9325
+G2 X-0.9552 Y-0.2596 I0.2182 J0.1044
+G2 X-1.077 Y-0.3354 I-0.2823 J0.3183
+G2 X-1.2019 Y-0.3574 I-0.1221 J0.3266
+G2 X-1.2917 Y-0.3463 I-0.0042 J0.3355
+G2 X-1.3716 Y-0.3038 I0.0496 J0.1895
+G2 X-1.4309 Y-0.1656 I0.1197 J0.1332
+G2 X-1.4292 Y-0.1385 I0.1837 J0.0022
+G0 Z0.125
+G0 X-1.3093 Y-0.1513
+G1 F10.0 Z-0.001
+G3 F60.0 X-1.3098 Y-0.1625 I0.0949 J-0.0101
+G3 X-1.2757 Y-0.2382 I0.0971 J-0.0018
+G3 X-1.228 Y-0.2624 I0.075 J0.0889
+G3 X-1.1748 Y-0.2685 I0.0506 J0.2058
+G3 X-1.0581 Y-0.2401 I0.0042 J0.2367
+G3 X-0.983 Y-0.1612 I-0.0857 J0.1567
+G3 X-0.9681 Y-0.1054 I-0.1756 J0.0768
+G3 X-0.9647 Y-0.0477 I-0.4434 J0.0549
+G1 Y-0.006
+G2 X-1.0563 Y-0.0325 I-0.2032 J0.5301
+G2 X-1.1502 Y-0.0489 I-0.2867 J1.3668
+G3 X-1.2003 Y-0.0575 I0.1165 J-0.8276
+G3 X-1.2492 Y-0.0716 I0.0505 J-0.2671
+G3 X-1.294 Y-0.1089 I0.0375 J-0.0907
+G3 X-1.3093 Y-0.1513 I0.0797 J-0.0526
+G0 Z0.125
+G0 X-0.6738
+G1 F10.0 Z-0.001
+G1 F60.0 Y0.3277
+G1 X-0.5704
+G1 Y0.2407
+G2 X-0.4877 Y0.317 I0.2085 J-0.1428
+G2 X-0.376 Y0.3429 I0.1052 J-0.2003
+G2 X-0.2246 Y0.2987 I0.0049 J-0.265
+G2 X-0.1256 Y0.1738 I-0.1536 J-0.2235
+G2 X-0.0921 Y-0.0022 I-0.4197 J-0.1709
+G2 X-0.1294 Y-0.1877 I-0.4477 J-0.0066
+G2 X-0.2366 Y-0.3139 I-0.2559 J0.1088
+G2 X-0.3842 Y-0.3574 I-0.1486 J0.2319
+G2 X-0.4865 Y-0.3335 I-0.0033 J0.2159
+G2 X-0.5603 Y-0.2729 I0.1079 J0.2068
+G1 Y-0.5991
+G1 X-0.6738
+G1 Y-0.1513
+G0 Z0.125
+G0 X-0.5586 Y-0.1219
+G1 F10.0 Z-0.001
+G3 F60.0 X-0.5186 Y-0.2022 I0.2066 J0.0526
+G3 X-0.4624 Y-0.2478 I0.1393 J0.1145
+G3 X-0.3918 Y-0.2641 I0.0691 J0.1387
+G3 X-0.3198 Y-0.2472 I0.0014 J0.1565
+G3 X-0.2625 Y-0.2003 I-0.0865 J0.1642
+G3 X-0.2186 Y-0.1056 I-0.1741 J0.1382
+G3 X-0.2082 Y-0.0016 I-0.463 J0.0985
+G3 X-0.2185 Y0.0988 I-0.445 J0.0051
+G3 X-0.2612 Y0.1902 I-0.2152 J-0.0448
+G3 X-0.3165 Y0.2368 I-0.1435 J-0.1141
+G3 X-0.3868 Y0.2539 I-0.0691 J-0.1308
+G3 X-0.459 Y0.2348 I-0.0004 J-0.1446
+G3 X-0.5155 Y0.1858 I0.1042 J-0.1769
+G3 X-0.5599 Y0.0921 I0.1824 J-0.1439
+G3 X-0.571 Y-0.0111 I0.4286 J-0.0981
+G3 X-0.561 Y-0.1111 I0.4541 J-0.0052
+G3 X-0.5586 Y-0.1219 I0.209 J0.0418
+G0 Z0.125
+G0 X0.0092 Y-0.0653
+G1 F10.0 Z-0.001
+G2 F60.0 X0.0069 Y-0.013 I0.4635 J0.0463
+G2 X0.0253 Y0.1266 I0.49 J0.0063
+G2 X0.094 Y0.2495 I0.2937 J-0.0834
+G2 X0.1968 Y0.3207 I0.209 J-0.1921
+G2 X0.3199 Y0.3429 I0.1188 J-0.3064
+G2 X0.4396 Y0.321 I0.004 J-0.3162
+G2 X0.5394 Y0.2514 I-0.1055 J-0.2578
+G2 X0.6066 Y0.1308 I-0.2207 J-0.202
+G2 X0.6246 Y-0.006 I-0.4641 J-0.1307
+G2 X0.624 Y-0.0363 I-1.1335 J0.0085
+G1 X0.1243
+G3 X0.1404 Y-0.1264 I0.3467 J0.0157
+G3 X0.1867 Y-0.2054 I0.1984 J0.0632
+G3 X0.3268 Y-0.2641 I0.1356 J0.1272
+G3 X0.4334 Y-0.2312 I0.0048 J0.1741
+G3 X0.4761 Y-0.184 I-0.1056 J0.1381
+G3 X0.5034 Y-0.1265 I-0.2498 J0.1544
+G1 X0.6208 Y-0.141
+G2 X0.583 Y-0.2296 I-0.3145 J0.082
+G2 X0.518 Y-0.3007 I-0.2247 J0.1403
+G2 X0.4267 Y-0.3449 I-0.1599 J0.2137
+G2 X0.3262 Y-0.3574 I-0.0969 J0.3673
+G2 X0.1999 Y-0.337 I-0.005 J0.3687
+G2 X0.0927 Y-0.2672 I0.094 J0.2616
+G2 X0.0252 Y-0.1484 I0.2166 J0.2017
+G2 X0.0092 Y-0.0653 I0.4475 J0.1294
+G0 Z0.125
+G0 X0.1306 Y0.0571
+G1 F10.0 Z-0.001
+G1 F60.0 X0.5047
+G3 X0.4926 Y0.1236 I-0.3356 J-0.0268
+G3 X0.4618 Y0.1839 I-0.1636 J-0.0456
+G3 X0.3211 Y0.2495 I-0.1362 J-0.1084
+G3 X0.1893 Y0.1971 I-0.0043 J-0.1815
+G3 X0.1467 Y0.1326 I0.1313 J-0.1328
+G3 X0.1306 Y0.0571 I0.2303 J-0.0887
+G0 Z0.125
+G0 X0.7412 Y0.0837
+G1 F10.0 Z-0.001
+G2 F60.0 X0.7407 Y0.1082 I0.5215 J0.0227
+G2 X0.7673 Y0.3004 I0.6459 J0.0084
+G2 X0.8643 Y0.4684 I0.405 J-0.1218
+G2 X1.0099 Y0.568 I0.2954 J-0.2758
+G2 X1.1836 Y0.599 I0.1677 J-0.4369
+G2 X1.4145 Y0.5378 I0.0061 J-0.4431
+G2 X1.5709 Y0.3669 I-0.2021 J-0.3421
+G2 X1.6126 Y0.246 I-0.4508 J-0.2228
+G2 X1.6252 Y0.1189 I-0.6041 J-0.1243
+G2 X1.6119 Y-0.0104 I-0.6073 J-0.0028
+G2 X1.5684 Y-0.1328 I-0.4911 J0.1054
+G2 X1.4075 Y-0.3013 I-0.3494 J0.1726
+G2 X1.1829 Y-0.3581 I-0.2218 J0.4049
+G2 X1.0618 Y-0.343 I-0.0025 J0.4735
+G2 X0.9495 Y-0.295 I0.1059 J0.4032
+G2 X0.7937 Y-0.1227 I0.2106 J0.3471
+G2 X0.7412 Y0.0837 I0.469 J0.2292
+G0 Z0.125
+G0 X0.867 Y0.0872
+G1 F10.0 Z-0.001
+G3 F60.0 X0.8863 Y-0.0342 I0.4738 J0.0131
+G3 X0.9564 Y-0.1574 I0.3033 J0.091
+G3 X1.1823 Y-0.2533 I0.2187 J0.2012
+G3 X1.3066 Y-0.2297 I0.004 J0.318
+G3 X1.4094 Y-0.1562 I-0.1146 J0.269
+G3 X1.4803 Y-0.0268 I-0.2365 J0.2136
+G3 X1.499 Y0.1195 I-0.5092 J0.1396
+G3 X1.4904 Y0.22 I-0.5565 J0.0028
+G3 X1.4605 Y0.3164 I-0.4006 J-0.0713
+G3 X1.3489 Y0.447 I-0.2653 J-0.1138
+G3 X1.1842 Y0.4937 I-0.1607 J-0.2531
+G3 X0.9602 Y0.4041 I-0.0045 J-0.3134
+G3 X0.9124 Y0.3396 I0.1696 J-0.1757
+G3 X0.8761 Y0.2256 I0.3481 J-0.1739
+G3 X0.8669 Y0.1063 I0.7033 J-0.1142
+G3 X0.867 Y0.0872 I0.474 J-0.0059
+G0 Z0.125
+G0 X1.7691
+G1 F10.0 Z-0.001
+G1 F60.0 Y0.5826
+G1 X1.8826
+G1 Y0.0552
+G1 X2.1514 Y0.3277
+G1 X2.2984
+G1 X2.0422 Y0.0792
+G1 X2.3243 Y-0.3423
+G1 X2.1842
+G1 X1.9627 Y0.0003
+G1 X1.8826 Y-0.0767
+G1 Y-0.3423
+G1 X1.7691
+G1 Y0.0872
+G0 Z0.125
+G0 X2.3737 Y0.0387
+G1 F10.0 Z-0.001
+G2 F60.0 X2.3936 Y0.142 I0.4767 J-0.0383
+G2 X2.4757 Y0.2684 I0.2668 J-0.0833
+G2 X2.5741 Y0.3252 I0.1952 J-0.225
+G2 X2.6864 Y0.3429 I0.109 J-0.3265
+G2 X2.8089 Y0.3214 I0.0042 J-0.3359
+G2 X2.9123 Y0.252 I-0.1045 J-0.2674
+G2 X2.9809 Y0.1358 I-0.2103 J-0.2027
+G2 X3.0 Y0.0022 I-0.4192 J-0.1278
+G2 X2.9922 Y-0.102 I-0.6432 J-0.0046
+G2 X2.9608 Y-0.2016 I-0.3284 J0.0487
+G2 X2.8479 Y-0.3164 I-0.2422 J0.1253
+G2 X2.6864 Y-0.3574 I-0.1586 J0.2861
+G2 X2.563 Y-0.3364 I-0.0045 J0.3461
+G2 X2.4586 Y-0.2672 I0.1004 J0.2648
+G2 X2.3902 Y-0.1456 I0.2134 J0.2001
+G2 X2.3722 Y-0.0073 I0.4726 J0.1318
+G2 X2.3737 Y0.0387 I0.4782 J0.0077
+G0 Z0.125
+G0 X2.4901 Y0.0298
+G1 F10.0 Z-0.001
+G3 F60.0 X2.4889 Y-0.0073 I0.4225 J-0.032
+G3 X2.5 Y-0.1088 I0.4249 J-0.0051
+G3 X2.5451 Y-0.2003 I0.2117 J0.0474
+G3 X2.6864 Y-0.2641 I0.1376 J0.1166
+G3 X2.8271 Y-0.1997 I0.0032 J0.1789
+G3 X2.8724 Y-0.1065 I-0.1677 J0.1391
+G3 X2.8832 Y-0.0035 I-0.432 J0.0977
+G3 X2.8719 Y0.0957 I-0.3951 J0.0049
+G3 X2.8265 Y0.1845 I-0.2065 J-0.0494
+G3 X2.6864 Y0.2489 I-0.1372 J-0.1139
+G3 X2.5451 Y0.1852 I-0.0037 J-0.1803
+G3 X2.4999 Y0.0939 I0.164 J-0.1379
+G3 X2.4901 Y0.0298 I0.4127 J-0.0961
+G0 Z0.125
+M5
+M30
\ No newline at end of file
diff --git a/sim/Makefile b/sim/Makefile
new file mode 100644
index 0000000..f81c151
--- /dev/null
+++ b/sim/Makefile
@@ -0,0 +1,47 @@
+# Part of Grbl Simulator
+#
+# Copyright (c) 2012 Jens Geisler
+#
+# Grbl is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Grbl is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Grbl. If not, see .
+
+OBJECTS = main.o simulator.o runtime.o ../protocol.o ../planner.o ../settings.o ../print.o ../nuts_bolts.o eeprom.o serial.o avr\pgmspace.o avr\interrupt.o util\delay.o util\floatunsisf.o ../stepper.o ../gcode.o ../spindle_control.o ../motion_control.o ../limits.o ../report.o ../coolant_control.o
+
+
+CLOCK = 16000000
+EXE_NAME= grbl_sim.exe
+COMPILER= C:\MinGW\bin\gcc.exe
+COMPILE = $(COMPILER) -Wall -Os -DF_CPU=$(CLOCK) -include config.h -I.
+
+# symbolic targets:
+all: main
+
+new: clean main
+
+clean:
+ rm -f $(EXE_NAME) $(OBJECTS)
+
+# file targets:
+main: $(OBJECTS)
+ $(COMPILE) -o $(EXE_NAME) $(OBJECTS) -lm -Wl,--gc-sections
+
+%.o: %.c
+ $(COMPILE) -c $< -o $@
+
+../protocol.o: ../protocol.c
+ $(COMPILE) -include rename_execute_runtime.h -c $< -o $@
+
+../planner.o: ../planner.c
+ $(COMPILE) -include planner_inject_accessors.c -c $< -o $@
+
+
diff --git a/sim/avr/interrupt.c b/sim/avr/interrupt.c
new file mode 100644
index 0000000..f47e11d
--- /dev/null
+++ b/sim/avr/interrupt.c
@@ -0,0 +1,42 @@
+/*
+ interrupt.c - replacement for the avr library of the same name to provide
+ dummy register variables
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+#include "interrupt.h"
+
+// dummy register variables
+uint16_t timsk0;
+uint16_t timsk1;
+uint16_t timsk2;
+uint16_t ocr1a;
+uint16_t ocr2a;
+uint16_t tcnt0;
+uint16_t tcnt2;
+uint16_t tccr0b;
+uint16_t tccr2b;
+uint16_t tccr1b;
+uint16_t tccr0a;
+uint16_t tccr1a;
+uint16_t tccr2a;
+uint16_t pcmsk0;
+uint16_t pcicr;
+
+void sei() {};
diff --git a/sim/avr/interrupt.h b/sim/avr/interrupt.h
new file mode 100644
index 0000000..5b17cc3
--- /dev/null
+++ b/sim/avr/interrupt.h
@@ -0,0 +1,82 @@
+/*
+ interrupt.h - replacement for the avr include of the same name to provide
+ dummy register variables and macros
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+
+#ifndef interrupt_h
+#define interrupt_h
+
+#include
+
+// dummy register variables
+extern uint16_t timsk0;
+extern uint16_t timsk1;
+extern uint16_t timsk2;
+extern uint16_t tcnt0;
+extern uint16_t tcnt2;
+extern uint16_t tccr0b;
+extern uint16_t tccr0a;
+extern uint16_t tccr2a;
+extern uint16_t tccr2b;
+extern uint16_t tccr1b;
+extern uint16_t tccr1a;
+extern uint16_t ocr1a;
+extern uint16_t ocr2a;
+extern uint16_t pcmsk0;
+extern uint16_t pcicr;
+
+// macros to turn avr interrupts into regular functions
+#define TIMER1_COMPA_vect
+#define ISR(a) void interrupt_ ## a ()
+
+// enable interrupts does nothing in the simulation environment
+void sei();
+
+// dummy macros for interrupt related registers
+#define TIMSK0 timsk0
+#define TIMSK1 timsk1
+#define TIMSK2 timsk2
+#define OCR1A ocr1a
+#define OCR2A ocr2a
+#define OCIE1A 0
+#define OCIE2A 0
+#define TCNT0 tcnt0
+#define TCNT2 tcnt2
+#define TCCR0B tccr0b
+#define TCCR0A tccr0a
+#define TCCR1A tccr1a
+#define TCCR1B tccr1b
+#define TCCR2A tccr2a
+#define TCCR2B tccr2b
+#define CS21 0
+#define CS10 0
+#define WGM13 0
+#define WGM12 0
+#define WGM11 0
+#define WGM10 0
+#define WGM21 0
+#define COM1A0 0
+#define COM1B0 0
+#define TOIE0 0
+#define TOIE2 0
+#define PCICR pcicr
+
+#endif
diff --git a/sim/avr/io.h b/sim/avr/io.h
new file mode 100644
index 0000000..885021a
--- /dev/null
+++ b/sim/avr/io.h
@@ -0,0 +1,21 @@
+/*
+ io.h - dummy replacement for the avr include of the same name
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
diff --git a/sim/avr/pgmspace.c b/sim/avr/pgmspace.c
new file mode 100644
index 0000000..17a49c8
--- /dev/null
+++ b/sim/avr/pgmspace.c
@@ -0,0 +1,26 @@
+/*
+ pgmspace.c - replacement for the avr library of the same name to provide
+ dummy functions
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+// this is not really ever called in the simulation
+char pgm_read_byte_near(const char* s) {
+ return s[0];
+}
diff --git a/sim/avr/pgmspace.h b/sim/avr/pgmspace.h
new file mode 100644
index 0000000..450b9f2
--- /dev/null
+++ b/sim/avr/pgmspace.h
@@ -0,0 +1,30 @@
+/*
+ pgmspace.h - replacement for the avr include of the same name to provide
+ dummy functions andd macros
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+#ifndef pgmspace_h
+#define pgmspace_h
+
+#define PSTR(s) s
+
+char pgm_read_byte_near(const char* s);
+
+#endif
diff --git a/sim/avr/sleep.h b/sim/avr/sleep.h
new file mode 100644
index 0000000..a9682b4
--- /dev/null
+++ b/sim/avr/sleep.h
@@ -0,0 +1,21 @@
+/*
+ sleep.h - dummy replacement for the avr include of the same name
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
diff --git a/sim/config.h b/sim/config.h
new file mode 100644
index 0000000..404accd
--- /dev/null
+++ b/sim/config.h
@@ -0,0 +1,117 @@
+/*
+ config.h - replacement for the include of the same name in grbl
+ to define dummy registers
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+#ifndef config_h
+#include "../config.h"
+#include
+
+
+// dummy register variables implemented in simulator.c
+extern uint8_t stepping_ddr;
+extern uint8_t stepping_port;
+extern uint8_t spindle_ddr;
+extern uint8_t spindle_port;
+extern uint8_t limit_ddr;
+extern uint8_t limit_port;
+extern uint8_t limit_int_reg;
+extern uint8_t pinout_ddr;
+extern uint8_t pinout_port;
+extern uint8_t pinout_int_reg;
+extern uint8_t coolant_flood_ddr;
+extern uint8_t coolant_flood_port;
+
+// ReDefine pin-assignments
+#undef STEPPING_DDR
+#define STEPPING_DDR stepping_ddr
+#undef STEPPING_PORT
+#define STEPPING_PORT stepping_port
+#undef X_STEP_BIT
+#define X_STEP_BIT 2 // Uno Digital Pin 2
+#undef Y_STEP_BIT
+#define Y_STEP_BIT 3 // Uno Digital Pin 3
+#undef Z_STEP_BIT
+#define Z_STEP_BIT 4 // Uno Digital Pin 4
+#undef X_DIRECTION_BIT
+#define X_DIRECTION_BIT 5 // Uno Digital Pin 5
+#undef Y_DIRECTION_BIT
+#define Y_DIRECTION_BIT 6 // Uno Digital Pin 6
+#undef Z_DIRECTION_BIT
+#define Z_DIRECTION_BIT 7 // Uno Digital Pin 7
+
+#undef STEPPERS_DISABLE_DDR
+#define STEPPERS_DISABLE_DDR stepping_ddr
+#undef STEPPERS_DISABLE_PORT
+#define STEPPERS_DISABLE_PORT stepping_port
+#undef STEPPERS_DISABLE_BIT
+#define STEPPERS_DISABLE_BIT 0 // Uno Digital Pin 8
+
+#undef LIMIT_DDR
+#define LIMIT_DDR limit_ddr
+#undef LIMIT_PORT
+#define LIMIT_PORT limit_port
+#undef LIMIT_PIN
+#define LIMIT_PIN limit_port
+#undef X_LIMIT_BIT
+#define X_LIMIT_BIT 1 // Uno Digital Pin 9
+#undef Y_LIMIT_BIT
+#define Y_LIMIT_BIT 2 // Uno Digital Pin 10
+#undef Z_LIMIT_BIT
+#define Z_LIMIT_BIT 3 // Uno Digital Pin 11
+#undef LIMIT_INT
+#define LIMIT_INT 0
+#undef LIMIT_PCMSK
+#define LIMIT_PCMSK limit_int_reg
+
+#undef SPINDLE_ENABLE_DDR
+#define SPINDLE_ENABLE_DDR spindle_ddr
+#undef SPINDLE_ENABLE_PORT
+#define SPINDLE_ENABLE_PORT spindle_port
+#undef SPINDLE_ENABLE_BIT
+#define SPINDLE_ENABLE_BIT 4 // Uno Digital Pin 12
+
+#undef SPINDLE_DIRECTION_DDR
+#define SPINDLE_DIRECTION_DDR spindle_ddr
+#undef SPINDLE_DIRECTION_PORT
+#define SPINDLE_DIRECTION_PORT spindle_port
+#undef SPINDLE_DIRECTION_BIT
+#define SPINDLE_DIRECTION_BIT 5 // Uno Digital Pin 13
+
+#undef PINOUT_DDR
+#define PINOUT_DDR pinout_ddr
+#undef PINOUT_PORT
+#define PINOUT_PORT pinout_port
+#undef PINOUT_PIN
+#define PINOUT_PIN pinout_port
+#undef PINOUT_PCMSK
+#define PINOUT_PCMSK pinout_int_reg
+#undef PINOUT_INT
+#define PINOUT_INT 0
+
+#undef COOLANT_FLOOD_DDR
+#define COOLANT_FLOOD_DDR coolant_flood_ddr
+#undef COOLANT_FLOOD_PORT
+#define COOLANT_FLOOD_PORT coolant_flood_port
+#undef COOLANT_FLOOD_BIT
+#define COOLANT_FLOOD_BIT 0
+
+
+#endif
diff --git a/sim/eeprom.c b/sim/eeprom.c
new file mode 100644
index 0000000..5c3c3f3
--- /dev/null
+++ b/sim/eeprom.c
@@ -0,0 +1,38 @@
+/*
+ eeprom.c - replacement for the avr library of the same name to provide
+ dummy functions
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+// These are never called in the simulator
+unsigned char eeprom_get_char( unsigned int addr ) {
+ return 0;
+}
+
+void eeprom_put_char( unsigned int addr, unsigned char new_value ) {
+}
+
+void memcpy_to_eeprom_with_checksum(unsigned int destination, char *source, unsigned int size) {
+}
+
+int memcpy_from_eeprom_with_checksum(char *destination, unsigned int source, unsigned int size) {
+ return 0;
+}
+
+// end of file
diff --git a/sim/gnuplot.plt b/sim/gnuplot.plt
new file mode 100644
index 0000000..c0b712c
--- /dev/null
+++ b/sim/gnuplot.plt
@@ -0,0 +1,6 @@
+t_= NaN
+x_= NaN
+y_= NaN
+z_= NaN
+
+splot 'HelloWorldSteps.dat' u 2:3:4:(dx= $2-x_,x_=$2,dy=$3-y_,y_=$3,dz=$4-z_,z_=$4,dt=$1-t_,t_=$1,sqrt(dx*dx+dy*dy+dz*dz)/(dt<0.01?0.01:dt)) title "Simulated steps with speed dependent coloring" with lines palette
\ No newline at end of file
diff --git a/sim/main.c b/sim/main.c
new file mode 100644
index 0000000..d157ac3
--- /dev/null
+++ b/sim/main.c
@@ -0,0 +1,88 @@
+/*
+ main.c - main grbl simulator program
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+#include
+#include
+#include
+#include "../planner.h"
+#include "../stepper.h"
+#include "../gcode.h"
+#include "../protocol.h"
+#include "../nuts_bolts.h"
+#include "../settings.h"
+#include "../spindle_control.h"
+#include "../limits.h"
+#include "../coolant_control.h"
+#include "simulator.h"
+
+
+int main(int argc, char *argv[]) {
+ // Get the minimum time step for printing stepper values.
+ // If not given or the command line cannot be parsed to a float than
+ // step_time= 0.0; This means to not print stepper values at all
+ if(argc>1) {
+ argv++;
+ step_time= atof(*argv);
+ }
+
+ // Setup output file handles. Block info goes to stdout. Stepper values go to stderr.
+ block_out_file= stdout;
+ step_out_file= stderr;
+ // Make sure the output streams are flushed immediately.
+ // This is important when using the simulator inside another application in parallel
+ // to the real grbl.
+ // Theoretically flushing could be limited to complete lines. Unfortunately Windows
+ // does not know line buffered streams. So for now we stick to flushing every character.
+ //setvbuf(stdout, NULL, _IONBF, 1);
+ //setvbuf(stderr, NULL, _IONBF, 1);
+
+ st_init(); // Setup stepper pins and interrupt timers
+ memset(&sys, 0, sizeof(sys)); // Clear all system variables
+ //settings_reset(); TODO: implement read_settings from eeprom
+ settings_init();
+ protocol_init(); // Clear incoming line data
+ //printf("plan_init():\n");
+ plan_init(); // Clear block buffer and planner variables
+ //printf("gc_init():\n");
+ gc_init(); // Set g-code parser to default state
+ //printf("spindle_init():\n");
+ spindle_init();
+ //printf("limits_init():\n");
+ limits_init();
+ //printf("coolant_init():\n");
+ coolant_init();
+ //printf("st_reset():\n");
+ st_reset(); // Clear stepper subsystem variables.
+ sys.auto_start = true; // runtime commands are not processed. We start to simulate immediately
+
+ // Main loop of command processing until EOF is encountered
+ //printf("protocol_process():\n");
+ protocol_process();
+
+ // flush the block buffer and print stepper info on any pending blocks
+ plan_synchronize();
+
+ // Graceful exit
+ fclose(block_out_file);
+ fclose(step_out_file);
+
+ exit(EXIT_SUCCESS);
+}
diff --git a/sim/planner_inject_accessors.c b/sim/planner_inject_accessors.c
new file mode 100644
index 0000000..7a04bcc
--- /dev/null
+++ b/sim/planner_inject_accessors.c
@@ -0,0 +1,10 @@
+#include "../planner.h"
+
+static block_t block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instructions
+block_t *get_block_buffer() { return block_buffer; }
+
+static volatile uint8_t block_buffer_head; // Index of the next block to be pushed
+uint8_t get_block_buffer_head() { return block_buffer_head; }
+
+static volatile uint8_t block_buffer_tail; // Index of the next block to be pushed
+uint8_t get_block_buffer_tail() { return block_buffer_tail; }
diff --git a/sim/rename_execute_runtime.h b/sim/rename_execute_runtime.h
new file mode 100644
index 0000000..03ec40f
--- /dev/null
+++ b/sim/rename_execute_runtime.h
@@ -0,0 +1,2 @@
+#define protocol_execute_runtime orig_protocol_execute_runtime
+//void __attribute__((weak)) protocol_execute_runtime(void);
diff --git a/sim/runtime.c b/sim/runtime.c
new file mode 100644
index 0000000..1b38d6a
--- /dev/null
+++ b/sim/runtime.c
@@ -0,0 +1,38 @@
+/*
+ runtime.c - replacement for the modul of the same name in grbl
+ Run time commands are not processed in the simulator.
+ Instead, the execute_runtime() is used as a hook to handle stepper simulation
+ and printing of simulation results.
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+#include "simulator.h"
+#include
+
+void orig_protocol_execute_runtime(void);
+
+// replacement for original execute_runtime as a hook to print blocks as they are generated
+// and to control simulation of buffered blocks
+void protocol_execute_runtime(void) {
+ orig_protocol_execute_runtime();
+ //printf("printBlock():\n");
+ printBlock();
+ //printf("handle_buffer():\n");
+ handle_buffer();
+}
diff --git a/sim/serial.c b/sim/serial.c
new file mode 100644
index 0000000..6fdbdd5
--- /dev/null
+++ b/sim/serial.c
@@ -0,0 +1,53 @@
+/*
+ serial.c - replacement for the modul of the same name in grbl
+ Make sure the simulator reads from stdin and writes to stdout.
+ Also print info about the last buffered block.
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+#include "../serial.h"
+#include
+#include "simulator.h"
+#include
+
+void serial_write(uint8_t data) {
+ printBlock();
+ if(print_comment && data!='\n' && data!='\r') {
+ fprintf(block_out_file, "# ");
+ print_comment= 0;
+ }
+ if(data=='\n' || data=='\r')
+ print_comment= 1;
+
+ fprintf(block_out_file, "%c", data);
+
+ // Indicate the end of processing a command. See simulator.c for details
+ runtime_second_call= 0;
+}
+
+
+uint8_t serial_read() {
+ int c;
+ if((c = fgetc(stdin)) != EOF) {
+ serial_write(c);
+ return c;
+ }
+
+ return SERIAL_NO_DATA;
+}
diff --git a/sim/sim.bat b/sim/sim.bat
new file mode 100644
index 0000000..3c993c9
--- /dev/null
+++ b/sim/sim.bat
@@ -0,0 +1 @@
+grbl_sim.exe 0.01 HelloWorld.dat 2> HelloWorldSteps.dat
\ No newline at end of file
diff --git a/sim/simulator.c b/sim/simulator.c
new file mode 100644
index 0000000..73c1405
--- /dev/null
+++ b/sim/simulator.c
@@ -0,0 +1,217 @@
+/*
+ simulator.c - functions to simulate how the buffer is emptied and the
+ stepper interrupt is called
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+#include
+#include
+#include
+#include
+#include "../stepper.h"
+#include "../planner.h"
+#include "../nuts_bolts.h"
+#include "simulator.h"
+
+// This variable is needed to determine if execute_runtime() is called in a loop
+// waiting for the buffer to empty, as in plan_synchronize()
+// it is reset in serial_write() because this is certainly called at the end of
+// every command processing
+int runtime_second_call= 0;
+
+
+// Current time of the stepper simulation
+double sim_time= 0.0;
+// Next time the status of stepper values should be printed
+double next_print_time= 0.0;
+// Minimum time step for printing stepper values. Given by user via command line
+double step_time= 0.0;
+
+// global system variable structure for position etc.
+system_t sys;
+int block_position[]= {0,0,0};
+uint8_t print_comment= 1;
+uint8_t end_of_block= 1;
+uint32_t block_number= 0;
+
+// Output file handles set by main program
+FILE *block_out_file;
+FILE *step_out_file;
+
+// dummy port variables
+uint8_t stepping_ddr;
+uint8_t stepping_port;
+uint8_t spindle_ddr;
+uint8_t spindle_port;
+uint8_t limit_ddr;
+uint8_t limit_port;
+uint8_t limit_int_reg;
+uint8_t pinout_ddr;
+uint8_t pinout_port;
+uint8_t pinout_int_reg;
+uint8_t coolant_flood_ddr;
+uint8_t coolant_flood_port;
+
+extern block_t block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instructions
+extern uint8_t block_buffer_head; // Index of the next block to be pushed
+extern uint8_t block_buffer_tail; // Index of the block to process now
+
+
+// Stub of the timer interrupt function from stepper.c
+void interrupt_TIMER2_COMPA_vect();
+
+// Call the stepper interrupt until one block is finished
+void sim_stepper() {
+ //printf("sim_stepper()\n");
+ block_t *current_block= plan_get_current_block();
+
+ // If the block buffer is empty, call the stepper interrupt one last time
+ // to let it handle sys.cycle_start etc.
+ if(current_block==NULL) {
+ interrupt_TIMER2_COMPA_vect();
+ return;
+ }
+
+ while(current_block==plan_get_current_block()) {
+ sim_time+= get_step_time();
+ interrupt_TIMER2_COMPA_vect();
+
+ // Check to see if we should print some info
+ if(step_time>0.0) {
+ if(sim_time>=next_print_time) {
+ if(end_of_block) {
+ end_of_block= 0;
+ fprintf(step_out_file, "# block number %d\n", block_number);
+ }
+ fprintf(step_out_file, "%20.15f, %d, %d, %d\n", sim_time, sys.position[X_AXIS], sys.position[Y_AXIS], sys.position[Z_AXIS]);
+
+ // Make sure the simulation time doesn't get ahead of next_print_time
+ while(next_print_time0.0) {
+ fprintf(step_out_file, "%20.15f, %d, %d, %d\n", sim_time, sys.position[X_AXIS], sys.position[Y_AXIS], sys.position[Z_AXIS]);
+ end_of_block= 1;
+ block_number++;
+ }
+}
+
+// Returns the index of the previous block in the ring buffer
+uint8_t prev_block_index(uint8_t block_index)
+{
+ if (block_index == 0) { block_index = BLOCK_BUFFER_SIZE; }
+ block_index--;
+ return(block_index);
+}
+
+block_t *get_block_buffer();
+uint8_t get_block_buffer_head();
+uint8_t get_block_buffer_tail();
+
+block_t *plan_get_recent_block() {
+ if (get_block_buffer_head() == get_block_buffer_tail()) { return(NULL); }
+ return(get_block_buffer()+prev_block_index(get_block_buffer_head()));
+}
+
+
+// Print information about the most recently inserted block
+// but only once!
+void printBlock() {
+ block_t *b;
+ static block_t *last_block;
+
+ //printf("printBlock()\n");
+
+ b= plan_get_recent_block();
+ if(b!=last_block && b!=NULL) {
+ //fprintf(block_out_file,"%s\n", line);
+ //fprintf(block_out_file," block: ");
+ if(b->direction_bits & (1<steps_x;
+ else block_position[0]+= b->steps_x;
+ fprintf(block_out_file,"%d, ", block_position[0]);
+
+ if(b->direction_bits & (1<steps_y;
+ else block_position[1]+= b->steps_y;
+ fprintf(block_out_file,"%d, ", block_position[1]);
+
+ if(b->direction_bits & (1<steps_z;
+ else block_position[2]+= b->steps_z;
+ fprintf(block_out_file,"%d, ", block_position[2]);
+
+ fprintf(block_out_file,"%f", b->entry_speed_sqr);
+ fprintf(block_out_file,"\n");
+
+ last_block= b;
+ }
+}
+
+// The simulator assumes that grbl is fast enough to keep the buffer full.
+// Thus, the stepper interrupt is only called when the buffer is full and then only to
+// finish one block.
+// Only when plan_synchronize() wait for the whole buffer to clear, the stepper interrupt
+// to finish all pending moves.
+void handle_buffer() {
+ // runtime_second_call is reset by serial_write() after every command.
+ // Only when execute_runtime() is called repeatedly by plan_synchronize()
+ // runtime_second_call will be incremented above 2
+ //printf("handle_buffer()\n");
+ if(plan_check_full_buffer() || runtime_second_call>2) {
+ sim_stepper(step_out_file);
+ } else {
+ runtime_second_call++;
+ }
+}
+
+double get_step_time() {
+/* code for the old stepper algorithm
+ uint16_t ceiling;
+ uint16_t prescaler;
+ uint32_t actual_cycles;
+ uint8_t invalid_prescaler= 0;
+
+ prescaler= ((TCCR1B>>CS10) & 0x07) - 1;
+ ceiling= OCR1A;
+
+ switch(prescaler) {
+ case 0:
+ actual_cycles= ceiling;
+ break;
+ case 1:
+ actual_cycles= ceiling * 8L;
+ break;
+ case 2:
+ actual_cycles = ceiling * 64L;
+ break;
+ case 3:
+ actual_cycles = ceiling * 256L;
+ break;
+ case 4:
+ actual_cycles = ceiling * 1024L;
+ break;
+ default:
+ invalid_prescaler= 1;
+ }
+
+ if(invalid_prescaler) return 12345.0;
+ else return (double)actual_cycles/F_CPU;*/
+ return (double)((ocr2a+1)*8)/(double)(F_CPU);
+}
diff --git a/sim/simulator.h b/sim/simulator.h
new file mode 100644
index 0000000..85d2c36
--- /dev/null
+++ b/sim/simulator.h
@@ -0,0 +1,60 @@
+/*
+ simulator.h - functions to simulate how the buffer is emptied and the
+ stepper interrupt is called
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+#ifndef simulator_h
+#define simulator_h
+
+#include
+#include "../nuts_bolts.h"
+
+// Output file handles
+extern FILE *block_out_file;
+extern FILE *step_out_file;
+
+// This variable is needed to determine if execute_runtime() is called in a loop
+// waiting for the buffer to empty, as in plan_synchronize()
+extern int runtime_second_call;
+
+// Minimum time step for printing stepper values. Given by user via command line
+extern double step_time;
+
+// global system variable structure for position etc.
+extern system_t sys;
+extern uint8_t print_comment;
+
+
+// Call the stepper interrupt until one block is finished
+void sim_stepper();
+
+// Check if buffer is full or if plan_synchronize() wants to clear the buffer
+void handle_buffer();
+
+// Print information about the most recently inserted block
+void printBlock();
+
+// Calculate the time between stepper interrupt calls from TCCR1B and OCR1A AVR registers
+// which are set in config_step_timer in stepper.c
+// This reconstructs the stepper-internal value of variable st.cycles_per_step_event
+// The reconstruction is done to truely decouple the simulator from the actual grbl code
+double get_step_time();
+
+#endif
diff --git a/sim/util/delay.c b/sim/util/delay.c
new file mode 100644
index 0000000..6fc7d18
--- /dev/null
+++ b/sim/util/delay.c
@@ -0,0 +1,27 @@
+/*
+ delay.c - replacement for the avr library of the same name to provide
+ dummy functions
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+// TODO: the simulation time should reflect when grbl dwells
+// Maybe this becomes a no-issue when dwell is executed in a buffered block
+void _delay_ms(int i) {}
+
+void _delay_us(int i) {}
diff --git a/sim/util/delay.h b/sim/util/delay.h
new file mode 100644
index 0000000..818ddc9
--- /dev/null
+++ b/sim/util/delay.h
@@ -0,0 +1,29 @@
+/*
+ delay.h - replacement for the avr include of the same name to provide
+ dummy functions
+
+ Part of Grbl Simulator
+
+ Copyright (c) 2012 Jens Geisler
+
+ Grbl is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Grbl is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Grbl. If not, see .
+*/
+
+#ifndef delay_h
+#define delay_h
+
+void _delay_ms(int i);
+void _delay_us(int i);
+
+#endif
diff --git a/sim/util/floatunsisf.c b/sim/util/floatunsisf.c
new file mode 100644
index 0000000..686bdba
--- /dev/null
+++ b/sim/util/floatunsisf.c
@@ -0,0 +1,3 @@
+float __floatunsisf (unsigned long v) {
+ return v;
+}
\ No newline at end of file