Skip to content

Commit

Permalink
Misc. dirty fixes
Browse files Browse the repository at this point in the history
- Fix for buserror#461

  Borrowed from https://stuvel.eu/post/2021-04-27-atmega328pb-on-gcc/

- Add easy to understand usage instructions

- Add `ArduinoUniqueID` example and binaries
  • Loading branch information
kholia committed Jan 14, 2022
1 parent ea4c450 commit e27c7d4
Show file tree
Hide file tree
Showing 6 changed files with 1,509 additions and 10 deletions.
Binary file added ArduinoUniqueID.ino.elf
Binary file not shown.
117 changes: 117 additions & 0 deletions ArduinoUniqueID.ino.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
:100000000C9435000C945D000C945D000C945D0024
:100010000C945D000C945D000C945D000C945D00EC
:100020000C945D000C945D000C945D000C945D00DC
:100030000C945D000C945D000C945D000C945D00CC
:100040000C94F6010C945D000C94C4010C949E0178
:100050000C945D000C945D000C945D000C945D00AC
:100060000C945D000C945D00510311241FBECFEF72
:10007000D8E0DEBFCDBF11E0A0E0B1E0ECE0F7E0FA
:1000800002C005900D92AC32B107D9F721E0ACE285
:10009000B1E001C01D92A23DB207E1F710E0C5E357
:1000A000D0E004C02197FE010E947E03C433D10733
:1000B000C9F70E9440020C9484030C940000AF9294
:1000C000BF92CF92DF92EF92FF920F931F93CF9345
:1000D000DF936C017B018B01040F151FEB015E01A7
:1000E000AE18BF08C017D10759F06991D601ED913C
:1000F000FC910190F081E02DC6010995892B79F7DB
:10010000C501DF91CF911F910F91FF90EF90DF908C
:10011000CF90BF90AF900895FC01538D448D252F53
:1001200030E0842F90E0821B930B541710F0CF9691
:10013000089501970895FC01918D828D981761F0C3
:10014000A28DAE0FBF2FB11D5D968C91928D9F5FDA
:100150009F73928F90E008958FEF9FEF0895FC01B9
:10016000918D828D981731F0828DE80FF11D858D6C
:1001700090E008958FEF9FEF0895FC01918D228DFF
:10018000892F90E0805C9F4F821B91098F73992784
:10019000089585E391E00E94BD0021E0892B09F4D8
:1001A00020E0822F089580E090E0892B29F00E94C2
:1001B000C90081110C9400000895FC01A48DA80FC2
:1001C000B92FB11DA35ABF4F2C91848D90E0019699
:1001D0008F739927848FA689B7892C93A089B189B9
:1001E0008C91837080648C93938D848D981306C05A
:1001F0000288F389E02D80818F7D80830895EF92BE
:10020000FF920F931F93CF93DF93EC0181E0888FD0
:100210009B8D8C8D98131AC0E889F989808185FFA0
:1002200015C09FB7F894EE89FF896083E889F98942
:1002300080818370806480839FBF81E090E0DF9144
:10024000CF911F910F91FF90EF900895F62E0B8D97
:1002500010E00F5F1F4F0F731127E02E8C8D8E1152
:100260000CC00FB607FCFACFE889F989808185FFB9
:10027000F5CFCE010E94DD00F1CFEB8DEC0FFD2F0D
:10028000F11DE35AFF4FF0829FB7F8940B8FEA8974
:10029000FB8980818062CFCFCF93DF93EC01888D83
:1002A0008823B9F0AA89BB89E889F9898C9185FDF1
:1002B00003C0808186FD0DC00FB607FCF7CF8C917F
:1002C00085FFF2CF808185FFEDCFCE010E94DD005A
:1002D000E9CFDF91CF9108953FB7F8948091310134
:1002E00090913201A0913301B091340126B5A89BC1
:1002F00005C02F3F19F00196A11DB11D3FBFBA2FB8
:10030000A92F982F8827BC01CD01620F711D811D77
:10031000911D42E0660F771F881F991F4A95D1F7FC
:100320000895FC0101900020E9F73197AF01481BC7
:10033000590BBC0185E391E00C945F001F920F9272
:100340000FB60F9211242F933F934F935F936F93A8
:100350007F938F939F93AF93BF93EF93FF9385E327
:1003600091E00E94DD00FF91EF91BF91AF919F91CD
:100370008F917F916F915F914F913F912F910F904E
:100380000FBE0F901F9018951F920F920FB60F92ED
:1003900011242F938F939F93EF93FF93E091450147
:1003A000F09146018081E0914B01F0914C0182FD7A
:1003B0001BC0908180914E018F5F8F7320914F0100
:1003C000821741F0E0914E01F0E0EB5CFE4F958F1B
:1003D00080934E01FF91EF919F918F912F910F90FC
:1003E0000FBE0F901F9018958081F4CF1F920F922F
:1003F0000FB60F9211242F933F938F939F93AF9338
:10040000BF9380912D0190912E01A0912F01B09169
:10041000300130912C0123E0230F2D3758F5019640
:10042000A11DB11D20932C0180932D0190932E01CD
:10043000A0932F01B093300180913101909132014E
:10044000A0913301B09134010196A11DB11D80939B
:10045000310190933201A0933301B0933401BF91E5
:10046000AF919F918F913F912F910F900FBE0F9061
:100470001F90189526E8230F0296A11DB11DD2CF1B
:10048000CF93DF93CDB7DEB7A1970FB6F894DEBF59
:100490000FBECDBF789484B5826084BD84B5816081
:1004A00084BD85B5826085BD85B5816085BD80913F
:1004B0006E00816080936E001092810080918100B7
:1004C00082608093810080918100816080938100AF
:1004D000809180008160809380008091B100846071
:1004E0008093B1008091B00081608093B0008091D2
:1004F0007A00846080937A0080917A008260809391
:100500007A0080917A00816080937A0080917A00ED
:10051000806880937A001092C100E0914501F091CB
:10052000460182E08083E0914101F0914201108216
:10053000E0914301F09144018FEC808310924D01D2
:10054000E0914901F0914A0186E08083E091470102
:10055000F0914801808180618083E0914701F091B2
:100560004801808188608083E0914701F0914801D3
:10057000808180688083E0914701F091480180810B
:100580008F7D808384E191E00E9491018FE191E071
:100590000E94910131E2C32E00E010E00E946C0144
:1005A0002B013C0128EE822E23E0922EA12CB12CAF
:1005B0000E946C016419750986099709683E7340A9
:1005C00081059105A8F321E0821A9108A108B108DC
:1005D00088EE480E83E0581E611C711C8114910442
:1005E000A104B10429F782E291E00E9491018FE118
:1005F00091E00E949101F12CE12CF7013E9641E03F
:1006000050E086E0E816F10410F450E040E0E40F1A
:10061000F51FC0925700D4902FE082E191E02D1594
:1006200010F481E291E00E9491014D2D50E070E0C4
:1006300060E019A2FE01B196DB01CA018F70992713
:10064000AA27BB2794E076956795579547959A9585
:10065000D1F78A3024F5805D8293411551056105FB
:10066000710551F7309719F0CF010E9491018FEF7A
:10067000E81AF80A29E0E216F10409F0BECF8FE18A
:1006800091E00E9491010115110509F487CF0E94A4
:10069000C900882309F482CF0E9400007FCF895CC3
:1006A000DBCFE5E3F1E01382128288EE93E0A0E075
:1006B000B0E084839583A683B78384E091E091833F
:1006C000808385EC90E09587848784EC90E0978721
:1006D000868780EC90E0918B808B81EC90E0938B0F
:1006E000828B82EC90E0958B848B86EC90E0978BEC
:1006F000868B118E128E138E148E0895EE0FFF1FAF
:0C0700000590F491E02D0994F894FFCFCF
:10070C0000000000FF005F008C004C01BD009B004E
:10071C00AF003000696E2073657475702829000D68
:0C072C000A00696E206C6F6F70282900B5
:00000001FF
24 changes: 24 additions & 0 deletions ArduinoUniqueID/ArduinoUniqueID.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// https://microchipsupport.force.com/s/article/Serial-number-in-AVR---Mega-Tiny-devices

#include <avr/boot.h>

void setup()
{
Serial.begin(9600);
Serial.println("in setup()");
}

void loop()
{
delay(1000);
Serial.println("in loop()");

unsigned int UniqueIDsize = 9;
for (size_t i = 0; i < UniqueIDsize; i++)
{
byte n = boot_signature_byte_get(0x0E + i + (UniqueIDsize == 9 && i > 5 ? 1 : 0));
Serial.print(n < 16 ? "0" : "");
Serial.print(n, HEX);
}
Serial.println();
}
156 changes: 146 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
simavr - a lean and mean Atmel AVR simulator for linux
======

_simavr_ is a new AVR simulator for linux, or any platform that uses avr-gcc. It uses
_simavr_ is a new AVR simulator for linux, or any platform that uses avr-gcc. It uses
avr-gcc's own register definition to simplify creating new targets for supported AVR
devices. The core was made to be small and compact, and hackable so allow quick
prototyping of an AVR project. The AVR core is now stable for use with parts
with <= 128KB flash, and with preliminary support for the bigger parts. The
simulator loads ELF files directly, and there is even a way to specify simulation
parameters directly in the emulated code using an .elf section. You can also
devices. The core was made to be small and compact, and hackable so allow quick
prototyping of an AVR project. The AVR core is now stable for use with parts
with <= 128KB flash, and with preliminary support for the bigger parts. The
simulator loads ELF files directly, and there is even a way to specify simulation
parameters directly in the emulated code using an .elf section. You can also
load multipart HEX files.

Installation
Expand Down Expand Up @@ -60,7 +60,7 @@ Emulated Cores (very easy to add new ones!)
Extras:
-------
* fully working _gdb_ support including some pretty cool “passive modes”.
* There is also very easy support for “VCD” (Value Change Dump) that can be visualized
* There is also very easy support for “VCD” (Value Change Dump) that can be visualized
graphically as “waveforms” with tools like _gtkwave_ (see below).
* There are a few examples of real life firmwares running on simavr, including OpenGL rendering of the display…
* There is support for _Arduino_, but no IDE integration
Expand All @@ -78,7 +78,7 @@ Contributing

Patches are always welcome! Please submit your changes via Github pull requests.

VCD Support -- built in logic analyzer
VCD Support -- built in logic analyzer
-----------
_simavr_ can output most of its pins, firmware variables, interrupts and a few other
things as signals to be dumped into a file that can be plotted using gtkwave for
Expand Down Expand Up @@ -117,12 +117,12 @@ And when the file is loaded in gtkwave, you see:
![gtkwave](https://github.com/buserror-uk/simavr/raw/master/doc/img/gtkwave1.png)

You get a very precise timing breakdown of any change that you add to the trace, down
to the AVR cycle.
to the AVR cycle.

Example:
--------
_simavr_ is really made to be the center for emulating your own AVR projects, not just
a debugger, but also the emulating the peripherals you will use in your firmware, so
a debugger, but also the emulating the peripherals you will use in your firmware, so
you can test and develop offline, and now and then try it on the hardware.

You can also use _simavr_ to do test units on your shipping firmware to validate it
Expand All @@ -149,3 +149,139 @@ And this is a gtkwave trace of what the firmware is doing. You can zoom in, meas
in gtkwave, select trades to see etc.

Quite a few other examples are available!

Usage
-----

Install build dependencies:

```
sudo apt-get install libelf-dev freeglut3 freeglut3-dev gcc-avr avr-libc gcc make
```

Clone and build the latest `simavr` code:

```
git clone https://github.com/kholia/simavr.git
cd simavr
make
```

Run sample programs:

```
./simavr/run_avr -f 16000000 -m atmega328p ArduinoUniqueID.ino.elf
./simavr/run_avr -f 16000000 -m atmega328p ArduinoUniqueID.ino.hex
```

Sample run:

```
$ ./simavr/run_avr -f 16000000 -m atmega328p ArduinoUniqueID.ino.hex
Loaded 1966 .text at address 0x0
Loaded 42 .data
in setup()..
in loop()..
5D000C945D00945D00..
in loop()..
5D000C945D00945D00..
in loop()..
5D000C945D00945D00..
in loop()..
5D000C945D00945D00..
...
```

GDB usage:

```
$ ./simavr/run_avr -g -f 16000000 -m atmega328p ArduinoUniqueID.ino.elf
Loaded 1966 .text at address 0x0
Loaded 42 .data
avr_gdb_init listening on port 1234
...
$ avr-gdb ArduinoUniqueID.ino.elf
GNU gdb (GDB) 10.1.90.20210103-git
...
(gdb) target remote :1234
Remote debugging using :1234
0x00000000 in __vectors ()
(gdb) break main
(gdb) c
Continuing.
Breakpoint 1, main () at /home/.../hardware/avr/1.8.4/cores/arduino/main.cpp:35
35 init();
(gdb)
```


```
$ avr-objdump -S -j .sec1 -d -C -m avr5 ArduinoUniqueID.ino.hex
...
616: 10 f4 brcc .+4 ; 0x61c
618: f0 e0 ldi r31, 0x00 ; 0
61a: e0 e0 ldi r30, 0x00 ; 0
61c: be 01 movw r22, r28
61e: 62 5f subi r22, 0xF2 ; 242
620: 7f 4f sbci r23, 0xFF ; 255
622: e6 0f add r30, r22
624: f7 1f adc r31, r23
626: 10 93 57 00 sts 0x0057, r17 ; 0x800057 <-- try to find this ;)
62a: e4 91 lpm r30, Z
62c: 6e 2f mov r22, r30
62e: 70 e0 ldi r23, 0x00 ; 0
630: 90 e0 ldi r25, 0x00 ; 0
632: 80 e0 ldi r24, 0x00 ; 0
634: 4a e0 ldi r20, 0x0A ; 10
636: 0e 94 9e 01 call 0x33c ; 0x33c
```

Tracing patch:

```diff
diff --git a/simavr/sim/sim_core.c b/simavr/sim/sim_core.c
index 4bb2543..118afb5 100644
--- a/simavr/sim/sim_core.c
+++ b/simavr/sim/sim_core.c
@@ -1013,6 +1013,8 @@ run_one_again:
uint16_t z = avr->data[R_ZL] | (avr->data[R_ZH] << 8);
int op = opcode & 1;
STATE("lpm %s, (Z[%04x]%s)\n", avr_regname(d), z, op ? "+" : "");
+ printf("Opcode -> %x\n", opcode);
+ printf("lpm %s, (Z[%04x]%s)\n", avr_regname(d), z, op ? "+" : "");
_avr_set_r(avr, d, avr->flash[z]);
if (op) {
z++;
```

```
$ ./simavr/run_avr -m atmega328pb -f 16000000 ArduinoUniqueID8.ino.elf
Loaded 1878 .text at address 0x0
Loaded 34 .data
...
Opcode -> 91e4
lpm ZL, (Z[000e])
Opcode -> 91e4
lpm ZL, (Z[000f])
Opcode -> 91e4
lpm ZL, (Z[0010])
Opcode -> 91e4
lpm ZL, (Z[0011])
Opcode -> 91e4
lpm ZL, (Z[0012])
Opcode -> 91e4
lpm ZL, (Z[0013])
Opcode -> 91e4
lpm ZL, (Z[0015])
Opcode -> 91e4
lpm ZL, (Z[0016])
Opcode -> 91e4
lpm ZL, (Z[0017])
UniqueID: 00 0C 94 5D 00 94 5D 00 ..
...
```
Loading

0 comments on commit e27c7d4

Please sign in to comment.