-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Knuettel
committed
Sep 29, 2016
1 parent
c39ff6e
commit f914090
Showing
2 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/python3 | ||
|
||
""" | ||
**py_register_machine2.core.interrupts**: Basic module to provide Interrupts. | ||
""" | ||
|
||
|
||
class Interrupt(object): | ||
""" | ||
.. _Interrupt: | ||
The Base Class for Interrupts. | ||
If ``Interrupt.interrupt`` is invoked this will invoke ``Processor.interrupt`` and provide the address of the | ||
Interrupt. | ||
This will allow one to place an ISR (Interrupt Service Routine) at this address. | ||
""" | ||
def __init__(self, address, processor): | ||
self.enable = False | ||
self.address = address | ||
self.processor = processor | ||
self.processor.interrupts.append(self) | ||
def interrupt(self): | ||
""" | ||
Will interrupt the Processor, if the Interrupt is enabled. | ||
""" | ||
if(self.enable): | ||
self.processor.interrupt(self.address) | ||
|
||
|
||
class Counter(Interrupt): | ||
""" | ||
.. _Counter: | ||
A Counter/Timer implementation. | ||
The ``__init__`` method will inject an ``on_cycle_callback`` into the Processor. | ||
This callback will increment the internal counter variable by one. | ||
If the internal counter reaches a predefined value the ``interrupt`` method will be invoked. | ||
""" | ||
|
||
def __init__(self, address, processor, overflow_size): | ||
Interrupt.__init__(self, address, processor) | ||
self.processor.on_cycle_callbacks.append(self.increment_counter) | ||
self.counter = 0 | ||
self.overflow = overflow_size | ||
|
||
def increment_counter(self): | ||
self.counter += 1 | ||
if(self.counter >= self.overflow): | ||
self.counter = 0 | ||
self.interrupt() | ||
|
||
class Autoreset(Interrupt): | ||
""" | ||
.. _Autoreset: | ||
A really rude form of the Watchdog. | ||
This Interrupt will force the Processor to jump to offset ``0``. | ||
""" | ||
|
||
def __init__(self, processor, overflow_size): | ||
Interrupt.__init__(self, 0, processor) | ||
self.counter = 0 | ||
self.overflow = overflow_size | ||
self.processor.on_cycle_callbacks.append(self.increment_counter) | ||
def increment_counter(self): | ||
self.counter += 1 | ||
if(self.counter >= self.overflow): | ||
self.counter = 0 | ||
self.interrupt() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters