REM: removed stdint.h ,  since we are using c99 when compiling.
     // perform read 
     // write the result to the provided card 
     if(!emv_readrecord(record,sfi,receivedAnswer)) {
-        if(EMV_DBGLEVEL >= 1) Dbprintf("readrecord failed");
+        if(MF_DBGLEVEL >= 1) Dbprintf("readrecord failed");
     }
     if(*(receivedAnswer+1) == 0x70){ 
         decode_ber_tlv_item(receivedAnswer+1, &inputtag);
     } 
     else
     {
-        if(EMV_DBGLEVEL >= 1) 
+        if(MF_DBGLEVEL >= 1) 
             Dbprintf("Record not found SFI=%i RECORD=%i", sfi, record); 
     }
     return;
     tlvtag inputtag; // create the tag structure
     // perform select 
     if(!emv_select(AID, AIDlen, receivedAnswer)){
-        if(EMV_DBGLEVEL >= 1) Dbprintf("AID Select failed");
+        if(MF_DBGLEVEL == 1) Dbprintf("AID Select failed");
         return; 
     }
     // write the result to the provided card 
         if(currentcard.tag_61_len !=0){
                 emv_decode_field(currentcard.tag_61, currentcard.tag_61_len, ¤tcard);}
     }
-    if(EMV_DBGLEVEL >= 2) 
+    if(MF_DBGLEVEL >= 2) 
         DbpString("SELECT AID COMPLETED");
 }
 
     tlvtag inputtag; //create the tag structure
     // perform pdol 
     if(!emv_getprocessingoptions(PDOL, PDOLlen, receivedAnswer)){
-        if(EMV_DBGLEVEL >= 1) Dbprintf("get processing options failed");
+        if(MF_DBGLEVEL >= 1) Dbprintf("get processing options failed");
         return 0; 
     }
     // write the result to the provided card 
         // store 82 and 94 tags (AIP, AFL) 
         emv_decode_field(inputtag.value, inputtag.valuelength, ¤tcard); 
     } 
-    if(EMV_DBGLEVEL >= 2) 
+    if(MF_DBGLEVEL >= 2) 
         DbpString("GET PROCESSING OPTIONS COMPLETE");
     return 1;
 }
     // tlvtag inputtag; //create the tag structure
     // perform select 
     if(!emv_getchallenge(receivedAnswer)){
-        if(EMV_DBGLEVEL >= 1) Dbprintf("get processing options failed");
+        if(MF_DBGLEVEL >= 1) Dbprintf("get processing options failed");
         return 1; 
     }
     return 0;
     // tlvtag inputtag; //create the tag structure
     // perform select 
     if(!emv_generateAC(refcontrol, cdolcommand, cdolcommandlen,receivedAnswer)){
-        if(EMV_DBGLEVEL >= 1) Dbprintf("get processing options failed");
+        if(MF_DBGLEVEL >= 1) Dbprintf("get processing options failed");
         return 1; 
     }
     if(receivedAnswer[2] == 0x77) //format 2 data field returned
     Dbhexdump(pdolcommandlen, pdolcommand,false);
 
     if(!EMVGetProcessingOptions(pdolcommand,pdolcommandlen, ¤tcard)) {
-        if(EMV_DBGLEVEL >= 1) Dbprintf("PDOL failed");
+        if(MF_DBGLEVEL >= 1) Dbprintf("PDOL failed");
         return 1; 
     }
 
         emv_generateDOL(currentcard.tag_9F38, currentcard.tag_9F38_len, ¤tcard, pdolcommand, &pdolcommandlen); 
     }
     if(EMVGetProcessingOptions(pdolcommand,pdolcommandlen, ¤tcard)) {
-        if(EMV_DBGLEVEL >= 1) Dbprintf("PDOL failed");
+        if(MF_DBGLEVEL >= 1) Dbprintf("PDOL failed");
         return 1; 
     }
     
        
     while(true) { 
         if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {
-            if(EMV_DBGLEVEL >= 1) Dbprintf("Can't select card");
+            if(MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
             break;
         }
         //selectPPSE 
         //TODO: add other card schemes like AMEX, JCB, China Unionpay etc 
         break;
     }
-    if (EMV_DBGLEVEL >= 2) DbpString("EMV TRANSACTION FINISHED");
+    if (MF_DBGLEVEL >= 2) DbpString("EMV TRANSACTION FINISHED");
         //finish up
     FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
     LEDsoff();
 
 
 #define DUMP(varname) Dbprintf("%s=", #varname);
 
-int EMV_DBGLEVEL = EMV_DBG_ALL;
 //uint8_t PCB = 0x00; //track Protocol Control Byte externally
 
 //util functions
 int emv_readrecord(uint8_t recordnumber, uint8_t sfi, void* data)
 {
     uint16_t readRecordCmd_len = 5; 
-    uint8_t readRecordCmd[readRecordCmd_len];
-    
+    uint8_t readRecordCmd[readRecordCmd_len];    
     readRecordCmd[0] = 0x00;
     readRecordCmd[1] = 0xB2;
     readRecordCmd[2] = recordnumber;
 int emv_getprocessingoptions(uint8_t* pdol, uint8_t pdol_len, void* data)
 {
     uint16_t processingCmd_len = 4 + 1 + 2 + pdol_len + 1; 
-    uint8_t processingCmd[processingCmd_len];
-    
+    uint8_t processingCmd[processingCmd_len];    
     processingCmd[0] = 0x80;
     processingCmd[1] = 0xA8;
     processingCmd[2] = 0x00;
 int emv_computecryptogram(uint8_t* UDOL, uint8_t UDOL_len, void *data)
 {
     uint16_t cryptogramCmd_len = 4 + 1 + UDOL_len + 1; 
-    uint8_t cryptogramCmd[cryptogramCmd_len];
-    
+    uint8_t cryptogramCmd[cryptogramCmd_len];   
     cryptogramCmd[0] = 0x80;
     cryptogramCmd[1] = 0x2A;
     cryptogramCmd[2] = 0x8E;
     cryptogramCmd[4] = UDOL_len;
     memcpy(&(cryptogramCmd[5]), UDOL, UDOL_len);
     cryptogramCmd[cryptogramCmd_len-1] = 0x00;
- 
     return iso14_apdu(cryptogramCmd, cryptogramCmd_len, data);
 }
 
 {
     uint16_t challengeCmd_len = 5; 
     uint8_t challengeCmd[challengeCmd_len];
-    
     challengeCmd[0] = 0x00;
     challengeCmd[1] = 0x84;
     challengeCmd[2] = 0x00;
     challengeCmd[3] = 0x00; 
-    challengeCmd[4] = 0x00;
-     
+    challengeCmd[4] = 0x00;     
     return iso14_apdu(challengeCmd, challengeCmd_len, data);
 }
 
 {
     uint16_t loopbackCmd_len = 4 + 1 + transData_len + 1; 
     uint8_t loopbackCmd[loopbackCmd_len];
-    
     loopbackCmd[0] = 0x00;
     loopbackCmd[1] = 0xEE;
     loopbackCmd[2] = 0x00;
 int emv_generateAC(uint8_t refcontrolparam, uint8_t* cdolinput, uint8_t cdolinputlen, void* data)
 {
     uint16_t acCmd_len = 4 + 1 + cdolinputlen + 1; 
-    uint8_t acCmd[acCmd_len];
-    
+    uint8_t acCmd[acCmd_len];    
     acCmd[0] = 0x80;
     acCmd[1] = 0xAE;
     acCmd[2] = refcontrolparam;
 //Print out AIP Bit meanings
 int emv_decodeAIP(uint8_t* AIP)
 {
-    if((AIP[0] & AIP_SDA_SUPPORTED) == AIP_SDA_SUPPORTED)
-        Dbprintf("SDA supported");
-    if((AIP[0] & AIP_DDA_SUPPORTED) == AIP_DDA_SUPPORTED)
-        Dbprintf("DDA supported");  
-    if((AIP[0] & AIP_CARDHOLDER_VERIFICATION)==AIP_CARDHOLDER_VERIFICATION)
-        Dbprintf("Cardholder verification is supported");  
-    if((AIP[0] & AIP_TERMINAL_RISK) == AIP_TERMINAL_RISK)
-        Dbprintf("Terminal risk management is to be performed");  
-    if((AIP[0] & AIP_ISSUER_AUTH) == AIP_ISSUER_AUTH)
-        Dbprintf("Issuer authentication is supported ");  
-    if((AIP[0] & AIP_CDA_SUPPORTED) == AIP_CDA_SUPPORTED)
-        Dbprintf("CDA supported");  
-    if((AIP[1] & AIP_CHIP_SUPPORTED) == AIP_CHIP_SUPPORTED)
-        Dbprintf("Chip supported");
-    if((AIP[1] & AIP_MSR_SUPPORTED) == AIP_MSR_SUPPORTED)
-        Dbprintf("MSR supported"); 
+    if ((AIP[0] & AIP_SDA_SUPPORTED) == AIP_SDA_SUPPORTED)   Dbprintf("SDA supported");
+    if ((AIP[0] & AIP_DDA_SUPPORTED) == AIP_DDA_SUPPORTED)   Dbprintf("DDA supported");  
+    if ((AIP[0] & AIP_CARDHOLDER_VERIFICATION) == AIP_CARDHOLDER_VERIFICATION) Dbprintf("Cardholder verification is supported");  
+    if ((AIP[0] & AIP_TERMINAL_RISK) == AIP_TERMINAL_RISK)   Dbprintf("Terminal risk management is to be performed");  
+    if ((AIP[0] & AIP_ISSUER_AUTH) == AIP_ISSUER_AUTH)       Dbprintf("Issuer authentication is supported ");  
+    if ((AIP[0] & AIP_CDA_SUPPORTED) == AIP_CDA_SUPPORTED)   Dbprintf("CDA supported");  
+    if ((AIP[1] & AIP_CHIP_SUPPORTED) == AIP_CHIP_SUPPORTED) Dbprintf("Chip supported");
+    if ((AIP[1] & AIP_MSR_SUPPORTED) == AIP_MSR_SUPPORTED)   Dbprintf("MSR supported"); 
     return 0;
 }
 
     amountX = bytes_to_num(CVM, 4);
     amountY = bytes_to_num(CVM+4, 4); 
     counter +=8;
-    while(counter < CVMlen)
+    while (counter < CVMlen)
     {
-        if((CVM[counter] & 0x40) == 0x40){
-            if((CVM[counter] & 0x3F)== 0x00){
-                Dbprintf("Fail CVM processing");
-            }
-            if((CVM[counter] & 0x3F) == 0x01){
-                Dbprintf("Plaintext PIN verification performed by ICC");
-            }
-            if((CVM[counter] & 0x3F) == 0x02){
-                Dbprintf("Enciphered PIN verified online");
-            }
-            if((CVM[counter] & 0x3F) == 0x03){
-                Dbprintf("Plaintext PIN verification performed by ICC and signature (paper)");
-            }
-            if((CVM[counter] & 0x3F) == 0x04){
-                Dbprintf("Enciphered PIN verification performed by ICC");
-            }  
-            if((CVM[counter] & 0x3F) == 0x05){
-                Dbprintf("Enciphered PIN verification performed by ICC and signature (paper)");
-            }  
-            if((CVM[counter] & 0x3F) == 0x30){
-                Dbprintf("Signature (paper)");
-            }  
-            if((CVM[counter] & 0x7F) == 0x40){ // iceman, wrong masked used? changed from 0x3f -> 0x7f
-                Dbprintf("No CVM required");
-            }
+        if ((CVM[counter] & 0x40) == 0x40){
+            if ((CVM[counter] & 0x3F)== 0x00){  Dbprintf("Fail CVM processing");}
+            if ((CVM[counter] & 0x3F) == 0x01){ Dbprintf("Plaintext PIN verification performed by ICC");}
+            if ((CVM[counter] & 0x3F) == 0x02){ Dbprintf("Enciphered PIN verified online");}
+            if ((CVM[counter] & 0x3F) == 0x03){ Dbprintf("Plaintext PIN verification performed by ICC and signature (paper)");}
+            if ((CVM[counter] & 0x3F) == 0x04){ Dbprintf("Enciphered PIN verification performed by ICC");}  
+            if ((CVM[counter] & 0x3F) == 0x05){ Dbprintf("Enciphered PIN verification performed by ICC and signature (paper)");}  
+            if ((CVM[counter] & 0x3F) == 0x30){ Dbprintf("Signature (paper)");}  
+                        // iceman, wrong masked used? changed from 0x3f -> 0x7f
+            if ((CVM[counter] & 0x7F) == 0x40){ Dbprintf("No CVM required");}
             counter +=2; 
-        }
-        else{
+        } else {
             Dbprintf("Fail cardholder verification if this CVM is unsuccessful"); 
             counter +=2; 
         }
-        if(CVM[counter+1] == 0x00){
-            Dbprintf("Always");}
-        if(CVM[counter+1] == 0x01){
-            Dbprintf("If unattended cash");}
-        if(CVM[counter+1] == 0x02){
-            Dbprintf("If not unattended cash and not manual cash and not purchase with cashback");}
-        if(CVM[counter+1] == 0x03){
-            Dbprintf("If terminal supports the CVM");}
-        if(CVM[counter+1] == 0x04){
-            Dbprintf("If manual cash");}
-        if(CVM[counter+1] == 0x05){
-            Dbprintf("If purchase with cashback");}
-        if(CVM[counter+1] == 0x06){
-            Dbprintf("If transaction is in the application currency and is under %lu value", amountX);}
-         if(CVM[counter+1] == 0x07){
-            Dbprintf("If transaction is in the application currency and is over %lu value", amountX);}
-         if(CVM[counter+1] == 0x08){
-            Dbprintf("If transaction is in the application currency and is under %lu value", amountY);}
-         if(CVM[counter+1] == 0x09){
-            Dbprintf("If transaction is in the application currency and is over %lu value", amountY);}
+        if (CVM[counter+1] == 0x00){ Dbprintf("Always");}
+        if (CVM[counter+1] == 0x01){ Dbprintf("If unattended cash");}
+        if (CVM[counter+1] == 0x02){ Dbprintf("If not unattended cash and not manual cash and not purchase with cashback");}
+        if (CVM[counter+1] == 0x03){ Dbprintf("If terminal supports the CVM");}
+        if (CVM[counter+1] == 0x04){ Dbprintf("If manual cash");}
+        if (CVM[counter+1] == 0x05){ Dbprintf("If purchase with cashback");}
+        if (CVM[counter+1] == 0x06){ Dbprintf("If transaction is in the application currency and is under %" PRIu32 " value", amountX);}
+        if (CVM[counter+1] == 0x07){ Dbprintf("If transaction is in the application currency and is over %" PRIu32 " value", amountX);}
+        if (CVM[counter+1] == 0x08){ Dbprintf("If transaction is in the application currency and is under %" PRIu32 " value", amountY);}
+        if (CVM[counter+1] == 0x09){ Dbprintf("If transaction is in the application currency and is over %" PRIu32 " value", amountY);}
      }
     return 0;
 }
     DUMP(currentcard->SAK2);
     Dbhexdump(1,  ¤tcard->SAK2, false);
     DUMP(currentcard->ATS);
-    Dbhexdump(currentcard->ATS_len,  currentcard->ATS, false);
-    
+    Dbhexdump(currentcard->ATS_len,  currentcard->ATS, false);    
     DUMP(currentcard->tag_4F);
     Dbhexdump(currentcard->tag_4F_len,  currentcard->tag_4F, false);
     DUMP(currentcard->tag_50);
     Dbhexdump(currentcard->tag_86_len,  currentcard->tag_86, false);
     DUMP(currentcard->tag_87);
     Dbhexdump(1,  currentcard->tag_87, false);
-DUMP(currentcard->tag_88);
+       DUMP(currentcard->tag_88);
     Dbhexdump(1,  currentcard->tag_88, false);
-DUMP(currentcard->tag_8A);
+       DUMP(currentcard->tag_8A);
     Dbhexdump(2,  currentcard->tag_8A, false); 
     DUMP(currentcard->tag_8C);
     Dbhexdump(currentcard->tag_8C_len,  currentcard->tag_8C, false);
 
 
 #include <stdarg.h>
 #include <stdint.h>
+#include <inttypes.h>
 #include "proxmark3.h"
 #include "apps.h"
 #include "tlv.h"
 // reader voltage field detector
 #define EMV_MINFIELDV      4000
 
-// debug
-// 0 - no debug messages 1 - error messages 2 - all messages 4 - extended debug mode
-#define EMV_DBG_NONE          0
-#define EMV_DBG_ERROR         1
-#define EMV_DBG_ALL           2
-#define EMV_DBG_EXTENDED      4
-
-extern int EMV_DBGLEVEL;
-
 //EMV emulator states need to update
 #define EMVEMUL_NOFIELD      0
 #define EMVEMUL_IDLE         1
 
 uint64_t emlGetKey(int sectorNum, int keyType) {\r
        uint8_t key[6] = {0x00};\r
        uint8_t* emCARD = BigBuf_get_EM_addr();\r
-       \r
        memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);\r
        return bytes_to_num(key, 6);\r
 }\r
 \r
 void emlClearMem(void) {\r
-       int b;\r
-       \r
        const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r
        const uint8_t uid[]   =   {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};\r
        uint8_t* emCARD = BigBuf_get_EM_addr();\r
-       \r
        memset(emCARD, 0, CARD_MEMORY_SIZE);\r
        \r
        // fill sectors trailer data\r
-       for(b = 3; b < 256; b<127?(b+=4):(b+=16))\r
-               emlSetMem((uint8_t *)trailer, b , 1);\r
+       for(uint8_t b = 3; b < 256; b < 127 ? (b += 4) : (b += 16))\r
+               emlSetMem((uint8_t *)trailer, b, 1);\r
 \r
        // uid\r
        emlSetMem((uint8_t *)uid, 0, 1);\r
 
  */
 #include "printf.h"
 
-typedef uint32_t uintmax_t;
-typedef int32_t intmax_t;
-
 typedef unsigned char u_char;
 typedef unsigned int u_int;
-typedef unsigned long u_long;
-typedef unsigned short u_short;
 typedef unsigned long long u_quad_t;
 typedef long long quad_t;
-
+typedef unsigned long u_long;
+typedef unsigned short u_short;
 typedef int ssize_t;
+/*
+typedef uint32_t uintmax_t;
+typedef int32_t intmax_t;
+*/
 
 #define NBBY    8               /* number of bits in a byte */
 
 
 
 #include <stdarg.h>
 #include <stddef.h>
-//#include "util.h"
 #include "string.h"
 
 int kvsprintf(const char *format, void *arg, int radix, va_list ap) __attribute__ ((format (printf, 1, 0)));
 
+++ /dev/null
-//-----------------------------------------------------------------------------
-// Copyright (C) 2010 Hector Martin "marcan" <marcan@marcansoft.com>
-//
-// This code is licensed to you under the terms of the GNU GPL, version 2 or,
-// at your option, any later version. See the LICENSE.txt file for the text of
-// the license.
-//-----------------------------------------------------------------------------
-// Replacement stdint.h because GCC doesn't come with it yet (C99)
-//-----------------------------------------------------------------------------
-
-#ifndef __STDINT_H
-#define __STDINT_H
-
-typedef signed char                            int8_t;
-typedef short int                              int16_t;
-typedef int                                            int32_t;
-typedef long long int                  int64_t;
-
-typedef unsigned char                  uint8_t;
-typedef unsigned short int             uint16_t;
-typedef unsigned int                   uint32_t;
-typedef unsigned long long int uint64_t;
-
-typedef int                                            intptr_t;
-typedef unsigned int                   uintptr_t;
-
-#endif /* __STDINT_H */
 
 //-----------------------------------------------------------------------------
 #include "string.h"
 
-void *memcpy(void *dest, const void *src, int len)
+RAMFUNC void *memcpy(void *dest, const void *src, int len)
 {
        uint8_t *d = dest;
        const uint8_t *s = src;
        return dest;
 }
 
-int memcmp(const void *av, const void *bv, int len)
+RAMFUNC int memcmp(const void *av, const void *bv, int len)
 {
        const uint8_t *a = av;
        const uint8_t *b = bv;
 
        LED_B_OFF();
        LED_A_OFF();
 
-       AT91C_BASE_EFC0->EFC_FMR =
-               AT91C_MC_FWS_1FWS |
-               MC_FLASH_MODE_MASTER_CLK_IN_MHZ(48);
+       AT91C_BASE_EFC0->EFC_FMR = AT91C_MC_FWS_1FWS | MC_FLASH_MODE_MASTER_CLK_IN_MHZ(48);
 #ifdef HAS_512_FLASH
-       AT91C_BASE_EFC1->EFC_FMR =
-               AT91C_MC_FWS_1FWS |
-               MC_FLASH_MODE_MASTER_CLK_IN_MHZ(48);
+       AT91C_BASE_EFC1->EFC_FMR = AT91C_MC_FWS_1FWS | MC_FLASH_MODE_MASTER_CLK_IN_MHZ(48);
 #endif 
     // Initialize all system clocks
     ConfigClocks();
 
 #include <inttypes.h>
 #include "usb_cmd.h"
 
-/*
-#define lu PRIu32
-#define lx  PRIx32
-*/
 #define PROXPROMPT "pm3 --> "
 
 void SendCommand(UsbCommand *c);
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <inttypes.h>
 #include "reveng.h"
 
 /* Private declarations */
        if((string = malloc(size))) {
                sprintf(strbuf, "\"%s\"", model->name);
                sprintf(string,
-                               "width=%lu  "
+                               "width=%" PRIu32 ""
                                "poly=0x%s  "
                                "init=0x%s  "
                                "refin=%s  "
 
 // Major modes:
 //   000 --  LF reader (generic)
 //   001 --  LF edge detect (generic)
-//   010 --  LF passthrough
+//   010 --  LF passthru
 
 mux8 mux_ssp_clk               (major_mode, ssp_clk,   lr_ssp_clk,   le_ssp_clk,         1'b0,   1'b0, 1'b0, 1'b0, 1'b0, 1'b0);
 mux8 mux_ssp_din               (major_mode, ssp_din,   lr_ssp_din,         1'b0,   lp_ssp_din,   1'b0, 1'b0, 1'b0, 1'b0, 1'b0);