examples: Extends i2ctest to include a second TWI driver

Adds a commonly used alternative to the Atmel TWI driver. This
particular driver is choosen because it covers a non-interrupt
driven approach to using the TWI module.
Changes to allow for free run in core between cycle timers
 with per interval limiting.

Average cycle times drop by about upwards of 50-60+ cycles per emulated cycle,
	dependant on usage.

sim_avr.h: struct avr_t changed.
	added members run_cycle_count and run_cycle_limit.
	run_cycle_count is number of cycles till next cycle timer.
	run_cycle_limit is maximum number of cycles to run per interval.

sim_core.c: avr_run_one
	* run_one_again label added at top.
	* clause added at end which loops to run_one_again given that the core
		is still in a cpu_Running state, run_cycle_count is greater than
		cycles, and no interrups are pending.

sim_cycle_timers.c:
	* static avr_cycle_timer_return_sleep_run_cycles_limited() added.
		run_cycle_count is bounded to run_cycle_limit.
		returns sleep count unbounded, preserving original behavior.

	* static avr_cycle_timer_reset_sleep_run_cycles_limited() added.
		sets new run_cycle_count based on present list of cycle timers.

	* avr_cycle_timer_reset() changed.
		run_cycle_count and run_cycle_limit is set to default values.

	* avr_cycle_timer_register() changed.
	* avr_cycle_timer_cancel() changed.
	* avr_cycle_timer_process() changed.
		call the relevant function to set/maintain run_cycle_count.

	modified:   simavr/sim/sim_avr.c
	modified:   simavr/sim/sim_avr.h
	modified:   simavr/sim/sim_cycle_timers.c
fifo: prevent potential misuse of _get_write_size()

Using the number obtained from _get_write_size() and writing that many
items to the fifo yields to _isempty() becoming true and not to
_isfull() becoming true.

I got caught assuming the latter.

To avoid thinking about edge cases and prevent potential misuse of
_get_write_size(), I suggest that it returns one unit less, thus, a
number between 0 and (fifo_size-1).  E.g., if there is only one place
left in the fifo, i.e., _isfull() is true, it then returns 0, preventing
us to fill that place and get into a wrap overflow situation.

(Michel: Thanks for the great architecture you laid out and the great
code.)
Merge pull request #88 from anthony-morel/master

fifo: prevent potential misuse of _get_write_size()
sim_hex: Only decrement maxlen in read_hex_string when a byte is added to the array

Without this fix, the output to the buffer would be limited to maxlen/2 bytes
instead of maxlen. It is obvious from the implementation that the latter
behavior is the expected one, so the function has been altered to reflect that.
Fixes in avr_timer_reconfigure after refactoring

Several fixes after fcce7868a2fe2ef028b7f6c1741a12380b82a9cc:
- Remove zeroing of mode in which erased mode
  seleced in avr_timer_write
- Fix wrong brackets in ?: in avr_timer_wgm_pwm mode
Enhance emulation of PWM mode

- Clear/set output pin on TOP when OCRnX is in clear/set mode,
  previously it was changed only on TCNT=OCRnX
- Call PWM irq for OCRnB even when TOP is OCRnA
Merge pull request #82 from dougszumski/i2c_test_extension

examples: Extends i2ctest to include a second TWI driver
Merge pull request #83 from bsekisser/master-core-run-many-limited

Changes to allow for free run in core between cycle timers
Merge pull request #92 from cyrozap/master

Fix the issue where Intel HEX files with hex strings longer than 32 bytes would be truncated
Merge pull request #94 from hedrok/master

Timer simulation fixes and improvements
build: re-accelerated the build

Simavr used to build pretty quick, and it's been crawling for a while
now, turns out some of the enhancements we added to the SIMAVR path
detection were being evaluated a LOT.
So went to the makefile and made sure most of the static variables are
evaluated once only.

Signed-off-by: Michel Pollet <buserror@gmail.com>
build: fix build of libsimavr.so

Was being linked with itself. Newer gcc are not amused.

Signed-off-by: Michel Pollet <buserror@gmail.com>
tests: Fix makefile

Was using ${OBJ} before it was declared.

Signed-off-by: Michel Pollet <buserror@gmail.com>
cores: AVR_IOPORT_DECLARE

convert port sructure declarations to macro AVR_IOPORT_DECLARE.

	modified:   simavr/cores/sim_90usb162.c
	modified:   simavr/cores/sim_mega128.c
	modified:   simavr/cores/sim_mega1280.c
	modified:   simavr/cores/sim_mega1281.c
	modified:   simavr/cores/sim_mega128rfa1.c
	modified:   simavr/cores/sim_mega128rfr2.c
	modified:   simavr/cores/sim_mega169.c
	modified:   simavr/cores/sim_mega2560.c
	modified:   simavr/cores/sim_megax.h
	modified:   simavr/cores/sim_tiny2313.c
	modified:   simavr/sim/avr_ioport.h
cores: add spi declaration

convert spi structure definitions to AVR_SPI_DECLARE

	modified:   simavr/cores/sim_90usb162.c
	modified:   simavr/cores/sim_mega128.c
	modified:   simavr/cores/sim_mega1280.c
	modified:   simavr/cores/sim_mega1281.c
	modified:   simavr/cores/sim_mega128rfr2.c
	modified:   simavr/cores/sim_mega169.c
	modified:   simavr/cores/sim_mega2560.c
	modified:   simavr/cores/sim_megax.h
	modified:   simavr/cores/sim_megax4.h
	modified:   simavr/cores/sim_megax8.h
	modified:   simavr/cores/sim_megaxm1.h
	modified:   simavr/sim/avr_spi.h
Merge pull request #99 from bsekisser/bsekisser-cores-ioport-declare

cores: AVR_IOPORT_DECLARE
Merge pull request #100 from bsekisser/bsekisser-cores-spi-declare

cores: add spi declaration
interrupts: modify handling of interrupt state

Interrupt state edge detection and wait states combined into multi
	function variable, enabling the removal of edge detection
	code in the run loop and simplifying need to service interrupts
	by placing the burden on code directly influencing the interrupt
	handling state.

modified:   simavr/sim/sim_avr.c
	edge detection code removed from both run loops.
	raw (non-gdb) loop does precheck of interrupt state, while not
	necessary, potentially saving a few cycles.

modified:   simavr/sim/sim_avr.h
	uint8_t i_shadow changed to int8_t interrupt_state.

modified:   simavr/sim/sim_core.c
	flag changes which may impact interrupt state are routed
		through avr_sreg_set().
	multi-cycle loop simplified to check avr->interrupt_state.

modified:   simavr/sim/sim_core.h
	static inline avr_sreg_set() - handles changes to global
		interrupt state and ensures wait states if I
		flag changes from 0 -> 1.  superfluous 1 -> 1
		states are ignored, should they occur. and
		disabling interrupts clears avr->interrupt_state.
	flag changes from SET_SREG_FROM routed to avr_sreg_set();

modified:   simavr/sim/sim_interrupts.c
	avr_interrupt_reset()
		interrupt_state cleared during reset.
	avr_raise_intrrupt()
		check interrupts enabled and no pending
		interrupt_state before marking pending interrupt state.
	avr_service_interrupts()
		servicing code changed to tick pending wait state then
		mark for any pending interrupts or set to zero
		if none waiting.
		on interrupt, direct interrupt state change to
			avr_sreg_set();

modified:   simavr/sim/sim_interrupts.h
	remove pending_wait.

modified:   tests/tests.c
	interrupt edge dectection code removed from test run loop.
	no further modifications required.

	modified:   simavr/sim/sim_avr.c
	modified:   simavr/sim/sim_avr.h
	modified:   simavr/sim/sim_core.c
	modified:   simavr/sim/sim_core.h
	modified:   simavr/sim/sim_interrupts.c
	modified:   simavr/sim/sim_interrupts.h
	modified:   tests/tests.c
Merge pull request #95 from bsekisser/master-core-run-many-limited-interrupt-state

interrupts: modify handling of interrupt state