Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 20 additions & 34 deletions memory-voting-prototype/memory_correction.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,48 @@
#include "memory_correction.h"
#include "environment.h"

#ifdef SCRUBBING_ON_MSP430
char agreements[NUM_CODE_COPIES];
#endif

// Performs a bit-level correction of the byte at loc in data_copies
__attribute__ ((always_inline))
char correct_bits(char** data_copies, int num_copies, int32_t loc) {

int i, j, k;
int i, j;

#ifndef SCRUBBING_ON_MSP430
char agreements[num_copies];
#endif
char one_votes;
char zero_votes;
char current_bit = 1;

char most_agreements;
char most_agreed_value;

char corrections = 0;

for(i = 0; i < BITS_IN_BYTE; i++) {

// Reset tracked agreements
for(k = 0; k < num_copies; k++) agreements[k] = 0;
most_agreements = 0;
most_agreed_value = 0;

char current_bit = 1 << i;
// Reset variables
one_votes = 0;
zero_votes = 0;

// Search forward for data copies that agree
// and keep track of how many are found
for(k = 0; k < num_copies; k++) {
for(j = k + 1; j < num_copies; j++) {
if((DATA_READ(data_copies, j, loc) & current_bit) == (DATA_READ(data_copies, k, loc) & current_bit)) {
agreements[k]++;
}
}
// Count zeros and ones votes
for(j = 0; j < num_copies; j++) {
if((DATA_READ(data_copies, j, loc) & current_bit) != 0) one_votes++;
else zero_votes++;
}

// Find the most agreed value
for(k = 0; k < num_copies; k++) {
if(agreements[k] > most_agreements) {
most_agreements = agreements[k];
most_agreed_value = DATA_READ(data_copies, k, loc) & current_bit;
}
}
if(one_votes > zero_votes) most_agreed_value = current_bit;
else most_agreed_value = 0;

// Correct data copies to match
for(k = 0; k < num_copies; k++) {
if((DATA_READ(data_copies, k, loc) & current_bit) != most_agreed_value) {
UNLOCK_MEMORY_SEGMENT(k);
DATA_WRITE(data_copies, k, loc, DATA_READ(data_copies, k, loc) ^ current_bit);
LOCK_MEMORY_SEGMENT(k);
for(j = 0; j < num_copies; j++) {
if((DATA_READ(data_copies, j, loc) & current_bit) != most_agreed_value) {
UNLOCK_MEMORY_SEGMENT(j);
DATA_WRITE(data_copies, j, loc, DATA_READ(data_copies, j, loc) ^ current_bit);
LOCK_MEMORY_SEGMENT(j);
corrections++;
}
}

current_bit <<= 1;

}

return corrections;
Expand Down
7 changes: 0 additions & 7 deletions msp430/environment.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#ifndef ENVIRONMENT_H
#define ENVIRONMENT_H

/*
* Environment Settings
*/

// Defines the size of the static agreements list
#define NUM_CODE_COPIES 3

/*
* MSP430 Microcontroller environment-specific code
*/
Expand Down